<?php

namespace App\Admin\Controllers\UserManagement\DocterManagement;

use App\Models\DocterSetting;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AppointController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '门诊预约配置列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DocterSetting());
        $grid->disableCreateButton();
        $grid->disableActions();
        $grid->disableBatchActions();
        $docter_id = request('docter_id');
        $grid->model()->where('docter_id',$docter_id)->where('type',1);
        $grid->column('id', __('Id'));
        $grid->column('docter_id', __('医生id'));
        $grid->column('docter.name', __('医生姓名'));
        $grid->column('docter.avatar', __('医生头像'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('organization.name', __('机构名'));
        $grid->column('type', __('类型'))->using([1=>'门诊预约']);
        $grid->column('show_days', __('可预约天数'));
        $grid->column('service_times', __('就诊时长'));
        $grid->column('service_num', __('同时服务订单数'));
        $grid->column('appoint_price', __('预约单价'));

        $grid->column('am', __('上午服务时间'))->display(function (){
            $service_time = DocterSetting::where('docter_id',$this->docter_id)->where('id',$this->id)->value('service_time');
            $arr = json_decode($service_time);
            $obj_arr = object_array($arr);
            if (empty($obj_arr[1]))
            {
                return '无';
            }else
            {
                $str = $obj_arr[1]['amStartTime'].'-'.$obj_arr[1]['amEndTime'];
                return $str;
            }
        });
        $grid->column('pm', __('下午服务时间'))->display(function (){
            $service_time = DocterSetting::where('docter_id',$this->docter_id)->where('id',$this->id)->value('service_time');
            $arr = json_decode($service_time);
            $obj_arr = object_array($arr);
            if (empty($obj_arr[2]))
            {
                return '无';
            }else
            {
                $str = $obj_arr[2]['pmStartTime'].'-'.$obj_arr[2]['pmEndTime'];
                return $str;
            }
        });
        $grid->column('night', __('晚上服务时间'))->display(function (){
            $service_time = DocterSetting::where('docter_id',$this->docter_id)->where('id',$this->id)->value('service_time');
            $arr = json_decode($service_time);
            $obj_arr = object_array($arr);
            if (empty($obj_arr[3]))
            {
                return '无';
            }else
            {
                $str = $obj_arr[3]['nightStartTime'].'-'.$obj_arr[3]['nightEndTime'];
                return $str;
            }
        });
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('docter_id', __('Docter id'));
        $show->field('type', __('Type'));
        $show->field('status', __('Status'));
        $show->field('show_days', __('Show days'));
        $show->field('service_times', __('Service times'));
        $show->field('service_num', __('Service num'));
        $show->field('org_id', __('Org id'));
        $show->field('appoint_price', __('Appoint price'));
        $show->field('service_time', __('Service time'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new DocterSetting());

        $form->number('docter_id', __('Docter id'));
        $form->number('type', __('Type'));
        $form->number('status', __('Status'));
        $form->number('show_days', __('Show days'));
        $form->number('service_times', __('Service times'));
        $form->number('service_num', __('Service num'));
        $form->number('org_id', __('Org id'));
        $form->decimal('appoint_price', __('Appoint price'))->default(0.00);
        $form->text('service_time', __('Service time'));

        return $form;
    }
}
