<?php

namespace App\Admin\Controllers;

use App\Model\DeviceType;
use App\Model\DeviceTypeBox;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DeviceTypeBoxController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '设备类型箱体';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DeviceTypeBox());

        $grid->disableCreateButton(false);
        $grid->column('id', __('编号'));
        $grid->column('type', __('类型'))->display(function ($w){
            return DeviceType::where(['id'=>$w])->value('name');
        });
        $grid->column('name', __('名称'));
        $grid->column('volume', __('容积'));
        $grid->column('status', __('状态'))->using([0=>'禁用',1=>'启用'])->label(['danger','success']);
        $grid->column('remark', __('备注'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时时间'));
        $grid->actions(function($actions){
            $actions->disableDelete(false);
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(DeviceTypeBox::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('type', __('类型'))->as(function ($w){
            return DeviceType::where(['id'=>$w])->value('name');
        });
        $show->field('name', __('名称'));
        $show->field('volume', __('容积'));
        $show->field('status', __('状态'))->using([0=>'禁用',1=>'启用']);
        $show->field('remark', __('备注'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

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

        $form->select('type', __('类型'))->options(DeviceType::getType())->rules('required',['请选择类型']);
        $form->text('name', __('名称'))->rules('required',['required'=>'请输入箱子名称']);
        $form->text('volume', __('容积'))->help('单位L')->rules('required',['required'=>'请输入容积']);
        $states = [
            'off'=>['text'=>'禁用','value'=>0,'color'=>'danger'],
            'on'=>['text'=>'启用','value'=>1,'color'=>'success'],
        ];
        $form->switch('status', __('状态'))->states($states)->default(1);
        $form->textarea('remark', __('备注'));

        return $form;
    }
}
