<!--识别器管理-->
<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:sensor: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="left" prop="status">
        <template slot-scope="scope">
          <el-switch
            class="switch"
            @change="switchChange(scope.row)"
            v-model="scope.row.status+''"
            active-value="3"
            inactive-value="4"
            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:sensor:query','laboratory:sensor:edit']"
            >修改</p>
            <p class="table-min-button"
               @click="handleDelete(scope.row)"
               v-hasPermi="['laboratory:sensor: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-dialog-box add-hardware-dialog-box">
      <el-form ref="form" :model="form" :rules="rules" >
        <el-form-item label="识别器编号" prop="hardwareNum">
          <el-input v-model="form.hardwareNum" placeholder="请输入识别器编号" />
        </el-form-item>
        <el-form-item label="IP地址" >
          <el-input v-model="form.ipAddress" placeholder="请输入IP地址" />
        </el-form-item>
        <el-form-item label="信道数量" >
          <el-select v-model="form.channels" placeholder="请选择信道数量">
            <el-option  label="1通道" value="1"></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-form-item label="推送频率" >
          <el-select v-model="form.sessionIndex" placeholder="请选择设备推送频率">
            <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-input-number v-model="form.uniformPower" placeholder="请输入设备输出频率" />
        </el-form-item>
        <el-form-item label="所属实验室" prop="subjectId">
          <el-select v-model="form.subjectId" placeholder="请选择实验室">
            <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>
      <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
} 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: 10,
        searchValue:'',
        type:'RFID_RECOGNIZER'
      },
      // 表单参数
      form: {
        id:'',
        hardwareNum:'',
        subjectId:'',
        type:'RFID_RECOGNIZER',
        ipAddress:'',//ip地址
        channels:4,//信道
        sessionIndex:0,//设备推送频率
        uniformPower:33,//设备推送频率
      },
      // 表单校验
      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();
  },
  methods: {
    //状态
    switchChange(row){
      console.log(row)
      //1上架0下架
      this.$confirm('是否确定'+(row.status==3?'关':'开')+'?', '提示', {
        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;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        type:'RFID_RECOGNIZER',
        hardwareNum: null,
        subjectId: null,
        ipAddress: null,
        channels: '4',
        sessionIndex: '0',
        uniformPower: 33,

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

      this.open = true;
      this.title = "新增识别器";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改识别器";
      this.form.id=row.id
      this.form.type='RFID_RECOGNIZER'
      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.uniformPower=row.uniformPower


    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          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">
  .add-hardware-dialog-box{
    .el-form-item__error {
      left: 21%!important;
    }
  }
</style>

