<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Box\CloseDoor;
use App\Admin\Actions\Box\getRule;
use App\Admin\Actions\Box\sendRule;
use App\Model\DeviceBox;
use App\Model\DeviceInfo;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class DeviceBoxController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '设备箱柜';

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

        $device_id = request('device_id');
        if(!empty($device_id)){
            $grid->model()->where('device_id',$device_id);
        }
        $grid->column('id', __('箱柜编号'));
        $grid->column('devices.device_name', __('设备名称'));
        $grid->column('name', __('箱柜名称'));
        $grid->column('volume', __('容量'));
        $grid->column('status', __('状态'))->using(['禁用','启用'])->label(['danger','success']);
        $grid->column('remark', __('备注'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->actions(function ($actions){
            $actions->add(new sendRule());
            $actions->add(new getRule());
            $actions->add(new CloseDoor());
        });

//        $grid->filter(function ($fliter){
//            $fliter->equal('device_id','设备')->select([DeviceInfo::get('device_id','device_name')->toArray()]);
//        });

        return $grid;
    }

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

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('volume', __('Volume'));
        $show->field('status', __('Status'));
        $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 DeviceBox());

        $form->display('name', __('名称'));
        $form->text('volume', __('容积'));
        $form->select('status', __('状态'))->options(DeviceBox::getStatus());
        $form->textarea('remark', __('备注'));

        return $form;
    }
}
