<?php

namespace App\Community\Controllers;

use App\Models\Order;
use App\Models\OrderNurse;
use App\Models\Patient;
use App\Models\PatientRemark;
use App\Models\Serviceapplys;
use EasyWeChat\Factory;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NurseUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '儿保用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $is_amdin = Admin::user()->isRole('administrator');

        $grid->model()->orderByDesc('id');
        $grid->model()->GroupBy('patient_id');
        $grid->column('orderPatient.name', __('姓名'));
        $grid->column('orderUser.nickname', __('监护人'));
        $grid->column('orderPatient.phone', __('监护人手机号'))->display(function ($w){
            if(empty($w)) return Patient::where('id',$this->patient_id)->value('phone');
            return $w;
        });;
        $grid->column('orderPatient.birthday', __('出生日期'));

        if(!$is_amdin){
            $grid->model()->where(['organization_id'=>Admin::user()->org_id,'product_type'=>5]);
            $grid->column('patient_id', __('备注'))->display(function ($w){
                return PatientRemark::where(['org_id'=>intval(Admin::user()->org_id),'patient_id'=>$w,'type'=>1])->value('remark');
            });
        }
        if($is_amdin){
            $grid->actions(function ($actions){
                $actions->disableDelete();
                $actions->disableView();
                $actions->disableEdit();
            });
        }

        $grid->filter(function ($flter){
            $flter->like('orderPatient.name','请输入用户姓名');
            $flter->like('orderUser.nickname','请输入监护人姓名');
            $flter->between('orderUser.birthday','生日')->datetime();
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(OrderNurse::findOrFail($id));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Order());
        $form->setAction('/cdms/api/paitent_remark');
        $form->hidden('patient_id');
        $org_id = Admin::user()->org_id;
        $form->hidden('type')->value(1);
        if(!empty($org_id)){
            $form->textarea('remark','备注')->default(function () use ($org_id,$form){
                return PatientRemark::where(['org_id'=>intval($org_id),'patient_id'=>$form->model()->patient_id,'type'=>1])->value('remark');
            });

        }
            $form->hidden('org_id')->value(intval($org_id));
        $form->saving(function ($form){
            $form->type = 1;
        });
        return $form;
    }
}
