<?php

namespace App\Admin\Controllers\Leader;

use App\Models\Category;
use App\Models\Leader;
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 Leader());

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

        $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(Leader::STATUS);
            });
        });

        $grid->column('id', '#ID#');
        $grid->column('title', '标题');
        $grid->column('cover', '封面')->image('', 60, 60);
        $grid->column('category.title', '所属分类');
        $grid->column('sort', '排序')->editable();
        $grid->column('status', '状态')->bool();
        $grid->column('created_at', '创建时间');

        return $grid;
    }

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

        $form->text('title', '标题')->required();

        $form->select('category_id', '所属分类')
            ->options(function () {
                return Category::where('type', Category::TYPE_LEADER)->pluck('title', 'id');
            })
            ->required();

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

        $form->text('head', '带头人')->required();
        $form->text('reserve', '后备带头人')->required();
        $form->textarea('description', '简介');
        $form->ueditor('content', '详情')->required();
        $form->number('sort', '封面')->default(0);
        $form->switch('status', '状态')->default(1);

        return $form;
    }

}
