<?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 OrderNurse());
        $grid->model()->orderByDesc('id');

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

        if(!$is_admin){
            $org_id = $user->org_id;
            $docter_id = $user->docter_id;

            $grid->model()->whereHas('orders',function ($query) use ($org_id,$docter_id) {
                if($docter_id){
                    $where['docter_id'] = $docter_id;
                 } else {
                    $where['organization_id']=$org_id;
                }
                $query->where($where);
            });
        }

        $grid->column('id', __('ID'));
        $grid->column('orders.user_id', __('预约用户'))->display(function($w){
            return User::where('id',$w)->value('nickname');
        });
        $grid->column('patients.appoint_start_time', __('预约时间'))->display(function ($w){
            return date('Y-m-d H:i:s',$w);
        });

        $grid->column('patients.appoint_end_time', __('儿保时间'))->display(function ($w){
            return date('Y-m-d H:i:s',$w);
        });
        $grid->column('patients.name', __('儿保患者'));
        $grid->column('patients.phone', __('电话'));
        $grid->column('patients.sex', __('患者性别'))->display(function ($w){
            return $w==1?'男':'女';
        });
        $grid->column('patients.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('orders.order_status','订单状态')->using(Order::getStatus())->label([1=>'info',2=>'warring',3=>'info',4=>'success',5=>'info']);
        $grid->column('orders.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('orders.order_status','订单状态')->select(Order::$_order_status);
                $filter->between('patients.appoint_start_time','预约时间')->datetime();
            });
        });

        $grid->actions(function ($actions) {
            if(!empty($actions->row->orders) && $actions->row->orders->order_status < 2){
                $actions->add(new Reserved());
            }
            if(!empty($actions->row->orders) && $actions->row->orders->order_status<= 3){
                $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('order_id', __('Order id'));
        $show->field('order_patient_id', __('Order patient id'));
        $show->field('nurse_id', __('Nurse id'));
        $show->field('nurse_name', __('Nurse name'));
        $show->field('nurse_price', __('Nurse price'));
        $show->field('nurse_remark', __('Nurse remark'));
        $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 OrderNurse());

        $form->display('order_id', __(' 订单id'));
        $form->display('patients.name', __('预约用户'));
        $form->display('nurse_name', __('项目名称'));
        $form->text('nurse_price', __('订单价格'));
        $form->textarea('nurse_remark', __('备注'))->rows(5);

        $form->setWidth(6);

        return $form;
    }
}
