<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Community\Nurse\DelNurse;
use App\Community\Actions\Nurse\Finished;
use App\Community\Actions\Nurse\OrderCance;
use App\Community\Actions\Nurse\Reserved;
use App\Models\Docter;
use App\Models\DocterOrganization;
use App\Models\Order;
use App\Models\OrderNurse;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderNurseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '儿保预约';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $grid->model()->orderByDesc('id');

        $user = Admin::user();
        $is_admin = Admin::user()->inRoles(['administrator','developer']);

        $where = ['product_type'=>5];
        if(!$is_admin){
            $org_id = $user->org_id;
            $docter_id = $user->docter_id;
            if($docter_id){
                $where['docter_id'] = $docter_id;
            } else {
                $where['organization_id']=$org_id;
            }
        }
        $grid->model()->where($where);

        $grid->column('id', __('ID'));
        $grid->column('orderUser.user_name', __('预约用户'))->display(function($w){
            if(empty($w)) return '';
            return $w;
        });
        $grid->column('orderPatient.appoint_start_time', __('预约时间'))->display(function ($w){
            if(empty($w)) return '';
            return date('Y-m-d H:i',$w).'~'.date('H:i',$w);
        });

        $grid->column('orderPatient.name', __('儿保患者'));
        $grid->column('orderPatient.phoe', __('电话'))->display(function ($w){
            if(empty($w)) {
                if(empty($this->orderUser->phone)) return '';
                return $this->orderUser->phone;
            }
            return $w;
        });
        $grid->column('orderPatient.sex', __('患者性别'))->display(function ($w){
            return $w==1?'男':'女';
        });
        $grid->column('orderPatient.birthday', __('患者年龄'))->display(function ($w){
            return birthday_to_age($w);
        });
//        $grid->column('orders.docter_id', __('排班医生'))->display(function ($w){
//            return Docter::where('id',$w)->value('name');
//        });
        $grid->column('nurse_name', __('项目名称'));
        $grid->column('order_status','订单状态')->using(Order::getStatus())->label([1=>'info',2=>'warring',3=>'info',4=>'success',5=>'info']);
        $grid->column('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款'])->label('info');

        //查询
        $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('orders.docter_id','排班医生')->select('/cdms/api/getDocter');
                $filter->equal('order_status','订单状态')->select(Order::$_order_status);
                $filter->between('orderPatient.appoint_start_time','预约时间')->datetime();
            });
        });

        $grid->actions(function ($actions) {
//            if(!empty($actions->row->orders) && $actions->row->orders->order_status < 2){
//                $actions->add(new Reserved());
//            }
            if($actions->row->order_status<= 3 || $actions->row->order_status == 7 ){
                $actions->add(new OrderCance());
                $actions->add(new Finished());
            }

//            $actions->add(new DelNurse());
            $actions->disableView();
            $actions->disableDelete();
        });

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('orderPatient.name', __('患者用户'));
        $show->field('orderUser.user_nam', __('患者名称'));
        $show->field('orderNurse.nurse_name', __('儿保项目'));
        $show->field('orderNurse.nurse_price', __('订单价格'));
        $show->field('nurse_remark', __('儿保备注'));
        $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('id', __(' 订单id'));
        $form->display('orderUser.user_name', __('预约用户'));
        $form->display('orderPatient.name', __('预约患者'));
        $form->display('orderNurse.nurse_name', __('项目名称'));
        $form->text('orderNurse.nurse_price', __('订单价格'));
        $form->textarea('nurse_remark', __('备注'))->rows(5);

        $form->setWidth(6);

        return $form;
    }
}
