<?php

namespace App\Community\Controllers;

use App\Community\Actions\Vaccine\AddVaccine;
use App\Community\Actions\Vaccine\Finished;
use App\Community\Actions\Vaccine\OrderCance;
use App\Community\Actions\Vaccine\Reserved;
use App\Models\Docter;
use App\Models\Order;
use App\Models\OrderVaccine;
use App\Models\TimePeriod;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderVaccinesController 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','devloper']);
        //检查是否是门诊医生
        $is_docter = $user->docter_id;
        if(!$is_admin){
            $org_id = $user->org_id;
            $where = ['product_type'=>4,'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('orderPatient.name', __('预约用户'));
        $grid->column('yuyue', __('预约时间'))->display(function ($w){
            if(empty($this->orderPatient) || empty($this->orderPatient->appoint_start_time)) return '';
            return date('Y-m-d H:i:s',($this->orderPatient->appoint_start_time)) .'致' . date('Y-m-d H:i:s',($this->orderPatient->appoint_end_time));
        });
        $grid->column('end_time', __('接种时间'))->display(function ($w){
            if(empty($w)) return '';
            return date('Y-m-d',$w);
        });
        $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('vac_name', __('接种疫苗'))->display(function ($w){
            $vaccine_name = OrderVaccine::where('order_id',$this->id)->pluck('vaccine_name')->toArray();
            if(empty($vaccine_name) ) return '';
            return implode(',',$vaccine_name);

        });
        $grid->column('docter.name', __('排班医生'));
        $grid->column('order_status', __('订单状态'))->using(Order::getStatus())->label('info');
        $grid->column('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款'])->label('info');

        $grid->filter(function ($filter)  {
            $filter->column(1/2, function ($filter) {
                $filter->equal('docter.id','排班医生')->select('/cdms/api/getDocter');
                $filter->equal('order_status','订单状态')->select(Order::$_order_status);
                $filter->between('patients.appoint_start_time','预约时间')->datetime();
            });
        });

        $grid->actions(function ($actions) {
            if( $actions->row->order_status<= 2 ){
                $actions->add(new Reserved());
                $actions->add(new OrderCance());
            }

            if( $actions->row->order_status<= 3 ){
                $actions->add(new Finished());
                $actions->add(new AddVaccine());
            }

            $actions->disableEdit();
//            $actions->disableView();
            $actions->disableDelete();
        });

        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('orderPatient.name', __('患者姓名'));
        $show->field('orderPatient.sex', __('患者性别'))->as(function ($w){
            return $w==1?'男':'女';
        });
        $show->field('orderPatient.birthday', __('生日'))->as(function ($w){
            return birthday_to_age($w);
        });
        $show->field('orderVaccine.id', __('疫苗'))->as(function ($w){
            $vaccine_name = OrderVaccine::where('order_id',$this->id)->pluck('vaccine_name')->toArray();
            if(empty($vaccine_name) ) return '';
            return implode(',',$vaccine_name);
        });
        $show->field('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款'])->label('info');

        $show->field('order_status', __('订单状态'))->using(Order::getStatus())->label('info');
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

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

        $form->number('order_id', __('Order id'));
        $form->number('order_patient_id', __('Order patient id'));
        $form->number('vaccine_id', __('Vaccine id'));
        $form->switch('vaccine_type', __('Vaccine type'));
        $form->number('vaccine_price', __('Vaccine price'));
        $form->text('vaccine_name', __('Vaccine name'));
        $form->text('vaccine_remark', __('Vaccine remark'));
        $form->text('vaccine_supplier', __('Vaccine supplier'));

        return $form;
    }
}
