<?php

namespace App\Community\Controllers;

use App\Models\Docter;
use App\Models\DocterOrganization;
use App\Models\Nurse;
use App\Models\Organization;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class NurseController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '儿保项目';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Nurse());
        $grid->disableCreateButton(false);
        $org_id = Admin::user()->org_id;
        if($org_id){
            $grid->model()->where(['org_id'=>$org_id]);
        }
        $grid->column('id', __('ID'));
        $grid->column('name', __('项目名称'))->editable();
        $grid->column('used', __('已使用'))->display(function ($w){
            return $w??0;
        });
        $grid->column('type','类型')->display(function ($t){
           return Nurse::getNursetype()[$t];
        });
        $grid->column('today', __('今日预约'));
        $grid->column('docters','服务医生')->display(function ($ids){
           $docters = Docter::whereIn('id',$ids)->pluck('name')->toArray();
           if(count($docters) > 3) return $docters[0].','.$docters[1].','.$docters[2].'...';
           return implode(',',$docters);
        });
        $grid->column('price', __('价格'))->display(function ($v){
            return round($v/100,4).'元';
        })->editable();
        $grid->column('remark', __('备注'))->editable('textarea');
        $grid->actions(function ($actions){
            $actions->disableView();
        });
        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('price', __('Price'));
        $show->field('remark', __('Remark'));
        $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 Nurse());
        $user = Admin::user();
        $org_id = $user->org_id;
        $form->editing(function ($f){
            $f->model()->price /= 100;
        });
        $form->text('name', __('项目名称'));
        $form->select('type','类型')->options(Nurse::getNursetype())->when(1,function (Form $form){
            $form->hidden('price', __('价格'))->default(0);
        })->when(2,function (Form $form){
            $form->text('price', __('价格'))->help('单位元');
        });
        $form->hidden('org_id')->value($org_id);

        $form->multipleSelect('docters','服务医生')->options(function () use ($org_id) {
            if(empty($org_id)){
                return  Docter::pluck('name','id');
            }
            $ids = DocterOrganization::where('organization_id',$org_id)->pluck('docter_id');
            if(empty($ids)) return [];
            return  Docter::whereIn('id',$ids)->pluck('name','id');
        });
        $form->textarea('remark', __('备注'));
        $form->saving(function ($form){
            if($form->type == 1){
                $form->price = 0;
            } else {
                $form->price = $form->price*100;
            }
        });
        $form->setWidth(6);

        return $form;
    }
}
