<?php

namespace App\Community\Controllers;

use App\Admin\Actions\Cdmuser;
use App\Admin\Actions\UpdateCdms;
use App\Models\Area;
use App\Models\Organization;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class SettingController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '机构';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Organization());
        $org_id = \Admin::user()->org_id;
        if($org_id){
            $grid->model()->where(['id'=>$org_id]);
        }

        $grid->filter(function ($filter){
            $filter->disableIdFilter();
            $filter->column(1/2, function ($filter) {
                $filter->equal('type','机构类型')->select([
                    1=>'公立医院',
                    2=>'私立医院'
                ]);
                $filter->equal('level','机构级别')->select([
                    0=>'未定级',
                    1=>'一级',
                    2=>'二级',
                    3=>'三级'
                ]);
            });
            $filter->column(1/2, function ($filter) {
                $filter->equal('province_id','省份')->select(Area::where('level',1)->pluck('name','id'));
                $filter->equal('city_id','城市')->select(Area::where('level',2)->pluck('name','id'));
                $filter->equal('area_id','地区')->select(Area::where('level',3)->pluck('name','id'));
            });
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('type', __('机构类型'))->using([1=>'公立医院',2=>'私立医院']);
        $grid->column('level', __('机构级别'))->using([0=>'未定级',1=>'一级',2=>'二级',3=>'三级']);
        $grid->column('name', __('名称'));
        $grid->column('province_id', __('省份'))->display(function ($id){
            return Area::where('id',$id)->value('name');
        });
        $grid->column('city_id', __('城市'))->display(function ($id){
            return Area::where('id',$id)->value('name');
        });
        $grid->column('area_id', __('地区'))->display(function ($id){
            return Area::where('id',$id)->value('name');
        });
        $grid->column('address', __('详细地址'));
        $grid->column('latitude', __('经度'));
        $grid->column('longitude', __('纬度'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('cdms_id', __('Cdms id'));
        $show->field('type', __('Type'));
        $show->field('level', __('Level'));
        $show->field('name', __('Name'));
        $show->field('province_id', __('Province id'));
        $show->field('city_id', __('City id'));
        $show->field('area_id', __('Area id'));
        $show->field('address', __('Address'));
        $show->field('nurse_notice', __('Nurse notice'));
        $show->field('vaccine_notice', __('Vaccine notice'));
        $show->field('intro', __('Intro'));
        $show->field('latitude', __('Latitude'));
        $show->field('longitude', __('Longitude'));
        $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 Organization());
        $form->select('type', __('机构类型'))->options([
            1=>'公立医院',
            2=>'私立医院'
        ])->required();
        $form->select('level', __('机构级别'))->options([
            0=>'未定级',
            1=>'一级',
            2=>'二级',
            3=>'三级',
        ])->required();
        $form->text('name', __('名称'))->required();
        $form->select('province_id', __('省份'))->options(function (){
            return Area::where('level',1)->pluck('name','id');
        })->load('city_id','/cdms/api/getCity')->required();
        $form->select('city_id', __('城市'))->options(function (){
            return Area::where(['level'=>2,'parent_id'=>$this->province_id])->pluck('name','id');
        })->load('area_id','/cdms/api/getArea')->required();
        $form->select('area_id', __('地区'))->options(function (){
            return Area::where(['level'=>3,'parent_id'=>$this->city_id])->pluck('name','id');
        })->required();
        $form->text('address', __('详细地址'))->required();
        Admin::script("
        function clearstyle(){
            $('.wangEditor-txt').css('height','300px')
        };
        setTimeout(clearstyle,500);
        ");
        $form->editor('nurse_notice','门诊通告');
        $form->editor('vaccine_notice','计免通告');
        $form->latlong('latitude', 'longitude','经纬度');

        return $form;
    }
}
