<?php

namespace App\Admin\Controllers\UserManagement\BmUser;

use App\Models\Order;
use App\Models\OrderPack;
use App\Models\Patient;
use App\Admin\Actions\backstage\User\Insurance ;
use App\Models\Team;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class UserServiceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务包管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Order());
        $product_type = 6;
        $uesr_id = \request('user_id');
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->equal('user_id','用户id');
            $filter->equal('product_type','订单类型');
        });
        $grid->actions(function ($actions) {

            // 去掉删除
            $actions->disableDelete();

            // 去掉编辑
            $actions->disableEdit();
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('orderPack.id', __('Id'));
        $grid->column('orderPack.order_id', __('订单id'));
        $grid->column('patient_id', __('患者姓名'))->display(function ($id){
            return Patient::where('id',$id)->value('name');
        });
        $grid->column('orderPack.service_pack_id', __('服务包id'));
        $grid->column('orderPack.pack_name', __('服务包名称'));
        $grid->column('orderPack.pack_intro', __('服务包简介'));
        $grid->column('orderPack.pack_price', __('服务包价格'))->display(function ($price){
            return $price/100;
        });
        $grid->column('orderPack.team_id', __('医生团队'))->display(function ($team)
        {
            $team = json_decode($team);
            if (empty($team))
            {
                return '无';
            }else
            {
                $team1 =[];
//                if (is_array($team))
//                {
//                    foreach ($team as $value)
//                    {
//                        $value = (int)$value;
//                        array_push($team1,$value);
//                    }
                    $res = Team::whereIn('id',$team)->pluck('name')->toArray();
                    return $res;
//                }
            }
        })->label('info');

        $grid->column('orderPack.effective_days', __('有效天数'));
        $grid->column('orderPack.start_time', __('开始时间'))->display(function ($start_time){
            return date("Y-m-d H:i",$start_time);
        });
        $grid->column('orderPack.end_time', __('结束时间'))->display(function ($end_time){
            return date("Y-m-d H:i",$end_time);
        });
        $grid->column('orderPack.created_at', __('创建时间'));
        $grid->column('orderPack.updated_at', __('更新时间'));
        return $grid;
    }

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

        $show->field('orderPack.id', __('Id'));
        $show->field('orderPack.order_id', __('订单ID'));
        $show->field('orderPack.service_pack_id', __('服务包ID'));
        $show->field('orderPack.pack_name', __('服务包名称'));
        $show->field('orderPack.pack_intro', __('服务包简介'));
        $show->field('orderPack.pack_price', __('服务包价格'))->as(function ($price){
            return $price/100;
        });
        $show->field('orderPack.insurance_policy', __('保单名称'));
        $show->field('orderPack.insurance_img_url', __('保单图片链接'));
        $show->field('orderPack.team_id', __('医生团队ID'));
        $show->field('orderPack.phone_minutes', __('	电话分钟数'));
        $show->field('orderPack.chat_num', __('图文咨询次数'));
        $show->field('orderPack.appoint_num', __('门诊预约次数'));
        $show->field('orderPack.vaccine_limit_amount', __('疫苗总金额限制'))->as(function ($price){
            return $price/100;
        });
        $show->field('orderPack.nurses_limit_amount', __('儿保总金额限制'))->as(function ($price){
            return $price/100;
        });
        $show->field('orderPack.effective_days', __('	有效天数'));
        $show->field('orderPack.start_time', __('开始时间'))->as(function ($start_time){
            return date("Y-m-d H:i",$start_time);
        });
        $show->field('orderPack.end_time', __('结束时间'))->as(function ($end_time){
            return date("Y-m-d H:i",$end_time);
        });
        $show->field('orderPack.is_security', __('宝贝是否有医保'))
            ->using([0=>'否',1=>'是']);
        $show->field('orderPack.guardian_name', __('监护人姓名'));
        $show->field('orderPack.relationship_type', __('与宝贝的关系'))
            ->using([1=>'父亲',2=>'母亲',3=>'祖父',4=>'祖母',5=>'外祖父',6=>'外祖母',7=>'叔侄']);
        $show->field('orderPack.created_at', __('创建时间'));
        $show->field('orderPack.updated_at', __('更新时间'));

        return $show;
    }

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