<?php

namespace App\Admin\Controllers\OrdersManagement;

use App\Models\Order;
use App\Models\OrderPack;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class InsuranceListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '投保信息';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new OrderPack());
        $grid->model()->where(function ($query){
            $query->whereHas('servicepacks',function ($query){
                $query->where('is_need_insure','=',1);
            });
        });
        $grid->actions(function ($actions){
           $actions->disableView();
           $actions->disableDelete();
        });
        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('pack_name','服务包名称');
            $filter->like('orderpatients.name','患者姓名');
            $filter->equal('users.phone','用户电话');
            $filter->equal('is_need_insurance','是否包含保单')->select([0=>'否',1=>'是']);
        });
        $grid->disableCreateButton();
        //实现只有服务包中包含保单的才显示进来

        $grid->model()->orderBy('id','desc');
        $grid->column('order_id', __('订单id'))->sortable();
        $grid->column('pack_name', __('服务包名称'));
        $grid->column('orderpatients.name', __('患者姓名'));
        $grid->column('orderpatients.guardian_name', __('监护人姓名'));
        $grid->column('orderpatients.sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
        $grid->column('orderpatients.relationship_type', __('与投保人的关系'))
            ->using([1=>'父亲',2=>'母亲',3=>'祖父',4=>'祖母',5=>'外祖父',6=>'外祖母',7=>'叔侄',8=>'其他']);
        $grid->column('orderpatients.id', __('患者id'))->hide();
        $grid->column('users.phone', __('用户电话'));
//        //保单信息
        $grid->column('orderpatients.birthday', __('出生日期'));
        $grid->column('orderpatients.card_type', __('证件类型'))->using([1=>'身份证',2=>'护照']);
        $grid->column('orderpatients.card_number', __('证件号码'));
        $grid->column('is_need_insurance', __('是否需要保单'))->using([0=>'否',1=>'是']);
        $grid->column('insurance_img_url', __('保单'))->link()->display(function (){
//            dd($url);
            $img = OrderPack::where('order_id',$this->order_id)->value('insurance_img_url');
//            dd($img);
            if (empty($img))
            {
                return '';
            }
            else
            {
                $herf = '../upload/'.$img;
                $url = "<a href=$herf target='_blank'>保单查看</a>";
                return $url;
            }
        });
        $grid->column('order.order_status', __('订单状态'))->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消']);
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new OrderPack());
        $form->file('insurance_img_url','保单文件')->help('请上传pdf文件')->rules('required|mimes:pdf');
        return $form;
    }
}
