<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class MemberRequest 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 = [
            'group_id' => 'required',
            'username' => 'required|unique:members',
            'email'    => 'required|unique:members',
            'password' => 'required|confirmed|min:3',
        ];

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

        return $rules;
    }

    /**
     * Get custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'group_id.required'  => '请选择所属会员组',
            'username.required'  => '用户名不能为空',
            'username.unique'    => '用户名已经存在',
            'email.required'     => '邮箱不能为空',
            'email.unique'       => '邮箱已经存在',
            'password.required'  => '密码不能为空',
            'password.confirmed' => '两次密码不匹配',
        ];
    }
}
