<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Exception;
use think\MhtFileMaker;
use think\Request;

class Devsncfg extends Common
{
    public function index()
    {
        return $this->fetch();
    }
 

    public function get_ajax(){
        $data = Db::query("SELECT DISTINCT concat('b',bdzid) id,bmc name,0 as pId from vw_sb
UNION DISTINCT SELECT concat('j',jgid) id,jmc name,concat('b',bdzid) pId from vw_sb
UNION DISTINCT SELECT concat('z',zid) id,zmc name,concat('j',jgid) pId from vw_sb
UNION DISTINCT SELECT concat('m',id) id,name,concat('z',zid) pId from vw_sb");
        echo  json_encode($data);
    }
	
	public function ajax(){
        $id=input('id');
        $data1 = Db::table('vw_sb')->where('id',$id)->find();
        echo $data1['mtid'];
    }

    public function lst()
    {
        $sid=input('sid');
        //$list=db('i2relation')->where('eqmid',$sid)->select();
        $list=Db::table('i2relation')->where('eqmid', $sid)->paginate(5,false,['query' => request()->param()]);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add()
    {
        $sid=input('sid');
        if (request()->isPost()){
            $data = input('post.');
            $data['eqmid']=$sid;
            try {
                if (db('i2relation')->insert($data)) {
                    return $this->success('添加设备编码成功', 'lst?sid=' . $sid);
                } else {
                    return $this->error('添加设备编码失败');
                }
            }
            catch (Exception $e)
            {
                return $this->error($e->getMessage());
            }
        }
        return $this->fetch();

    }
    public function edit(){
        $sid=input('sid');
        $sensorid=input('sensorid');
        $list=db('i2relation')->find($sensorid);
        if(request()->ispost()){
            $data=[
                'eqmid'=>input('eqmid'),
                'cacid'=>input('cacid'),
                'sensorid'=>input('sensorid1'),
                'equipmentid'=>input('equipmentid'),
                'monitortype'=>input('monitortype'),
                'phase'=>input('phase'),
                'sensorindex'=>input('sensorindex'),
            ];

            $save=db('i2relation')->where("sensorid",$data["sensorid"])->update($data);
            if($save !== false){
                $this->success('修改设备编码成功！','lst?sid='.$sid);
            }else{
                $this->error('修改设备编码失败！');
            }
        }
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function del(){
        $sid=input('sid');
        $sensorid=input('sensorid');
        if(db('i2relation')->delete(input('sensorid'))){
            $this->success('删除设备编码成功！','lst?sid='.$sid);
        }else{
            $this->error('删除设备编码失败！');
        }

    }
}
