<!--应急管理-疏散硬件-->
<template>
  <div class="app-container sparseHardware">
    <div class="sparseHardware-page" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">

        <el-form-item label="设备类型" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择设备类型" clearable>
            <el-option v-for="(item,index) in typeList" :key="index" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋" prop="buildId">
          <el-select v-model="queryParams.buildId" placeholder="请选择楼栋" clearable @change="deptChange">
            <el-option v-for="(item,index) in buildsDataList" :key="index" :label="item.deptName" :value="item.deptId" />
          </el-select>
        </el-form-item>
        <!--<el-form-item label="实验室" prop="type">-->
          <!--<el-select v-model="queryParams.type" placeholder="请选择实验室" clearable>-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="设备状态" prop="operate">
          <el-select v-model="queryParams.operate" placeholder="请选择设备状态" clearable>
            <el-option v-for="(item,index) in operate" :key="index" :label="item.name" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item style="float: right;">
          <el-col :span="1.5" style="margin-right:20px;">
            <!--@click="handleAdd"-->
            <p class="add-button-one-120"
               @click="pageTypeClick(2)"
               v-hasPermi="['laboratory:sparseHardware:add']"
            ><i class="el-icon-plus"></i>新增疏散设备</p>
          </el-col>
        </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>
      <el-table v-loading="loading" border :data="sparseHardwareList" >
        <el-table-column label="设备名称" align="center" prop="name" />
        <el-table-column label="设备类型" align="center" prop="type">
          <template slot-scope="scope">
            {{scope.row.type == 1 ? '摄像头' : (scope.row.type == 2 ? '喇叭' : '')}}
          </template>
        </el-table-column>
        <el-table-column label="设备编号" align="center" prop="hardwareNum" />
        <el-table-column label="楼栋" align="center" prop="floorName" >
          <template slot-scope="scope">{{scope.row.floorName?scope.row.floorName:'--'}}</template>
        </el-table-column>
        <el-table-column label="楼层" align="center" prop="buildName" >
          <template slot-scope="scope">{{scope.row.buildName?scope.row.buildName:'--'}}</template>
        </el-table-column>
        <el-table-column label="实验室" align="center" prop="subjectName">
          <template slot-scope="scope">{{scope.row.subjectName?scope.row.subjectName:'--'}}</template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="operate">
          <template slot-scope="scope">
            {{scope.row.operate == 0 ? '离线' : (scope.row.operate == 1 ? '异常' : (scope.row.operate == 2 ? '在线' : (scope.row.operate == 3 ? '开启' : (scope.row.operate == 4 ? '关闭' : ''))))}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="button-box" style="display: flex">
              <p class="table-min-button"
                 @click="pageTypeClick(3,scope.row)"
                 v-hasPermi="['laboratory:sparseHardware:edit']">编辑</p>
              <p class="table-min-button"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['laboratory:sparseHardware:remove']">删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <add-page v-if="pageType == 2" :propsData="propsData"></add-page>
    <!-- 添加或修改疏散硬件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编码" prop="hardwareNum">
          <el-input v-model="form.hardwareNum" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型(1 摄像头，2 喇叭)" prop="type">
          <el-select v-model="form.type" placeholder="请选择设备类型(1 摄像头，2 喇叭)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋表楼层id" prop="floorId">
          <el-input v-model="form.floorId" placeholder="请输入楼栋表楼层id" />
        </el-form-item>
        <el-form-item label="楼栋ID" prop="buildId">
          <el-input v-model="form.buildId" placeholder="请输入楼栋ID" />
        </el-form-item>
        <el-form-item label="设备状态" prop="operate">
          <el-input v-model="form.operate" placeholder="请输入设备状态" />
        </el-form-item>
        <el-form-item label="功能状态">
          <el-radio-group v-model="form.functionStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSparseHardware, getSparseHardware, delSparseHardware, addSparseHardware, updateSparseHardware,buildsList,getNoAdminSubjectListNopage } from "@/api/laboratory/sparseHardware";
import addPage from "./addPage.vue"
export default {
  name: "SparseHardware",
  components: {
    addPage,
  },
  data() {
    return {
      tableButtonType:this.hasPermiDom(['laboratory:sparseHardware:edit','laboratory:sparseHardware:remove']),
      //页面状态
      pageType:1,
      //传参数据
      propsData:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 疏散硬件表格数据
      sparseHardwareList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize:20,
        type: '',
        buildId: '',
        operate: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "设备类型(1 摄像头，2 喇叭)不能为空", trigger: "change" }
        ],
      },
      // 设备类型
      typeList:[
        { name:"摄像头",value:"1", },
        { name:"喇叭",value:"2", },
      ],
      //楼栋数据
      buildsDataList:[],
      //设备状态
      operate:[
        { name:"离线",value:"0"},{ name:"异常",value:"1"},{ name:"在线",value:"2"},{ name:"开启",value:"3"},{ name:"关闭",value:"4"},
      ]
    };
  },
  created() {
    this.getList();
    this.buildsList();
  },
  methods: {
    //页面状态切换
    pageTypeClick(type,item){
      if(this.pageType != type){
        if(type == 1){
          this.getList();
          this.pageType = type;
        }else if(type == 2){
          this.$set(this,'propsData',null)
          this.pageType = type;
        }else if(type == 3){
          console.log('item',item);
          this.$set(this,'propsData',item)
          this.pageType = 2;
        }
      }
    },
    deptChange(e){
      console.log("e",e)
      this.getSubjectDict(e);
    },
    //查询楼栋
    buildsList(){
      buildsList().then( response => {
        this.buildsDataList = response.data;
      });
    },
    //选择学院获取实验室
    getSubjectDict(id){
      console.log(id)
      let obj = {
        deptId:id,
      }
      getNoAdminSubjectListNopage(obj).then(response => {
        console.log("response",response);
      });
    },
    /** 查询疏散硬件列表 */
    getList() {
      this.loading = true;
      listSparseHardware(this.queryParams).then( response => {
        this.sparseHardwareList =  response.rows;
        this.total =  response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hardwareNum: null,
        name: null,
        type: null,
        floorId: null,
        buildId: null,
        operate: null,
        functionStatus: "0",
        deptId: null,
        deptName: null,
        createTime: null,
        userId: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.$set(this,'queryParams',{
        pageNum: 1,
        pageSize:20,
        type:"",
        floorId: '',
        operate: '',
      });
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加疏散硬件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSparseHardware(id).then( response => {
        this.form =  response.data;
        this.open = true;
        this.title = "修改疏散硬件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSparseHardware(this.form).then( response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSparseHardware(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 delSparseHardware(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('laboratory/sparseHardware/export', {
        ...this.queryParams
      }, `laboratory_sparseHardware.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .sparseHardware{
    flex:1;
    display: flex;
    flex-direction: column;
    overflow: hidden !important;
    .sparseHardware-page {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: hidden !important;
      padding:20px;
      box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      .button-box{
        display: flex;
        height:30px;
        width:190px;
        margin:0 auto;
      }
    }
  }
</style>
