<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Community\Docter\Failed;
use App\Admin\Actions\Community\Docter\Pass;
use App\Models\DocterOrganization;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
class DocterVerifyController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '认证列表';

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

        $grid = new Grid(new DocterOrganization());

        $user = Admin::user();
        $is_admin = Admin::user()->inRoles(['administrator','devloper']);
        if(!$is_admin){
            $org_id = $user->org_id;
            $grid->model()->where(['organization_id'=>$org_id]);
        }
        $grid->model()->orderByDesc('id');

        $grid->column('id', __('Id'));
        $grid->column('docter.name', __('医生'));
        $grid->column('docter.sex', __('性别'))->using(['未知','男','女']);
        $grid->column('docter.card_id', __('身份证号'));
        $grid->column('office.name', __('认证科室'));
        $grid->column('qualification.name', __('认证资质'));
        $grid->column('state', __('状态'))->using(['待审核','已认证','审核驳回','审核中'])->label(['info','success','danger','info']);

        $grid->actions(function($actions){
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
            if($actions->row->state == 0){
                $actions->add(new Pass());
                $actions->add(new Failed());
            }
        });

        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->like('docter.name','姓名');
            $filter->equal('docter.sex','性别')->select(['未知','男','女']);
            $filter->equal('state','状态')->select(['待审核','已认证','认证失败']);
        });

        return $grid;
    }

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

        $show->field('docter_id', __('Docter id'));
        $show->field('organization_id', __('Organization id'));
        $show->field('offices_id', __('Offices id'));
        $show->field('qualifications_id', __('Qualifications id'));
        $show->field('state', __('State'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

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

        $form->display('docters.name', __('医生'));
        $form->display('office.name', __('科室名称'));
        $form->display('qualification.name', __('资质'));
        $form->select('state', __('状态'))->options(['待审核','已通过','已拒绝']);

        return $form;
    }
}
