<?php

namespace App\Community\Controllers;

use App\Community\Actions\Clinc\Finished;
use App\Community\Actions\Clinc\OrderCancel;
use App\Community\Actions\Clinc\Reserved;
use App\Models\Docter;
use App\Models\Order;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

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

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $user = Admin::user();
        $is_admin = Admin::user()->inRoles(['administrator','devloper']);
        $grid->model()->orderByDesc('id');

        //检查是否是门诊医生
        $is_docter = $user->isRole('opc');
        if(!$is_admin){
            $org_id = $user->org_id;
            $where = ['product_type'=>3,'organization_id'=>$org_id];
            if($is_docter) {
                $where['docter_id'] = Docter::where(['id'=>$user->docter_id])->value('id');
            }
            $grid->model()->where($where);
        }
        $grid->column('id', __('Id'));
        $grid->column('orderUser.nickname', __('预约用户'));
        $grid->column('orderPatient.appoint_start_time', __('预约时间'))->display(function ($w){
            $end ='';
            $start = '';
            if(!empty($this->orderPatient) && $this->orderPatient->appoint_end_time){
                $end = date('H:i',$this->orderPatient->appoint_end_time);
            }
            if(!empty($w)){
                $start = date('Y-m-d H:i',$w);
            }
            return $start.'~'.$end;
        });
//        $grid->column('orderPatient.appoint_end_time', __('预约结束时间'))->display(function ($w){
//            if(empty($w)) return '';
//            return date('Y-m-d H:i',$w);
//        });
        $grid->column('orderPatient.phone', __('电话号码'))->display(function ($p){
            if(empty($p))  return $this->user->phone;
            return $p;
        });

        $grid->column('orderPatient.name', __('预约患者'));
        $grid->column('orderPatient.sex', __('患者性别'))->display(function ($w){
            return $w==1?'男':'女';
        });
        $grid->column('orderPatient.birthday', __('患者年龄'))->display(function ($w){
            return birthday_to_age($w);
        });;
        $grid->column('docter.name', __('预约医生'));
//        $grid->column('order_status', __('订单状态'))->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消']);
        $grid->column('order_status','订单状态')->using(Order::getStatus())->label([1=>'info',2=>'warring',3=>'info',4=>'success',5=>'danger']);
        $grid->column('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款']);
        //查询
        $grid->quickSearch(function ($model, $query) {
            $model->whereHas('patients',function ($model) use ($query) {
                $model->where('name', 'like', "{$query}%");
            });
        })->placeholder('请输入用户姓名');
        $grid->actions(function ($actions) {
//            if(!empty($actions->row->order_status) && $actions->row->order_status < 2){
//                $actions->add(new Reserved());
//            }
            if(!empty($actions->row->order_status) && ($actions->row->order_status<= 3 || $actions->row->order_status == 7) ){
                $actions->add(new OrderCancel());
                $actions->add(new Finished());
            }
//            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
        });
        $grid->filter(function (Grid\Filter $filter)  {
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter)   {
                $filter->equal('docter_id','排班医生')->select('/cdms/api/getDocter');
                $filter->equal('order_status','订单状态')->select(Order::$_order_status);
                $filter->between('orderPatient.appoint_start_time','预约时间')->datetime();
            });
        });
        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('orderUser.nickname', __('预约用户'));
        $show->field('orderPatient.sex', __('患者性别'))->as(function ($w){
            return $w==1?'男':'女';
        });
        $show->field('orderPatient.appoint_start_time', __('开始时间'));
        $show->field('orderPatient.appoint_end_time', __('结束时间'));
        $show->field('orderPatient.birthday', __('患者年龄'));
        $show->field('docter.name', __('预约医生'));
        $show->field('order_status', __('订单状态'))->using(Order::getStatus())->label('info');
        $show->field('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款'])->label('info');
        $show->panel()->tools(function ($tools) {
                $tools->disableEdit();
                $tools->disableDelete();
        });

        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

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

        $form->display('orderUser.nickname', __('用户'));
        $form->display('docter_id', __('医生'));

        return $form;
    }
}
