<?php

namespace App\Http\Requests\User;

use Illuminate\Foundation\Http\FormRequest;

class StoreUserRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'username' => 'required|string|max:50|unique:admin_users,username',
            'password' => 'required|string|min:6|max:20',
            'name' => 'required|string|max:50',
            'email' => 'nullable|email|max:100|unique:admin_users,email',
            'phone' => 'nullable|string|max:20',
            'avatar' => 'nullable|string|max:255',
            'status' => 'nullable|in:0,1',
            'role_id' => 'nullable|integer|exists:admin_roles,id',
            'role_ids' => 'nullable|array',
            'role_ids.*' => 'exists:admin_roles,id',
            'ext_fields' => 'nullable|array',
        ];
    }

    public function attributes()
    {
        return [
            'username' => '用户名',
            'password' => '密码',
            'name' => '姓名',
            'email' => '邮箱',
            'phone' => '手机号',
            'avatar' => '头像',
            'status' => '状态',
            'role_ids' => '角色',
        ];
    }

    public function messages()
    {
        return [
            'username.unique' => ':attribute已存在，请更换',
            'username.required' => ':attribute不能为空',
            'username.max' => ':attribute不能超过:max个字符',
            'password.required' => ':attribute不能为空',
            'password.min' => ':attribute不能少于:min个字符',
            'password.max' => ':attribute不能超过:max个字符',
            'name.required' => ':attribute不能为空',
            'name.max' => ':attribute不能超过:max个字符',
            'email.email' => ':attribute格式不正确',
            'email.unique' => ':attribute已存在，请更换',
            'email.max' => ':attribute不能超过:max个字符',
            'phone.max' => ':attribute不能超过:max个字符',
            'role_id.exists' => '所选:attribute不存在',
            'role_ids.*.exists' => '所选:attribute不存在',
            'status.in' => ':attribute值不合法',
        ];
    }
}
