<?php

namespace App\Admin\Controllers\ServicePacksManagment;

use App\Admin\Actions\Serve\Addmember;
use App\Models\Docter;
use App\Models\Team;
use App\Models\TeamDocter;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;

class TeamController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '团队列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Team());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('团队名称'));
        $grid->column('introduction','简介')->limit(15,'...')->width(200);
        $grid->column('member','成员查看')->display(function (){
            return '点击查看';
        })->modal('成员列表', function ($model){
            $docters = $model->docter()->get()->map(function ($docter) {
                return $docter->only(['id', 'name']);
            });
            return new Table(['医生ID', '医生名称'], $docters->toArray());
        });
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));
        $status = [
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        ];
        $grid->column('status', __('状态'))->switch($status);
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });
        return $grid;
    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Team());
        $form->text('name', __('团队名称'))->rules('required' ,['required'=>'请填写团队名称！']);
        $form->text('introduction','简介')->rules('required' ,['required'=>'请填写团队简介！']);
        $form->multipleSelect('member',' 成员')->options(function (){
            //筛选医生列表
            $member = Docter::where('is_then', 1)->pluck('name', 'id');
            return $member;
        })->rules('required');
        $status = [
            'off' => ['value' => 0, 'text' => '禁用', 'color' => 'danger'],
            'on'  => ['value' => 1, 'text' => '启用', 'color' => 'success'],
        ];
        $form->switch('status', __('状态'))->states($status);
        $form->saved(function (Form $form) {
            //保存后回调插入数据
            $id = $form->model()->id;
            $team = TeamDocter::where('team_id',$id)->get();
            if ($team != null)
            {
                TeamDocter::where('team_id',$id)->delete();
            }
            foreach ($form->member as $value)
            {
                if ($value != null)
                {
                    $arr = [
                        'docter_id' => $value,
                        'team_id' =>$id,
                    ];
                    TeamDocter::create($arr);
                }
            }
        });
        return $form;
    }

}
