<?php

namespace App\Admin\Controllers;

use App\Model\DeviceBox;
use App\Model\DeviceInfo;
use App\Model\LockInfo;
use App\Server\DeviceServer;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class LockController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '远程所锁';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new LockInfo());
        $rule = '{
            "box1": [
                {"open":"16:10", "close":"16:11", "type":1, "start":"2020-09-01 10:00:00","value":" " },
                {"open":"16:12", "close":"16:13", "type":2, "start":"2020-09-01 10:00:00","value":" " },
                {"open":"16:14", "close":"16:15", "type":3, "start":"2020-09-01 10:00:00","value":["5", "7"] },
                {"open":"16:16", "close":"16:17", "type":4, "start":"2020-09-01 10:00:00","value":["25","26"]},
                {"open":"16:18", "close":"16:19", "type":5, "start":"2020-09-01 10:00:00","value":["09-25","09-26"]}
                ],

            "box2": [
                {"open":"16:10", "close":"16:11", "type":1, "start":"2020-09-01 10:00:00","value":" " },
                {"open":"16:12", "close":"16:13", "type":2, "start":"2020-09-01 10:00:00","value":" " },
                {"open":"16:14", "close":"16:15", "type":3, "start":"2020-09-01 10:00:00","value":["5", "7"] },
                {"open":"16:16", "close":"16:17", "type":4, "start":"2020-09-01 10:00:00","value":["25","26"]},
                {"open":"16:18", "close":"16:19", "type":5, "start":"2020-09-01 10:00:00","value":["09-25","09-26"]}
                ],
            "query":6
        }';
//        $rule = '{"cmd":"stop_test"}';
//        $rule = '{"get":"box4"}';
//        $res = (new DeviceServer())->doorOperation('rozl8eri3NwjyMrG6gYT000000 ', 0);
//        $res = (new DeviceServer())->sendMsg(868626044260472 ,$rule);
//        $res = (new DeviceServer())->sendMsg(868626044260522  ,$rule);
//        $res = (new DeviceServer())->getProperty('rozl8eri3NwjyMrG6gYT000000 ');
//        $res = (new DeviceServer())->getDeviceStatus('rozl8eri3NwjyMrG6gYT000000 ');//设备状态
//        $res = (new DeviceServer())->switchDevice('rozl8eri3NwjyMrG6gYT000000 ',1);//启用禁用
//        dd($res);
        $grid->disableCreateButton(false);
        $device_id = request('device_id');
        $box_id = request('box_id');
        if(!empty($device_id)){
            $box = DeviceBox::where(['device_id'=>$device_id])->pluck('id');
            $grid->model()->whereIn('box_id',$box);
        }
        if(!empty($box_id)){
            $grid->model()->where('box_id',$box_id);
        }
        $grid->column('boxs.device_id','设备')->display(function ($w){
           return DeviceInfo::where(['id'=>$w])->value('device_name');
        });
        $grid->column('boxs.name', __('锁位名称'));
        $grid->column('open_time', __('开门时间'));
        $grid->column('close_time', __('关门时间'));
        $grid->column('start_time', __('启动时间'));
        $grid->column('type', __('类型'))->using(LockInfo::get_type());
        $grid->column('value','时间')->display(function ($is){
            if($this->type !== 3){
               return LockInfo::get_type()[$this->type];
            } else {
               $str = '';
               foreach ($is as $i){
                   $str .=LockInfo::week_day()[$i].',';
               }
               return $str;
            }
        });
        $states= [
            'off'=>['text'=>'禁用','value'=>0,'color'=>'danger'],
            'on'=>['text'=>'启用','value'=>1,'color'=>'success']
        ];
        $grid->column('status', __('状态'))->switch($states);
        $grid->filter(function ($fliter){
            $fliter->where(function($query){
                $query->whereHas('boxs',function($query){
                   $query->where('name','like',"%{$this->input}%");
                });
            },'盒子名称');
            $fliter->equal('status','状态')->select(['禁用','启用']);
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(LockInfo::findOrFail($id));
        $show->field('id', __('Id'));
        $show->field('device_id', __('Device id'));
        $show->field('lock_key', __('Lock key'));
        $show->field('lock_name', __('Lock name'));
        $show->field('open_time', __('Open time'));
        $show->field('close_time', __('Close time'));
        $show->field('start_time', __('Start time'));
        $show->field('rules', __('Rules'));
        $show->field('status', __('Status'));
        $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 LockInfo());


        if($form->isEditing()){
            $form->display('boxs.devices.device_name', __('设备'));
            $form->select('box_id', __('锁位'))->options(function (){
                return  DeviceBox::where('device_id','=',$this->boxs->device_id)->pluck('name','id')->toArray();
            });
        } else {
            $form->select('device_id', __('设备'))->options(function (){
                return DeviceInfo::pluck('device_name','id')->toArray();
            })->load('box_id','/admin/get_box')->rules('required',['required'=>'设备必须选择']);
            $form->select('box_id', __('锁位'))->options(function (){
                return  DeviceBox::where('device_id','=',$this->device_id)->pluck('name','id')->toArray();
            })->rules('required',['required'=>'请选择盒子']);
        }
        $form->datetime('open_time', __('开门时间'))->format("HH:mm")->rules('required',['required'=>'开门时间必须']);
        $form->datetime('close_time', __('关闭时间'))->format("HH:mm")->rules('required',['required'=>'关门时间必须']);
        $form->datetime('start_time', __('开启时间'))->rules('required',['required'=>'开启时间必须']);
        $form->select('type', __('类型'))->options([
            '1'=>'不重复',
            '2'=>'每天',
            '3'=>'每周',
        ])->when(3,function (Form $form){
            $form->multipleSelect('value','日期')->options(LockInfo::week_day());
        })->rules('required',['required'=>'请选择类型']);

        $states= [
            'off'=>['text'=>'禁用','value'=>0,'color'=>'danger'],
            'on'=>['text'=>'启用','value'=>1,'color'=>'success']
        ];
        $form->ignore(['device_id','boxs.id']);
        $form->switch('status', __('状态'))->states($states)->default(1);

        return $form;
    }
}
