<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PatientRepository;
use App\Models\PatientModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

// 问诊人
class PatientController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PatientRepository(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('id_card');
            $grid->column('gender', '性别')->using(PatientModel::SEX_MAP);
            $grid->column('age', '年龄')->display(function ($value) {
                return getAgeByIdCard($this->id_card);
            });
            $grid->column('mobile');
            $grid->column('miniapp_openid');
            $grid->column('avatar');
            $grid->column('nick_name');

            // 快捷搜索
            $grid->quickSearch(['name', 'id_card', 'mobile'])->placeholder('请输入[姓名/身份证/手机号]')->width(25);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel(); // 更改为 panel 布局
                $filter->expand(); // 默认展开搜索框

                $filter->like('name')->width(3);
                $filter->like('id_card')->width(3);
                $filter->like('mobile')->width(3);
            });

            // 行按钮控制
            $grid->disableCreateButton(); // 禁用创建按钮
            $grid->disableEditButton(); // 禁用编辑按钮
            $grid->disableDeleteButton(); // 禁用删除按钮

            // 工具栏按钮控制
            $grid->disableBatchDelete(); // 禁用批量删除
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PatientRepository(), function (Show $show) {
            $show->field('id')->width(4);
            $show->field('name')->width(4);
            $show->field('gender')->width(4);
            $show->field('id_card')->width(4);
            $show->field('mobile')->width(4);
            $show->field('miniapp_openid')->width(4);
            $show->field('avatar')->width(4);
            $show->field('nick_name')->width(4);
            $show->field('created_at')->width(4);
            $show->field('updated_at')->width(4);

            $show->panel()->tools(function ($tools) {
                $tools->disableEdit(); // 禁止编辑
                $tools->disableDelete(); // 禁止删除按钮
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PatientRepository(), function (Form $form) {
            $form->display('id')->width(4);
            $form->text('name')->width(4);
            $form->text('gender')->width(4);
            $form->text('id_card')->width(4);
            $form->text('mobile')->width(4);
            $form->text('miniapp_openid')->width(4);
            $form->text('avatar')->width(4);
            $form->text('nick_name')->width(4);

            $form->display('created_at')->width(4);
            $form->display('updated_at')->width(4);

            // 右上角按钮控制
            $form->disableDeleteButton(); // 去掉删除按钮
        });
    }
}
