<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class TeamUserRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'name'     => 'required|min:2',
            'username' => 'required|unique:users',
            'email'    => 'required|email|unique:users',
            'position' => 'required',
            'password' => 'required|min:3'
        ];


        if ($this->isMethod('patch') || $this->isMethod('put')) {
            $rules = array_except($rules, ['password', 'username', 'email']);
            $rules['username'] = 'required';
            $rules['email'] = 'required';
        }

        return $rules;
    }

    public function messages()
    {
        return [
            'name.required'      => '姓名不能为空',
            'username.required'  => '用户名不能为空',
            'username.unique'    => '用户名已存在',
            'email.required'     => '邮箱不能为空',
            'email.email'        => '邮箱格式不对',
            'email.unique'       => '邮箱已存在',
            'position.required'  => '职位不能为空',
            'password.required'  => '密码不能为空',
        ];
    }
}
