<?php

namespace App\Admin\Controllers\Staff;

use App\Models\Category;
use App\Models\Staff;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class IndexController extends AdminController
{

    protected $title = '领导管理';

    public function grid(): Grid
    {
        $grid = new Grid(new Staff());

        $grid->model()->with('category')->oldest('order');

        $grid->filter(function (Grid\Filter $filter) {
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('name', '姓名');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->like('category.title', '所属分类');
            });
            $filter->column(1 / 3, function (Grid\Filter $filter) {
                $filter->equal('status', '状态')->select(Staff::STATUS);
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('name', '姓名');
        $grid->column('cover', '头像')->image('', 60, 60);
        $grid->column('category.title', '所属分类');
        $grid->column('order', '排序')->editable();
        $grid->column('status', '状态')->bool();
        $grid->column('created_at', '创建时间');

        return $grid;
    }

    public function form(): Form
    {
        $form = new Form(new Staff());

        $form->text('name', '姓名')
            ->rules([
                'required',
                'max:4',
            ], [
                'max' => '姓名最大长度不能超过 :max 个字符',
            ])
            ->required();
        $form->select('category_id', '所属分类')
            ->options(function () {
                return Category::where('type', Category::TYPE_PERSON)->pluck('title', 'id');
            })
            ->required();

        $form->image('cover', '头像')
            ->move('images/'.date('Y/m/d'))
            ->removable()
            ->uniqueName();

        $form->text('job', '职位')->required();
        $form->textarea('duty', '分工');

        $form->ueditor('content', '简历')->required();
        $form->number('order','封面')->default(0);
        $form->switch('status', '状态')->default(1);

        return $form;
    }

}
