<!--识别器管理-->
<template>
  <div class="app-container certification">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px" class="form-box">
      <el-form-item label="关键字" prop="name">
        <el-input
          v-model="queryParams.searchValue"
          placeholder="设备编号/所属实验室"
          clearable
          maxLength="30"
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <p class="inquire-button-one" @click="handleQuery">查询</p>
        <p class="reset-button-one" @click="resetQuery">重置</p>
      </el-form-item>
      <el-form-item style="float: right;">
        <el-col :span="1.5">
          <p class="add-button-one-120"
             @click="handleAdd"
             v-hasPermi="['laboratory:hardware7:add']"
          ><i class="el-icon-plus"></i>新增</p>
        </el-col>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="sensorList">
      <el-table-column label="设备编号" align="left" prop="hardwareNum" />
      <el-table-column label="学院" align="center" prop="deptName" width="250"/>
      <el-table-column label="位置" align="center" prop="posi" width="250" show-overflow-tooltip/>
      <el-table-column label="设备状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-hasPermi="['laboratory:hardware7:changeStatus']"
            class="switch"
            @change="switchChange(scope.row)"
            v-model="scope.row.onOff+''"
            active-value="1"
            inactive-value="0"
            active-text="开"
            inactive-text="关"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="250"/>
      <el-table-column label="创建人" align="center" prop="createBy" width="250"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <div class="button-box">
            <p class="table-min-button"
               @click="handleUpdate(scope.row)"
               style="margin-right:20px;"
               v-hasPermiAnd="['laboratory:hardware7:query','laboratory:hardware7:edit']"
            >修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['laboratory:hardware7:remove']"
            >删除</p>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      layout="total, prev, pager, next, sizes, jumper"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改传感器对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body class="recognizer add-hardware-dialog-box">
      <el-form ref="form" :model="form" :rules="rules" :inline="true">
        <el-form-item label="识别器编号" prop="hardwareNum">
          <el-input v-model="form.hardwareNum" placeholder="请输入识别器编号" style="width: 400px"/>
        </el-form-item>
        <el-form-item label="所属实验室" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择实验室" style="width: 400px">
            <el-option
              v-for="item in subjectAddOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="IP地址" >
          <el-input v-model="form.ipAddress" placeholder="请输入IP地址" style="width: 400px"/>
        </el-form-item>
        <el-form-item label="连接方式">
          <el-select v-model="form.manufacturerType" placeholder="请选择" style="width:400px;" >
            <el-option :label="item.dictLabel" :value="item.dictValue" v-for="(item,index) in materialTypeList" :key="index"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="推送频率" >
          <el-select v-model="form.sessionIndex" placeholder="请选择设备推送频率"  style="width:400px;">
            <el-option  label="最大" value="0"></el-option>
            <el-option  label="中等" value="1"></el-option>
            <el-option  label="最小" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="天线数量" >
          <el-select v-model="form.channels" placeholder="请选择信道数量" @change="antennaNumFun()"  style="width:400px;">
            <el-option  label="1" value="1"></el-option>
            <el-option  label="2" value="2"></el-option>
            <el-option  label="4" value="4"></el-option>
            <el-option  label="8" value="8"></el-option>
            <el-option  label="16" value="16"></el-option>
          </el-select>
        </el-form-item>
        <el-row  v-for="(item,index) in antennaNum" :key="index">
          <el-col :span="12">
            <el-form-item :label="(index+1)+'号天线感应距离'">
              <el-input-number v-model="uniformPower[index]" placeholder="请输入设备输出频率" style="width:140px;"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="(index+1)+'号天线检测类型'">
              <el-select v-model="detection[index]" placeholder="请选择检测类型" style="width:140px;">
                <el-option
                  v-for="item in detectionArr"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item :label="(index+1)+'号天线感应距离'" v-for="(item,index) in antennaNum" :key="index">-->
<!--          <el-input-number v-model="uniformPower[index]" placeholder="请输入设备输出频率" />-->
<!--        </el-form-item>-->


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { subjectDictList } from "@/api/laboratory/group";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  recognizerAdd,
  recognizerAnew,
  recognizerDelete,
  recognizerList,
  recognizerSwitch,
  supplierStop, RFIDTypeList, recognizerNettyFrid
} from '@/api/gasManage3_0/gasManage'

export default {
  name: "Sensor",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 传感器表格数据
      sensorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      subjectAddOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        searchValue:'',
        deviceType:'7'
      },

      // 表单参数
      form: {
        id:'',
        hardwareNum:'',
        subjectId:'',
        deviceType:'7',
        ipAddress:'',//ip地址
        channels:4,//信道
        sessionIndex:0,//设备推送频率
        uniformPower:'',//设备推送频率
        manufacturerType:'',//供应商
      },
      materialTypeList:[],
      uniformPower:[20,20,20,20],
      antennaNum:[1,2,3,4],//天线数量
      detection:['0','0','0','0',],//检测类型选择的值
      detectionArr:[],//检测类型数组
      // 表单校验
      rules: {
        name:[
          {required: true, message: '识别器名称不能为空', trigger: 'blur'}
        ],
        hardwareNum:[
          {required: true, message: '识别器编号不能为空', trigger: 'blur'}
        ],
        subjectId:[
          {required: true, message: '所属实验室不能为空', trigger: 'blur'}
        ]
      },
    };
  },
  created() {
  },
  mounted(){
    this.getList();
    this.getSubjectAddDictList();
    this.supplierTypeList();
    this.recognizerNettyFrid();
  },
  methods: {
    //根据选择的天线数量循环距离和检测类型
    antennaNumFun(){
      this.uniformPower=[];
      this.antennaNum=[];
      this.detection=[];
      for(let i=0;i<this.form.channels;i++){
        this.antennaNum.push(i)
        this.uniformPower.push(20)
        this.detection.push('0')
      }
    },
    //供应商类型
    supplierTypeList(){
      let _this=this;
      RFIDTypeList().then( response => {
        let res=response.rows;
        _this.materialTypeList=res;

      });
    },
    //状态
    switchChange(row){
      console.log(row)
      //1上架0下架
      this.$confirm('是否确定'+(row.onOff==1?'关闭':'打开')+'?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.upDownShelf(row.id)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    //上架下架
    upDownShelf(id){
      recognizerSwitch(id).then( response => {
        this.msgSuccess("修改成功");
        this.getList();
      });
    },
    /** 查询实验室字典列表 */
    getSubjectAddDictList () {
      this.subjectAddOptions = null;
      this.loading = true;
      let params = {};
      subjectDictList(params).then(response => {
        this.subjectAddOptions = response.data;
        this.loading = false;
      });
    },
    /** 查询传感器列表 */
    getList() {
      this.loading = true;
      recognizerList(this.queryParams).then(response => {
        this.sensorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //检测类型
    recognizerNettyFrid(){
      let _this=this;
      recognizerNettyFrid().then(response => {
        _this.detectionArr = []
        response.data.forEach(function(item,index) {
          for(let i in item){
            console.log(item[i])
            _this.detectionArr.push({'id':i,'name':item[i]})
          }
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        deviceType:'7',
        hardwareNum: null,
        subjectId: null,
        ipAddress: null,
        channels: '4',
        sessionIndex: '0',
        uniformPower: '',

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.searchValue = '';
      this.handleQuery();
    },


   // 20:10,30:20
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();

      this.open = true;
      this.title = "新增识别器";
      this.uniformPower=[20,20,20,20];
      this.detection=['0','0','0','0']
      this.antennaNum=[1,2,3,4]//天线数量
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if(row.onOff == 1){
        this.msgError("修改检测门前请先关闭设备");
        return
      }
      let _this=this;
      this.open = true;
      this.title = "修改识别器";
      this.form.id=row.id
      this.form.deviceType='7'
      this.form.hardwareNum=row.hardwareNum
      this.form.subjectId=row.subjectId
      this.form.ipAddress=row.ipAddress
      this.form.channels=row.channels+''
      this.form.sessionIndex=row.sessionIndex+''
      this.form.manufacturerType=row.manufacturerType+''
      //this.uniformPower=row.uniformPower.split(',')
      this.antennaNum=[];
      this.uniformPower=[];
      this.detection=[];
      //天线感应距离和检测类型编辑
      let arr=[]
      this.form.uniformPower=row.uniformPower//天线感应距离和检测类型
      arr=row.uniformPower.split(',')
      arr.forEach(function(item,index) {
          console.log(item.split(':'))
        _this.antennaNum.push(index)//天线数量
        _this.uniformPower.push(item.split(':')[0])
        _this.detection.push(item.split(':')[1])
      })

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //this.form.uniformPower=this.uniformPower.join(',')

          let uniformArr=[]
          for(let i=0;i<this.uniformPower.length;i++){
            uniformArr.push(this.uniformPower[i]+':'+this.detection[i])
          }
          this.form.uniformPower=uniformArr.join(',')
          console.log(uniformArr.join(','))

          this.form.channels=Number(this.form.channels)
          this.form.sessionIndex=Number(this.form.sessionIndex)

          if (this.form.id != null) {
            this.form.senseState = null
            recognizerAnew(this.form).then(response => {
              this.msgSuccess("修改成功,请重新开启设备");
              this.open = false;
              this.getList();
            });
          } else {
            recognizerAdd(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('确认删除传感器吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return recognizerDelete(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/sensor/export', {
        ...this.queryParams
      }, `laboratory_sensor.xlsx`)
    }
  },

};
</script>
<style scoped lang="scss">
.certification{
  display: flex!important;
  flex-direction: column;
  box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
  padding:20px!important;
  .button-box{
    width:200px;
    display: flex;
  }
  .form-box{
  }
}


</style>
<style lang="scss">
  .certification{
    .switch .el-switch__label {
      position: absolute;
      display: none;
      color: #fff !important;
    }
    .switch .el-switch__label--right {
      z-index: 1;
    }
    .switch .el-switch__label--right span{
      margin-left:10px;
    }
    .switch .el-switch__label--left {
      z-index: 1;
    }
    .switch .el-switch__label--left span{
      margin-left: 34px;
    }
    .switch .el-switch__label.is-active {
      display: block;
    }
    .switch.el-switch .el-switch__core,
    .el-switch .el-switch__label {
      width: 64px !important;
      margin: 0;
    }
  }
.add-hardware-dialog-box{
  .el-form-item__error {
    left: 21%!important;
  }
}
.recognizer{
  .el-form-item__label{
    width:130px!important;
  }
  //.el-input--suffix{
  //  width:340px!important;
  //}
  //.el-input__inner{
  //  width:340px!important;
  //}
  //.el-input--medium{
  //  width:340px!important;
  //}
  //.el-input-number--medium{
  //  width:240px!important;
  //}
}
</style>

