<?php

namespace App\Http\Requests\Menu;

use Illuminate\Foundation\Http\FormRequest;

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

    public function rules()
    {
        return [
            'name' => 'required|string|max:50',
            'parent_id' => [
                'nullable',
                'integer',
                function ($attribute, $value, $fail) {
                    if ($value !== null && $value !== 0 && $value !== '' && $value !== '0') {
                        if (! \DB::table('admin_menus')->where('id', $value)->exists()) {
                            $fail('父菜单不存在');
                        }
                    }
                },
            ],
            'icon' => 'nullable|string|max:50',
            'path' => 'nullable|string|max:200',
            'component' => 'nullable|string|max:200',
            'permission_code' => 'nullable|string|max:100',
            'type' => 'nullable|in:1,2,3',
            'visible' => 'nullable|in:0,1',
            'sort' => 'nullable|integer|min:0',
            'status' => 'nullable|in:0,1',
            'terminal_type' => 'nullable|string|max:50',
        ];
    }

    public function attributes()
    {
        return [
            'name' => '菜单名称',
            'parent_id' => '父菜单',
            'icon' => '图标',
            'path' => '路径',
            'component' => '组件',
            'permission_code' => '权限编码',
            'type' => '类型',
            'visible' => '是否可见',
            'sort' => '排序',
            'status' => '状态',
        ];
    }
}
