<template>
  <div class="app-container">
      <div class="main-right-box">
        <div class="main-search-box">
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="监控主机" name="dvs"></el-tab-pane>
            <el-tab-pane label="报警主机" name="alarmHost"></el-tab-pane>
            <el-tab-pane label="动环主机" name="fsu"></el-tab-pane>
            <el-tab-pane label="对讲主机" name="talkDevice"></el-tab-pane>
            <el-tab-pane label="门禁主机" name="doorDevice"></el-tab-pane>
          </el-tabs>
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="组织机构">
              <org-tree
                v-model="queryParams.orgId"
                @defaultOrg="getDefaultOrg"
                @checkChange="checkChange"
                @click="clickTreeNode"
                ref="orgTree"
              ></org-tree>
            </el-form-item>

            <el-form-item label="设备名称" prop="deviceName">
              <el-input clearable v-model="queryParams.deviceName" placeholder="请输入关键字" maxlength="50"
                        @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item label="设备类型" prop="deviceType" label-width="80">
              <el-select style="width: 100%;"  v-model="queryParams.deviceType" placeholder="设备类型"
                         @change="searchChangeSelectDeviceType" clearable>
                <el-option v-for="dict in deviceTypeSelectList" :key="dict.value" :label="dict.label"
                           :value="`${dict.value}`"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="网络状态" prop="netStatus" label-width="80" v-if="activeName!='fsu' ">
              <el-select style="width: 100%;"  v-model="queryParams.netStatus" placeholder="网络状态"
                         @change="searchChangeSelectNetStatus" clearable>
                <el-option v-for="dict in netStatusList" :key="dict.value" :label="dict.label"
                           :value="`${dict.value}`"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <el-row :gutter="10">
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-search"
                size="mini"
                @click="handleQuery"
              >搜索</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="primary"
                icon="el-icon-refresh"
                size="mini"
                @click="resetQuery"

              >重置</el-button
              >
            </el-col>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>
        </div>
        <el-table
          border
          height="646"
          size="small"
          v-loading="loading"
          :data="deviceInfoList"
          @selection-change="handleSelectionChange"

        >
          <!-- :header-cell-style="{backgroundColor:'#CCE8F7'}" -->
          <!--<el-table-column type="selection" width="55" align="center" />-->
          <el-table-column label="序号" align="center" min-width="50">
            <template v-slot:default="scope">
              <span v-text="getPageIndex(scope.$index)"> </span>
            </template>
          </el-table-column>
          <el-table-column label="地区" align="center" prop="firstOrgName" />
          <el-table-column label="行社" align="center" prop="secondOrgName" />
          <el-table-column label="所属机构" align="center" prop="orgName" />
          <el-table-column label="设备类型" align="center" prop="deviceTypeName"  />
          <el-table-column label="设备名称" align="center" prop="deviceName"  />
          <el-table-column
            label="网络状态"
            align="center"
            prop="netStatus"
            v-if="activeName !== 'fsu'"
          >
            <template slot-scope="scope">
              {{ getNetStatusDesc(scope.row)}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="showDetail(scope.row)"
              >详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    <detail-info-page ref="detailInfo"></detail-info-page>
  </div>
</template>

<script>
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import detailInfoPage from "./detailDialog.vue"
import { deptTreeSelect } from "@/api/system/public";
import {
  list,
} from "@/api/iot/deviceInfo";
export default {
  name: "IotDeviceInfo",
  components: { OrgTree, detailInfoPage },
  data() {
    return {
      checkList: [],
      roleOptions: [],
      defaultKeys: [],
      //是否关联下级
      checked: false,
      defaultProps: {
        children: "children",
        label: "name",
      },
      deviceTypeSelectList:[],
      //0:未知,1:在线,2:离线,3:异常
      netStatusList: [
        { label: "未知", value: "0"},
        { label: "在线", value: "1"},
        { label: "离线", value: "2"},
        { label: "异常", value: "3"},
      ],
      //监控主机下拉查询
      DvsDeviceTypeList: [
        { label: "监控主机", value: "1"},
        { label: "模拟摄像头", value: "2"},
        { label: "数字摄像头", value: "3"}
      ],
      //报警主机主机下拉查询
      AlarmHostDeviceTypeList: [
        { label: "报警主机", value: "4"},
      ],
      //动环主机下拉查询
      FsuDeviceTypeList: [
        { label: "动环主机", value: "5"},
        { label: "烟雾传感器", value: "6"},
        { label: "红外传感器", value: "7"},
        { label: "温湿度传感器", value: "8"},
        { label: "门磁传感器", value: "9"},
        { label: "燃气传感器", value: "10"},
        { label: "卷帘门传感器", value: "11"},
        { label: "水浸传感器", value: "12"},
        { label: "智能电表", value: "13"},
        { label: "空调", value: "14"},
        { label: "UPS", value: "15"},
        { label: "DO8小时控电", value: "16"},
        { label: "三相电电压传感器", value: "17"},
        { label: "盗情传感器", value: "18"},
        { label: "DO8小时采集", value: "21"},
      ],
      //动环主机下拉查询
      talkDeviceTypeList: [
        { label: "对讲主机", value: "23"},
      ],
      //动环主机下拉查询
      doorDeviceTypeList: [
        { label: "门禁主机", value: "24"},
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      //搜索tree
      deptName: null,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      inparm: 0,
      // 【请填写功能名称】表格数据
      deviceInfoList: [],
      //主机集合
      hostList: [],
      activeName: "dvs",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      dept: {
        id: null,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        orgName: null,
        deviceName: null,
        netStatus: null,
        deviceType: null,
        deviceTypeList: [],
        checkSub: true,
      },
      //修改新增中的机构树
      deptOptions: [],
      selectOrgId: null,
    };
  },
  created() {
    this.getDeptTree();
    this.queryParams.deviceType = null;
    this.queryParams.deviceTypeList = ["1","2","3"];
    this.deviceTypeSelectList = this.DvsDeviceTypeList;
  },

  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },

  methods: {
    getNetStatusDesc(row){
      if (row.deviceType === '2' || row.deviceType === '3'){
        return "-";
      }else {
        let val = row.netStatus;
        if (val === '0'){
          return "未知";
        }else if(val === '1'){
          return "在线";
        }else if(val === '2'){
          return "离线";
        }else if(val === '3'){
          return "异常";
        }
      }
    },
    onOrgSelect(node) {
      this.form.orgPath = node.path;
      this.form.orgName = node.name;
    },
    //切换tab栏刷新数据
    handleClick() {
      this.queryParams.deviceType = null;
      if ("dvs" === this.activeName){
        this.queryParams.deviceTypeList = ["1","2","3"];
        this.deviceTypeSelectList = this.DvsDeviceTypeList;
      }else if ("alarmHost" === this.activeName){
        this.queryParams.deviceTypeList = ["4"];
        this.deviceTypeSelectList = this.AlarmHostDeviceTypeList;
      }else if ("fsu" === this.activeName){
        this.queryParams.deviceTypeList = ["5","6","7","8","9","10","11","12","13","14","15","16","17","18","21"];
        this.deviceTypeSelectList = this.FsuDeviceTypeList;
      }else if ("talkDevice" === this.activeName){
        this.queryParams.deviceTypeList = ["23"];
        this.deviceTypeSelectList = this.talkDeviceTypeList;
      }else if ("doorDevice" === this.activeName){
        this.queryParams.deviceTypeList = ["24"];
        this.deviceTypeSelectList = this.doorDeviceTypeList;
      }
      this.getList();
    },

    getDefaultOrg(node) {
      this.queryParams.orgId = node.id;
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      if(data==null){
        return;
      }
      this.queryParams.orgId = data.id;
      this.getList();
    },

    getPageIndex($index) {
      //表格序号
      return (
        (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
      );
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      console.log(this.queryParams)
      console.log(this.queryParams.deviceType)
      console.log(this.queryParams.deviceTypeList)
      this.loading = true;
      list(this.queryParams).then((response) => {
        this.deviceInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
        this.defaultKeys.push(response.data[0].id);
        this.queryParams.orgId = response.data[0].id;
        this.selectOrgId = response.data[0].id;

        this.handleQuery();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.deviceType = null;
      this.resetForm("queryForm");
      this.handleClick();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.names = selection.map((item) => item.deviceName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    searchChangeSelectDeviceType(val) {
      if (val) {
        this.queryParams.deviceType = val;
      }
    },
    searchChangeSelectNetStatus(val) {
      if (val) {
        this.queryParams.netStatus = val;
      }
    },
    showDetail(row){
      this.$refs.detailInfo.show(row.id,row.deviceType);
    },
  },
};
</script>
<style lang="scss">
.el-checkbox {
  display: -webkit-box !important;
}
</style>
