<?php

namespace App\Admin\Controllers\ServicePacksManagment;

use App\Models\Nurse;
use App\Models\ServicePack;
use App\Models\Team;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ServicePacksController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务包列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
        protected function grid()
    {
        $grid = new Grid(new ServicePack());
        $grid->actions(function ($actions){
           $actions->disableView();
        });
        $grid->model()->orderBy('id','desc');
        $grid->column('id', __('Id'));
        $grid->column('name', __('服务包名称'));
        $grid->column('intro', __('简介'));
        $grid->column('desc', __('详细内容'))->limit(100);
        $grid->column('label','服务类型')->display(function (){
            $tag = [1 => '图文',2 => '电话',3 => '门诊',4 => '计免',5 => '儿保'];
            $datas = $this -> label;

            if($datas){
                for ($i=0;$i<count($datas);$i++){
                    $str = $tag[$datas[$i]];
                    $label[$i]= $str;
                }
                return $label;
                    }
            return '无';
        })->label('info');
        $grid->column('price', __('价格'))->display(function ($money){
            return $money/100;
        });
        $grid->column('team_id', __('团队名称'))->display(function (){
            $modal = $this->team_id;
            if ($modal){
                for ($i=0; $i < count((array)$modal); $i++){
                    $id = $modal[$i];
                    $name[$i] = Team::where('id',$id)->value('name');
                }
                return $name;
            }
            return '通用';
        })->label('success');

        $grid->column('image','图片')->lightbox(['width' =>'', 'height' => 30]);
        $grid->column('phone_minutes', __('电话次数（分钟）'));
        $grid->column('chat_num', __('图文次数'));
        $grid->column('appoint_num', __('门诊次数'));
        $grid->column('vaccine_limit_amount', __('计免次数'));
        $grid->column('nurses_limit_amount', __('儿保次数'));
        $grid->column('effective_days', __('服务时长'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

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

        $form->text('name', __('服务包名称'))->rules('required' ,['required'=>'请填写名称！']);
        $form->image('image','图片')->rules('required' ,['required'=>'请选择图片！']);
        $form->text('intro', __('简介'))->rules('required' ,['required'=>'请填写简介！']);;
        $form->editor('desc', __('详情内容'))->rules('required' ,['required'=>'请填写内容！']);;
        $form->number('price', __('价钱'))->default(0);
        $form->radio('has_team','是否包含团队')->options([
           0=>'否',
           1=>'是'
        ])->default(1)->when(0,function (Form $form){
            $form->checkbox('label','服务类型')
                ->options([
                    1 => '图文',
                    2 => '电话',
                    3 => '门诊',
                    4 => '计免',
                    5 => '儿保'
                ])->when('has',1,function (Form $form){
                    $form->number('chat_num', __('图文次数'))->default(0);
                })->when('has',2,function (Form $form){
                    $form->number('phone_minutes', __('电话分钟数'))->default(0);
                })->when('has',3,function (Form $form){
                    $form->number('appoint_num', __('门诊次数'))->default(0);
                })->when('has',4,function (Form $form){
                    $form->number('vaccine_limit_amount', __('计免次数'))->default(0);
                })->when('has',5,function (Form $form){
                    //暂未完成，这里展示所有的自费的儿保项目
                    $form->select('product', __('儿保项目'))->options(Nurse::where('type',2)->pluck('name','id'));
                })->rules('required',['required'=>'请选择服务类型']);
        })->when(1,function (Form $form)
        {
            $form->multipleSelect('team_id', __('团队选择'))->options(Team::all()->pluck('name','id'));
            $form->checkbox('label','服务类型')
                ->options([
                    1 => '图文',
                    2 => '电话',
                    3 => '门诊',
                    4 => '计免',
                    5 => '儿保'
                ])->when('has',1,function (Form $form){
                    $form->number('chat_num', __('图文次数'))->default(0);
                })->when('has',2,function (Form $form){
                    $form->number('phone_minutes', __('电话分钟数'))->default(0);
                })->when('has',3,function (Form $form){
                    $form->number('appoint_num', __('门诊次数'))->default(0);
                })->when('has',4,function (Form $form){
                    $form->number('vaccine_limit_amount', __('计免次数'))->default(0);
                })->when('has',5,function (Form $form){
                    $form->select('product', __('儿保项目'))->options(Nurse::where('type',$form->team_id)->pluck('name','id'));
                })->rules('required',['required'=>'请选择服务类型']);

        });
        $form->ignore('has_team');
        $form->text('effective_days', __('服务时长（天）'))->default(0);
        $form->saving(function (Form $form){
            $form->price = $form->price*100;
//            dd($form->price);
        });
        return $form;
    }
}
