<?php

namespace App\Community\Controllers;

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){
            return date('Y-m-d H:i:s',$w);
        });
        $grid->column('orderPatient.appoint_end_time', __('门诊时间'))->display(function ($w){
            if(empty($w)) return '';
            return date('Y-m-d H:i:s',$w);
        });
        $grid->column('orderPatient.phone', __('电话号码'));

        $grid->column('orderPatient.name', __('预约患者'))->display(function ($w){
            return $w==1?'男':'女';
        });;
        $grid->column('orderPatient.sex', __('患者性别'));
        $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=>'error']);
        $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->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('user_id', __('User id'));
        $show->field('docter_id', __('Docter id'));
        $show->field('patient_id', __('Patient id'));
        $show->field('organization_id', __('Organization id'));
        $show->field('order_sn', __('Order sn'));
        $show->field('payment_type', __('Payment type'));
        $show->field('product_type', __('Product type'));
        $show->field('order_status', __('Order status'));
        $show->field('payment_status', __('Payment status'));
        $show->field('total_amount', __('Total amount'));
        $show->field('payment_amount', __('Payment amount'));
        $show->field('discount_amount', __('Discount amount'));
        $show->field('payment_time', __('Payment 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 Order());

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

        return $form;
    }
}
