<?php

namespace App\Admin\Controllers\UserManagement\DocterManagement;

use App\Admin\Actions\backstage\Pass;
use App\Admin\Actions\backstage\Refuse;
use App\Admin\Actions\backstage\Revoke;
use App\Models\Docter;
use App\Models\DocterOrganization;
use App\Models\Office;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ApproveController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '认证列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new DocterOrganization());
        $grid->filter(function($filter){
            $filter->between('authentication_time', '签约时间')->datetime();
            $filter->between('authentication_end_time', '签约到期时间')->datetime();
        });

        $grid ->model()->where('state','>',0)->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('docter.id', __('医生ID'));
        $grid->column('docter.name', __('医生姓名'));
        $grid->column('docter.avatar', __('医生头像'))->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('docter.sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
        $grid->column('organization.name', __('机构'));
        $grid->column('office.name', __('科室'));
        $grid->column('qualification.name', __('医生资质'));
        $grid->column('remarks', __('备注'));
        $grid->column('docter.practice','医生照片和执业证书')->display(function ($img) {
            $arr = explode(',',$img);
            return $arr;
        })->lightbox(['width' =>'', 'height' => 30]);

        $grid->column('docter.card_photo','	身份证正反面')->display(function ($img) {
            $arr = explode(',',$img);
            return $arr;
        })->lightbox(['width' =>'', 'height' => 30]);

        $grid->column('docter.is_quail','照片和资格证书')->display(function ($img) {
            $arr = explode(',',$img);
            return $arr;
        })->lightbox(['width' =>'', 'height' => 30]);

        $grid->column('state', __('认证状态'))->using([1=>'已认证',2=>'审核驳回',3=>'待审核'])->label('info');
        $grid->column('authentication_time', __('签约时间'))->display(function ($time){
            if ($time == 0){
                return '';
            }else
            {
                return $time;
            }
        });
        $grid->column('authentication_end_time', __('签约到期时间'))->display(function ($time){
            if ($time == 0){
                return '';
            }else
            {
                return $time;
            }
        });
        //禁用创建按钮
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
            $actions->disableEdit();
            // 去掉查看
            $actions->disableView();
            //待审核状态下 给通过和驳回
            if ($actions->row->state == 3){
                //通过申请
                $actions->add(new Pass());
                //驳回申请
                $actions->add(new Refuse());
            }
            //已认证状态下 给撤销
            if ($actions->row->state == 1){
                $actions->add(new Revoke());
            }
            //驳回状态下 不给任何操作
            if ($actions->row->state == 2){

            }

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

        $show = new Show(DocterOrganization::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('docter.id', __('医生ID'));
        $show->field('docter.name', __('名字'));
        $show->field('docter.sex', __('性别'))->using([0=>'未知',1=>'男',2=>'女']);
        $show->field('docter.card_id', __('身份证'));
        $show->field('organization.name', __('机构'));
        $show->field('office.name', __('科室'));
        $show->field('qualification.name', __('医生资质'));
//        $show->field('docter.card_photo', __('身份证正反面'))->as(function ($id){
////            dd($id);
//            $imgs = explode(',',$id);
//            $html = '';
//            foreach ($imgs as $val){
//                $html .='img src="'.$val.'"';
//            }
//            return "<{$html}>";
////            return json_decode($id,true);
//        });
        $show->field('docter.card_photo', __('身份证正反面'))->image();
        return $show;
    }

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

        $form->number('docter_id', __('Docter id'));
        $form->number('organization_id', __('Organization id'));
        $form->number('offices_id', __('Offices id'));
        $form->number('qualifications_id', __('Qualifications id'));
        $form->number('state', __('State'));

        return $form;
    }
}
