<?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 Order());
        $grid->actions(function ($actions){
           $actions->disableView();
           $actions->disableDelete();
           $need = OrderPack::where('order_id',$actions->row->id)->value('is_need_insurance');
//           dd($need);
           if ($actions->row->payment_status != 2 || $need == 0 )
           {
               $actions->disableEdit();
           }
        });
        $grid->disableCreateButton();
        $grid->model()->wherein('product_type',[6]);
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('订单id'))->sortable();
        $grid->column('orderPack.pack_name', __('服务包名称'));
        $grid->column('user.nickname', __('用户姓名'));
        $grid->column('orderPatient.name', __('患者姓名'));
        $grid->column('orderPatient.sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
        $grid->column('orderPatient.relationship_type', __('与投保人的关系'))
            ->using([1=>'父亲',2=>'母亲',3=>'祖父',4=>'祖母',5=>'外祖父',6=>'外祖母',7=>'叔侄',8=>'其他']);
        $grid->column('orderPatient.patient_id', __('患者id'))->hide();
        $grid->column('user.phone', __('用户电话'));
        //保单信息
        $grid->column('orderPatient.birthday', __('出生日期'));
        $grid->column('orderPatient.card_type', __('证件类型'))->using([1=>'身份证',2=>'护照']);
        $grid->column('orderPatient.card_number', __('证件号码'));
        $grid->column('orderPack.insurance_img_url', __('保单'))->link()->display(function ($url){
            $img = OrderPack::where('order_id',$this->id)->value('insurance_img_url');
            if (empty($img))
            {
                return '';
            }
            else
            {
                $herf = '../upload/'.$img;
                $url = "<a href=$herf target='_blank'>保单查看</a>";
                return $url;
            }
        });
        $grid->column('orderPatient.social_card_number', __('社保卡号'));
        $grid->column('order_status', __('订单状态'))->using([1=>'未支付',2=>'待接单',3=>'进行中',4=>'已完成',5=>'已取消']);
        $grid->column('payment_status', __('支付状态'))->using([1=>'待付款',2=>'已付款',3=>'退款中',4=>'已退款']);


        return $grid;
    }

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