<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\PrescriptionRepository;
use App\Models\DiagnosiModel;
use App\Models\DoctorModel;
use App\Models\InquiryModel;
use App\Models\PatientModel;
use App\Models\PharmacistModel;
use App\Models\PharmacyDrugModel;
use App\Models\PharmacyModel;
use App\Models\PrescriptionModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

// 处方列表
class PrescriptionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PrescriptionRepository(), function (Grid $grid) {
            $grid->model()->orderBy('id', 'DESC');

            $grid->column('id', '处方单编号')->sortable();
            $grid->column('status')->using(PrescriptionModel::PRESCRIPTION_STATUS_MAP);
            $grid->column('patient_name');
            $grid->column('patient_age');
            $grid->column('patient_gender');
            $grid->column('diagnosis_name');
            $grid->column('doctor_name');
            $grid->column('pharmacist_name');
            $grid->column('drug_info');
            $grid->column('pharmacy_name');
            // $grid->column('diagnosis_id');
            // $grid->column('inquiry_info');
            // $grid->column('doctor_id');
            // $grid->column('doctor_online_hospital_name');
            // $grid->column('doctor_department');
            // $grid->column('doctor_title');
            // $grid->column('doctor_license_no');
            // $grid->column('pharmacy_id');
            // $grid->column('pharmacist_id');
            // $grid->column('pharmacist_license_number');
            $grid->column('created_at');
            // $grid->column('updated_at')->sortable();

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

                $filter->like('patient_name')->width(3);
                $filter->like('diagnosis_name')->width(3);
                $filter->equal('pharmacy_id')->select(PharmacyModel::all()->pluck('name', 'id'))->width(3);
                $filter->in('status')->checkbox(PrescriptionModel::PRESCRIPTION_STATUS_MAP)->width(3);
            });

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

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

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PrescriptionRepository(), function (Show $show) {
            $show->field('id')->width(4);
            $show->field('status')->width(4);
            $show->field('patient_name')->width(4);
            $show->field('patient_age')->width(4);
            $show->field('patient_gender')->width(4);
            $show->field('diagnosis_id')->width(4);
            $show->field('diagnosis_name')->width(4);
            $show->field('inquiry_info')->width(4);
            $show->field('drug_info')->width(4);
            $show->field('doctor_id')->width(4);
            $show->field('doctor_name')->width(4);
            $show->field('doctor_online_hospital_name')->width(4);
            $show->field('doctor_department')->width(4);
            $show->field('doctor_title')->width(4);
            $show->field('doctor_license_no')->width(4);
            $show->field('pharmacy_id')->width(4);
            $show->field('pharmacy_name')->width(4);
            $show->field('pharmacist_id')->width(4);
            $show->field('pharmacist_name')->width(4);
            $show->field('pharmacist_license_number')->width(4);
            $show->field('created_at')->width(4);
            $show->field('updated_at')->width(4);

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

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PrescriptionRepository(), function (Form $form) {
            $form->display('id')->width(4);
            $form->radio('status')->options(PrescriptionModel::PRESCRIPTION_STATUS_MAP)->default(PrescriptionModel::PRESCRIPTION_STATUS_PENDING);

            $form->select('patient_id')->options(PatientModel::all()->pluck('name', 'id'))->width(4);
            $form->hidden('patient_name');
            $form->hidden('patient_age');
            $form->hidden('patient_gender');

            $form->select('diagnosis_id')->options(DiagnosiModel::all()->pluck('name', 'id'))->width(4);
            $form->hidden('diagnosis_name');

            $form->select('doctor_id')->options(DoctorModel::where('status', 1)->pluck('name', 'id'))->width(4);
            $form->hidden('doctor_name');
            $form->hidden('doctor_online_hospital_name');
            $form->hidden('doctor_department');
            $form->hidden('doctor_title');
            $form->hidden('doctor_license_no');
            $form->hidden('doctor_signed_pic');

            $form->select('pharmacy_id')->options(PharmacyModel::where('status', 1)->pluck('name', 'id'))->width(4);
            $form->hidden('pharmacy_name');

            $form->select('pharmacist_id')->options(PharmacistModel::where('status', 1)->pluck('name', 'id'))->width(4);
            $form->hidden('pharmacist_name');
            $form->hidden('pharmacist_license_number');
            $form->hidden('pharmacist_signed_pic');

            $form->multipleSelect('inquiry_info')->options(InquiryModel::all()->pluck('question', 'id'))->width(4)->saving(function ($v) {
                return json_encode($v);
            });

            $form->multipleSelect('drug_info')->options(PharmacyDrugModel::with('drug')->get()->pluck('drug')->pluck('name', 'id'))->width(4)->saving(function ($v) {
                return json_encode($v);
            });
            $form->switch('is_voided')->width(4);

            $form->display('created_at')->width(4);
            $form->display('updated_at')->width(4);
            // 添加隐藏字段以保存冗余数据

            $form->saving(function (Form $form) {
                $patient = PatientModel::find($form->input('patient_id'));
                $doctor = DoctorModel::find($form->input('doctor_id'));
                $pharmacist = PharmacistModel::find($form->input('pharmacist_id'));
                $diagnosis = DiagnosiModel::find($form->input('diagnosis_id'));
                $pharmacy = PharmacyModel::find($form->input('pharmacy_id'));

                // 根据身份证计算年龄和性别
                if ($patient) {
                    $form->patient_name = $patient->name;
                    $form->patient_age = getAgeByIdCard($patient->id_card);
                    $form->patient_gender = $patient->gender;
                }

                if ($diagnosis) {
                    $form->diagnosis_name = $diagnosis->name; // 假设有诊断名称字段
                }

                if ($doctor) {
                    $form->doctor_name = $doctor->name;
                    $form->doctor_online_hospital_name = $doctor->online_hospital_name; // 假设有在线医院名称字段
                    $form->doctor_department = $doctor->department;
                    $form->doctor_title = $doctor->doctor_title;
                    $form->doctor_license_no = $doctor->license_no;
                    $form->doctor_signed_pic = $doctor->signed_pic;
                }

                if ($pharmacist) {
                    $form->pharmacist_name = $pharmacist->name;
                    $form->pharmacist_license_number = $pharmacist->license_number;
                    $form->pharmacist_signed_pic = $pharmacist->signed_pic;
                }

                if ($pharmacy) {
                    $form->pharmacy_name = $pharmacy->name;
                }
            });
            // 右上角按钮控制
            $form->disableDeleteButton(); // 去掉删除按钮
        });
    }

    // 计算年龄的辅助方法
    private function calculateAge($id_card)
    {
        // 根据身份证计算年龄的逻辑
        // 返回计算出的年龄
    }
}
