<?php

namespace App\Http\Controllers;

use App\Http\Resources\PermissionResource;
use App\Http\Requests\UsersRequest;
use App\Http\Resources\UserResource;
use App\Laravue\JsonResponse;
use App\Laravue\Models\Device;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Validator;

class DevicesController extends Controller
{
    //设备列表
    /*
     * 设备类型
     * type 不传默认是第一个
     * @type 设备类型id
     * */
    public function devicelist(Request $request){
        $type = $request->input('type');//设备类型id
        $pagenNum=$request->input('page')-1;//页数
        $limit = $request->input('limit');
        if($pagenNum === '' || $limit == ''){
            return $this->jsonErrorData(105,'页数或limit不能为空');
        }
        return $this->jsonSuccessData($this->whertype($type==''? '1':$type,$pagenNum,$limit));
    }
    /*
     * 删除设备
     * type 1==逻辑删除
     * type 2== 物理删除
     * */
    public function deletedecice(Request $request){
        $type =  (int)$request->input('type');
        $deviceid = (int)$request->input('deviceid');//设备id
        if ($type == '' || $deviceid == ''){
            return $this->jsonErrorData(105,'参数不能为空');
        }
        if ($type == 1){//逻辑删除
            $dete  = Device::where('id','=',$deviceid)
                ->value('delete');
            if ($dete != 1){
                $update = Device::where('id','=',$deviceid)
                    ->update(['delete'=>1]);
                return $this->jsonSuccessData($update);
            }else{
                return $this->jsonErrorData(105,'数据异常');
            }

        }else if ($type == 2){//物理删除
            $dete  = Device::where('id','=',$deviceid)
                ->value('delete');
            if ($dete != 2){
                $delete = Device::where('id','=',$deviceid)
                    ->delete();
                return $this->jsonSuccessData($delete);
            }else{
                return $this->jsonErrorData(105,'数据异常');
            }
        }
    }

    //返回逻辑删除的设备
    public function physicsdelete(Request $request){
        $deviceid = $request->input('deviceid');
        $databadevice = DB::table('device');
        if($_POST){
            $up = $databadevice
                ->where('id','=',$deviceid)
                ->update(['delete'=>2]);
            return $this->jsonSuccessData($up);
        }else{
            //返回物理删除设备列表
            $type =  (int)$request->input('type');//设备类型
            $pagenNum=$request->input('page')-1;//页数
            $limit = $request->input('limit');//条数
            if($pagenNum === '' || $limit == ''){
                return $this->jsonErrorData(105,'页数或limit不能为空');
            }
            $devicelist = $databadevice
                ->where('dtype','=',$type == '' ? '1':$type)
                ->where('delete','=','1')//设备逻辑删除状态
                ->join('device_type as dy',"device.dtype",'=','dy.tid')
                ->join('gas as g',"device.status",'=','g.id')
                ->select('dy.tname','g.gas','device.*')
                ->orderBy('device.id', 'desc')
                ->offset($pagenNum)
                ->limit($limit)
                ->get()->toArray();
            return $this->jsonSuccessData($devicelist);
        }

    }

    //添加设备
    public  function  adddevice(Request $request){

    }

    //返回设备类型和单位和介质
    public function devicetype(){
        $databadevice = DB::table('device_type')->get();
        $gas = DB::table('gas')->get();
        $danwei = DB::table('danwei')->get();
            return $this->jsonSuccessData(['devicetype'=>$databadevice,'gas'=>$gas,'danwei'=>$danwei]);
    }
    //返回设备列表
    private function whertype($type,$pagenNum,$limit) : array {
        $databadevice = DB::table('device');
        $devicelist = $databadevice
            ->where('dtype','=',$type)
            ->where('delete','=','2')//设备不是删除状态
            ->join('device_type as dy',"device.dtype",'=','dy.tid')
            ->join('gas as g',"device.status",'=','g.id')
            ->select('dy.tname','g.gas','device.*')
            ->orderBy('device.id', 'desc')
            ->offset($pagenNum)
            ->limit($limit)
            ->get()->toArray();
        return $devicelist;
    }


    /**
     * @param bool $isNew
     * @return array
     * 用户添加设备表单验证
     */
    private function getValidationRulesdevice($isNew = true)
    {
        return [
            'devicenum'=>'required|between:2,25|unique:device,devicenum',//设备编号
            'username'=>'required|unique:device,username|max:40',//设备名称
            'devicephone'=> 'required|regex:/^1[3465789]\d{9}$/|unique:device,devicephone|max:11',//联系人手机号
            'devicelinkman'=>'required|max:15',
            'dtypeid'=>'sometimes|required',
            'statusid'=>'sometimes|required',
            'devicemonadid'=>'sometimes|required'
        ];
    }

}