<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\CleanData;
use App\Admin\Actions\UserImport;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class UserController extends AdminController
{

    protected $title = '用户管理';

    public function grid(): Grid
    {
        $grid = new Grid(new User);
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new UserImport);
            $tools->append(new CleanData);
        });
        $grid->filter(function (Grid\Filter $v) {
            $v->column(1 / 2, function (Grid\Filter $f) {
                $f->like('name', '姓名');
            });
            $v->column(1 / 2, function (Grid\Filter $f) {
                $f->equal('mobile', '手机号');
            });
        });
        $grid->column('归属干事')->display(function () {
            return $this->parent->name ?? '';
        });
        $grid->column('mobile', '手机号');
        $grid->column('name', '姓名');
        $grid->column('sign', '签到')->bool();
        $grid->column('type', '角色')->using([
            0 => '投票人',
            1 => '干事',
            2 => '总干事',
        ]);
        $grid->column('created_at', '创建时间')->date('Y-m-d H:i:s');;

        return $grid;
    }

    protected function form(): Form
    {
        $form = new Form(new User);

        $form->text('name', '姓名');
        $form->text('mobile', '手机号');
        $form->select('parent_id', '归属干事')->options(
            User::where('type', 1)->pluck('name', 'id')
        )->default(0);
        $form->select('type')->options([
            0 => '投票人',
            1 => '干事',
            2 => '总干事',
        ])->help('只有干事，才可以呗归属，投票人必须要有归属，干事不可以有归属。');

        return $form;
    }

}
