<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Community\Docter\AddAccount;
use App\Admin\Actions\Community\Docter\EditAccount;
use App\Community\Actions\Docter\ChangeType;
use App\Models\CdmsUsers;
use App\Models\Docter;
use App\Models\DocterOrganization;
use App\Models\Office;
use App\Models\Qualification;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DocterOrgController 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->disableCreateButton(false);
        $is_manager = Admin::user()->isRole('manager');
        $grid->model()->orderByDesc('id');
        $grid->column('id', __('Id'));
        $grid->column('docter.name', __('成员名称'));
        if($is_manager){
            $grid->model()->where(['organization_id'=>Admin::user()->org_id]);
        } else {
            $grid->column('organization.name','所属机构')->display(function ($org_name){
                return $org_name ?? '总后台';
            });
        }
        $grid->column('docter.office_id', __('所属部门'))->display(function ($w){
            return Office::where(['id'=>$w])->value('name');
        });
        $grid->column('docter.qualification_id', __('认证资质'))->display(function ($w){
            return Qualification::where(['id'=>$w])->value('name');
        });
        $grid->column('docter.sex', __('性别'))->display(function ($w){
            return $w==1?'男':'女';
        });;
        $grid->column('docter.phone', __('联系方式'));
//        $grid->column('docter.email', __('邮箱'));
        $grid->column('docter.card_id', __('身份证号'));
        $grid->column('docter.type', __('成员身份'))->display(function ($w){
            if(empty($w)) return '未知';
            $type = DocterOrganization::$_doct_type;
            return $type[$w];
        })->label('info');


        $grid->actions(function ($actions){
            $actions->disableDelete();
            $actions->disableView(false);
            $actions->disableEdit();
            $actions->add(new ChangeType());
            $docter_id = $actions->row->docter_id;
            $org_id = $actions->row->organization_id;
            $have_user = CdmsUsers::where(['org_id'=>$org_id,'docter_id'=>$docter_id])->first();
            if($have_user){
                $actions->add(new EditAccount());
            } else {
                $actions->add(new AddAccount());
            }
        });
        return $grid;
    }

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

        $show->field('docter.name', __('名称'));
        $show->field('docter.phone', __('联系方式 '));
        $show->field('organization.name', __('机构'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));
        $show->panel()
            ->tools(function ($tools) {
                $tools->disableEdit(true);
                $tools->disableDelete(true);
            });
        return $show;
    }

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

        $form->display('docter.name', __('姓名'))->rules('required',['required'=>'名称必须']);
        $form->display('docter.sex', __('姓名'));
        $form->display('docter.phone', __('电话'));
//        $form->display('docter.email', __('邮箱'));
        $form->display('docter.card_id', __('身份证号'));
        $form->select('docter.office_id', __('所属部门'))->options(function (){
            $org_id = Admin::user()->org_id;
            return Office::where(['org_id'=>$org_id])->pluck('name','id');
        });
        $form->select('docter.tyep', __('类型'))->options(DocterOrganization::$_doct_type);
        $form->select('docter.qualification_id', __('资质'))->options(function (){
            return Qualification::pluck('name','id');
        });

        $form->saved(function ($form){
            return redirect('/docters');
        });

        return $form;
    }
}
