<template>
  <div class="safetyList">
    <customHeader>监控分站管理详情</customHeader>
    <div class="content">
      <a-tabs class="tab-box" v-model:activeKey="activeKey" @change="onChangeTab">
        <a-tab-pane tab="分站监测" key="device" />
        <a-tab-pane tab="监测详情" key="manageAuto" />
        <a-tab-pane tab="操作记录" key="operationRecord" />
      </a-tabs>
      <div class="box-content">
        <!-- 分站监测 -->
        <div class="now-content" v-if="activeKey == 'device'">
          <div class="left-box">
            <div class="left-title">
              <div class="title-fz">
                <span>分站：</span>
                <span>
                  [通讯正常]
                  <span class="zd-open">{{ openNum || 0 }}</span>
                </span>
                <span>
                  [通讯中断]
                  <span class="zd-close">{{ clsoeNum || 0 }}</span>
                </span>
              </div>
              <div class="title-btn" style="margin-left: 55%">
                <a-button type="primary" size="small" @click="getAllShow">显示全部</a-button>
              </div>
            </div>
            <div class="left-content">
              <div class="card-box" v-for="(item, index) in cardList" :key="index">
                <div :class="[
                  'card-item',
                  {
                    selected: selectedIndex === index,
                    'card-itemL': item.linkstatus,
                    'card-itemD': !item.linkstatus,
                  },
                ]" @click="cardClick(item, index)">
                  <div class="card-item-label">{{ item.strname }}</div>
                </div>
                <div :class="activeIndex % 4 == 3 ? 'card-modal1' : 'card-modal'" v-if="activeIndex == index && isShow">
                  <div class="modal-name">站点名称：</div>
                  <a-input v-model:value="stationName" size="small" placeholder="请输入" @blur="changeName" />
                  <div class="modal-lj">连接状态：</div>
                  <a-radio-group v-model:value="stationStatus" size="small" :options="ljList" @change="changeStatus" />
                  <a-popconfirm title="删除内容无法恢复，是否删除" ok-text="确定" cancel-text="取消" @confirm="handleDelStation"
                    @cancel="handleCancelDelStation">
                    <a-button type="primary" preIcon="ant-design:delete-outlined" size="mini"
                      class="down-btn">删除</a-button>
                  </a-popconfirm>
                </div>
              </div>
            </div>
          </div>
          <div class="right-box">
            <div class="right-title">详细信息：</div>
            <a-table size="small" :scroll="{ y: 680 }" :columns="columns" :data-source="tableData"
              :pagination="pagination" @change="pageChange">
              <template #action="{ record }">
                <a-button v-if="!record.devInfoList" type="primary" :disabled="record.linkId != '0'" size="small"
                  @click="handlerunDeviceMonitor(record, '启动')">启动</a-button>
                <a-button type="success" size="small" style="margin: 0px 10px"
                  @click="handlerunDeviceMonitor(record, '编辑')">编辑</a-button>
                <a-popconfirm title="删除内容无法恢复，是否删除" ok-text="确定" cancel-text="取消"
                  @confirm="handlerunDeviceMonitor(record, '删除')" @cancel="handleCancelDelStation">
                  <a-button v-if="!record.devInfoList" type="success" size="small"
                    style="margin-right:10px">删除</a-button>
                </a-popconfirm>
                <a-button type="primary" v-if="!record.devInfoList" size="small" @click="debugClick(record)">{{
                  record.debugTitle }}</a-button>
              </template>
              <template #bodyCell="{ column, text }">
                <template v-if="column.dataIndex === 'valueJc' && text">
                  <div v-for="item in text.split(',')" :key="item">
                    <span
                      v-if="item.substring(item.indexOf(':') + 1) && !isNaN(parseFloat(item.substring(item.indexOf(':') + 1)))"
                      style="display: inline-block; width: 42%; text-align: right; color: rgb(0, 242, 255); margin-right: 5px">{{
                        item.substring(0, item.indexOf(':') + 1) }}</span>
                    <span
                      v-if="item.substring(item.indexOf(':') + 1) && !isNaN(parseFloat(item.substring(item.indexOf(':') + 1)))"
                      style="display: inline-block; width: 52%; text-align: left; color: #fff">{{
                        item.substring(item.indexOf(':') + 1) === '1'
                          ? '正风'
                          : item.substring(item.indexOf(':') + 1) === '2'
                            ? '反风'
                            : item.substring(item.indexOf(':') + 1)
                      }}
                    </span>
                  </div>
                </template>
              </template>
            </a-table>
            <!-- 一键启动弹窗 -->
            <a-modal style="top: 300px; left: 360px" v-model:visible="visibleModal" :width="450" title="一键启动"
              @ok="handleOk" @cancel="handleCancel">
              <a-form :model="startupData" labelAlign="right" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
                <a-form-item label="安装位置">
                  <a-input v-model:value="startupData.address" placeholder="请输入" style="width: 260px" />
                </a-form-item>
              </a-form>
            </a-modal>
            <!-- 编辑弹窗 -->
            <a-modal style="top: 300px; left: 360px" v-model:visible="visibleModalEdit" :width="450" title="编辑信息"
              @ok="handleOkEdit" @cancel="handleCancelEdit">
              <a-form :model="startupDataEdit" labelAlign="right" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
                <a-form-item label="安装位置">
                  <a-input v-model:value="startupDataEdit.address" placeholder="请输入" style="width: 260px" />
                </a-form-item>
              </a-form>
            </a-modal>
            <!-- 调试弹窗 -->
            <a-modal style="top: 300px; left: 360px" v-model:visible="visibleModalDebug" :width="450" title="调试信息"
              @ok="handleOkDebug" @cancel="handleCancelDebug">
              <a-form :model="startupDataDebug" labelAlign="right" :label-col="{ span: 12 }"
                :wrapper-col="{ span: 12 }">
                <a-form-item label="风速">
                  <a-input type="number" v-model:value="startupDataDebug.speed" placeholder="请输入"
                    style="width: 260px" />
                </a-form-item>
                <a-form-item label="风向">
                  <a-select v-model:value="startupDataDebug.direction" style="width: 260px">
                    <a-select-option v-for="file in derictList" :key="file.label" :value="file.value">{{ file.label
                    }}</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item label="密码">
                  <a-input type="password" v-model:value="startupDataDebug.pwd" placeholder="请输入"
                    style="width: 260px" />
                </a-form-item>
                <a-form-item label="浮动范围">
                  <a-input type="number" v-model:value="startupDataDebug.range" placeholder="请输入"
                    style="width: 260px" />
                </a-form-item>
              </a-form>
            </a-modal>
            <!-- 结束调试弹窗 -->
            <a-modal style="top: 300px; left: 360px" v-model:visible="visibleModalDebugEnd" :width="450" title="调试信息"
              @ok="handleOkDebugEnd" @cancel="handleCancelDebugEnd">
              <a-form :model="startupDataDebugEnd" labelAlign="right" :label-col="{ span: 12 }"
                :wrapper-col="{ span: 12 }">
                <a-form-item label="密码">
                  <a-input type="password" v-model:value="startupDataDebugEnd.pwd" placeholder="请输入"
                    style="width: 260px" />
                </a-form-item>
              </a-form>
            </a-modal>
          </div>
        </div>

        <!-- 监测详情 -->
        <div class="detail-content" v-if="activeKey == 'manageAuto'">

          <a-table size="small" :scroll="{ y: 710 }" :row-key="(record) => record.stationId"
            :expandedRowKeys="expandedRowKeys" :columns="columnsDetail130" :data-source="tableData1"
            @expand="tableExpand">

            <template #bodyCell="{ column, text }"></template>
            <template #expandedRowRender="{ record }">
              <a-table :columns="deviceColumns130" :data-source="record.devInfoList" :pagination="false" maxWidth="340"
                :scroll="{ x: 'max-content', y: 200 }">
                <template #action="{ record }">
                  <a-button v-if="hasPermission('operateRecord:return')" type="primary" size="small"
                    @click="handleEdit(record)">编辑</a-button>
                </template>
              </a-table>
            </template>
          </a-table>
          <!-- 编辑弹窗 -->
          <a-modal v-model:visible="visibleModalEdit1" :width="1100" @cancel="cancenModal"
            :bodyStyle="{ height: '680px', 'overflow-y': 'auto', 'margin-bottom': '15px' }" title="编辑信息" :footer="null">
            <!-- <a-form :model="formView" labelAlign="right" :label-col="{ span: 8 }" :wrapper-col="{ span: 16 }">
              <a-form-item label="第一路风速风向：">
                <a-input v-model:value="formView.dylfsfx" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="第一路报警状态：">
                <a-input v-model:value="formView.dylbjzt" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="第一路1发2收AD值：">
                <a-input v-model:value="formView.dyl1f2sADz" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="第一路2发1收AD值：">
                <a-input v-model:value="formView.dyl2f1sADz" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="通风量：">
                <a-input v-model:value="formView.tfl" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="硬件版本：">
                <a-input v-model:value="formView.yjbb" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="软件版本：">
                <a-input v-model:value="formView.rjbb" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="在线离线标志：">
                <a-input v-model:value="formView.zxlxbz" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
              <a-form-item label="日期和时间：">
                <a-input v-model:value="formView.rqsj" placeholder="请输入" disabled
                  style="width: 260px; margin-right: 10px" />
              </a-form-item>
            </a-form> -->

            <a-form :model="formEdit" labelAlign="right" layout="inline" :label-col="{ span: 7 }"
              :wrapper-col="{ span: 17 }">
              <!-- <a-form-item label="传感器设备：">
                <a-select v-model:value="formEdit.cgq" @change="changeCgq" style="width: 260px; margin-right: 10px">
                  <a-select-option v-for="(file, index) in cgqList" :key="index" :value="file.value">{{ file.label
                  }}</a-select-option>
                </a-select>
                <a-button type="success" @click="getDeviceList">读取</a-button>
              </a-form-item> -->
              <a-form-item label="地址:">
                <a-input v-model:value="formEdit.dz" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('地址')">下发</a-button>
              </a-form-item>
              <a-form-item label="探头距离:">
                <a-input v-model:value="formEdit.ttjl" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('探头距离')">下发</a-button>
              </a-form-item>
              <a-form-item label="基线距离:">
                <a-input v-model:value="formEdit.jxjl" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('基线距离')">下发</a-button>
              </a-form-item>
              <a-form-item label="角度:">
                <a-input v-model:value="formEdit.jd" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('角度')">下发</a-button>
              </a-form-item>
              <a-form-item label="开启时长:">
                <a-input v-model:value="formEdit.kqsz" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('开启时长')">下发</a-button>
              </a-form-item>
              <a-form-item label="平滑周期:">
                <a-input v-model:value="formEdit.phzq" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('平滑周期')">下发</a-button>
              </a-form-item>
              <a-form-item label="上行运放倍数:">
                <a-input v-model:value="formEdit.sxyfbs" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('上行运放倍数')">下发</a-button>
              </a-form-item>
              <a-form-item label="下行运放倍数:">
                <a-input v-model:value="formEdit.xxyfbs" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('下行运放倍数')">下发</a-button>
              </a-form-item>
              <a-form-item label="风道截面积:">
                <a-input v-model:value="formEdit.fdjmj" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('风道截面积')">下发</a-button>
              </a-form-item>
              <a-form-item label="总体校准系数:">
                <a-input v-model:value="formEdit.ztxzxs" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('总体校准系数')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数一:">
                <a-input v-model:value="formEdit.xzxs1" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数一')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数二:">
                <a-input v-model:value="formEdit.xzxs2" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数二')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数三:">
                <a-input v-model:value="formEdit.xzxs3" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数三')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数四:">
                <a-input v-model:value="formEdit.xzxs4" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数四')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数五:">
                <a-input v-model:value="formEdit.xzxs5" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数五')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数六:">
                <a-input v-model:value="formEdit.xzxs6" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数六')">下发</a-button>
              </a-form-item>
              <a-form-item label="校准系数b:">
                <a-input v-model:value="formEdit.xzxsb" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('校准系数b')">下发</a-button>
              </a-form-item>
              <a-form-item label="系数b符号:">
                <a-input v-model:value="formEdit.xsbfh" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('系数b符号')">下发</a-button>
              </a-form-item>
              <a-form-item label="高报警阈值:">
                <a-input v-model:value="formEdit.gbjyz" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('高报警阈值')">下发</a-button>
              </a-form-item>
              <a-form-item label="低报警阈值:">
                <a-input v-model:value="formEdit.dbjyz" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('低报警阈值')">下发</a-button>
              </a-form-item>
              <a-form-item label="报警使能:">
                <a-input v-model:value="formEdit.bjsn" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('报警使能')">下发</a-button>
              </a-form-item>
              <a-form-item label="第一路485波特率:">
                <a-input v-model:value="formEdit.dyl485btl" placeholder="请输入"
                  style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('第一路485波特率')">下发</a-button>
              </a-form-item>
              <a-form-item label="复位:">
                <a-input v-model:value="formEdit.fw" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('复位')">下发</a-button>
              </a-form-item>
              <a-form-item label="浮动范围:">
                <a-input v-model:value="formEdit.range" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('浮动范围')">下发</a-button>
              </a-form-item>
              <a-form-item label="测试模式:">
                <a-select ref="select" v-model:value="formEdit.csms" placeholder="请选择..."
                  style="width: 260px; margin-right: 10px">
                  <a-select-option v-for="(item, index) in optionList" :key="index" :value="item.value">{{ item.label
                  }}</a-select-option>
                </a-select>
                <a-button class="down-btn" type="primary" @click="handleClick('测试模式')">下发</a-button>
              </a-form-item>
              <a-form-item label="测试风速:">
                <a-input v-model:value="formEdit.csfs" placeholder="请输入" style="width: 260px; margin-right: 10px" />
                <a-button class="down-btn" type="primary" @click="handleClick('测试风速')">下发</a-button>
              </a-form-item>
              <a-form-item label="测试风向:">
                <a-select ref="select" v-model:value="formEdit.csfx" placeholder="请选择..."
                  style="width: 260px; margin-right: 10px">
                  <a-select-option v-for="(item, index) in optionList1" :key="index" :value="item.value">{{ item.label
                  }}</a-select-option>
                </a-select>
                <a-button class="down-btn" type="primary" @click="handleClick('测试风向')">下发</a-button>
              </a-form-item>
            </a-form>
          </a-modal>
        </div>
        <!-- 操作记录 -->
        <div class="detail-content" v-if="activeKey == 'operationRecord'">
          <operateRecord :operationData="operationData" @getSearchRecord="getSearchRecord"></operateRecord>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick, reactive, onMounted, onUnmounted, inject } from 'vue';
import { usePermission } from '/@/hooks/web/usePermission';
import {
  subStationList,
  get130List,
  //   getList,
  getListAll,
  getEdit,
  update130DevName,
  runDeviceMonitor,
  //   update158DevName,
  updateDebugStatus,
  get130StationData,
  set130StationDevicesRead,
  set130StationData,
  get130StationDevices,
  set130StationRead,
  remove130Substation,
  // get130SetLog,
  remove130Device,
  //   get158StationData,
  //   set158StationDevicesRead,
  //   set158StationData,
  //   get158StationDevices,
  //   set158StationRead,
  //   remove158Substation,
  get158SetLog,
  //   remove158Device
} from '../safetyList.api';

import { columnsDetail130, columns, deviceColumns130, optionList, optionList1, derictList, pagination } from '../safetyList.data';

import customHeader from '/@/components/vent/customHeader.vue';
import operateRecord from './operateRecord.vue'
import { AesEncryption } from '/@/utils/cipher';
import { loginCipher } from '/@/settings/encryptionSetting';
import { message } from 'ant-design-vue';


let expandedRowKeys = reactive<any[]>([])
const { hasPermission } = usePermission();
let activeKey = ref('device');
let cardList = ref<any[]>([]);
let activeIndex = ref(null);
let isShow = ref(false);
let stationName = ref('');
let stationStatus = ref(null);
let ljList = reactive<any[]>([
  { value: 0, label: '断开' },
  { value: 1, label: '连接' },
]);
let selectedIndex = ref(0);
let openNum = ref(0);
let clsoeNum = ref(0);
let tableData = ref<any[]>([]);
let tableData1 = ref<any[]>([]);

let visibleModalEdit = ref(false);
let visibleModalEdit1 = ref(false);
let formEdit = reactive({
  id: '',
  dz: '',
  csms: '',
  ttjl: '',
  jxjl: '',
  jd: '',
  kqsz: '',
  phzq: '',
  sxyfbs: '',
  xxyfbs: '',
  fdjmj: '',
  ztxzxs: '',
  xzxs1: '',
  xzxs2: '',
  xzxs3: '',
  xzxs4: '',
  xzxs5: '',
  xzxs6: '',
  xzxsb: '',
  xsbfh: '',
  gbjyz: '',
  dbjyz: '',
  bjsn: '',
  dyl485btl: '',
  fw: '',
  range: '',
  csfs: '',
  csfx: ''
});
// let formView = reactive({
//   dylfsfx: '',
//   dylbjzt: '',
//   dyl1f2sADz: '',
//   dyl2f1sADz: '',
//   tfl: '',
//   yjbb: '',
//   rjbb: '',
//   zxlxbz: '',
//   rqsj: '',
// });

// let cgqList = reactive<any[]>([]);
let devId = ref('');
let startupDataEdit = reactive({
  address: '',
});
//一键启动弹窗
let visibleModal = ref(false);
let startupData = reactive({
  address: '',
});
let paramId = ref('');
let startupDataDebug = reactive({
  speed: '',
  direction: '',
  pwd: '',
  range: '',
});
let visibleModalDebug = ref(false);
let debugFlag = ref('');
let debugStationId = ref('');
let debugDeviceId = ref('');

let devStationId = ref('');
let stationId = ref('');

//结束调试弹窗相关信息
let visibleModalDebugEnd = ref(false);
let startupDataDebugEnd = reactive({
  pwd: '',
});
let operationData = ref<any[]>([])//操作记录列表


//定时刷新左侧分站数据
let timer: null | NodeJS.Timeout = null;
function getMonitor(flag = false) {
  timer = setTimeout(
    async () => {
      if (activeKey.value == 'device') {
        await getSubStationList();
        await getStationList1();
      } else if (activeKey.value == 'manageAuto') {
        await getStationList();
      }
      if (timer) {
        timer = null;
      }
      getMonitor();
    },
    flag ? 0 : 3000
  );
}
// let timer2: null | NodeJS.Timeout = null;
// function getMonitor2(flag = false) {
//   timer2 = setTimeout(
//     async () => {
//       await getShowReadList();
//       if (timer2) {
//         timer2 = null;
//       }
//       getMonitor2();
//     },
//     flag ? 0 : 3000
//   );
// }
let timer3: null | NodeJS.Timeout = null;
function getMonitor3(flag = false) {
  timer3 = setTimeout(
    async () => {
      await getStationListAll();
      if (timer3) {
        timer3 = null;
      }
      getMonitor3();
    },
    flag ? 0 : 3000
  );
}
// async function getDeviceList() {
//   let res = await set130StationRead({ stationId: devStationId.value, deviceId: formEdit.cgq });
//   if (res) {
//     getMonitor2(true)
//   }
// }
// async function getShowReadList() {
//   let data = await get130StationData();
//   if (data && data.length != 0) {
//     let list = data.filter((v) => v.stationId == devStationId.value)[0];
//     formView.dylfsfx = list.dylfsfx
//     formView.dyl1f2sADz = list.dyl1f2sADz
//     formView.dyl2f1sADz = list.dyl2f1sADz
//     formView.tfl = list.tfl
//     formView.yjbb = list.yjbb
//     formView.rjbb = list.rjbb
//     formView.zxlxbz = list.zxlxbz
//     formView.rqsj = list.rqsj
//   }
// }
async function tableExpand(expaned, record) {
  if (expaned) {
    expandedRowKeys.push(record.stationId)
  } else {
    expandedRowKeys.length = 0
  }
}


//tab选项切换
async function onChangeTab(tab) {
  activeKey.value = tab;
  stationId.value = ''
  clearTimeout(timer);
  clearTimeout(timer3)
  if (activeKey.value == 'device') {
    // await getSubStationList();
    // await getStationList1();
    getMonitor()
  } else if (activeKey.value == 'manageAuto') {
    await getStationList();
    getMonitor();
  } else if (activeKey.value == 'operationRecord') {
    await getSearchRecord({ stationId: '', deviceId: '' })
  }
}

//弹窗关闭清空数据
function cancenModal() {
  // clearTimeout(timer2)
  formEdit.id = ''
  formEdit.dz = ''
  formEdit.csms = ''
  formEdit.ttjl = ''
  formEdit.jxjl = ''
  formEdit.jd = ''
  formEdit.kqsz = ''
  formEdit.phzq = ''
  formEdit.sxyfbs = ''
  formEdit.xxyfbs = ''
  formEdit.fdjmj = ''
  formEdit.ztxzxs = ''
  formEdit.xzxs1 = ''
  formEdit.xzxs2 = ''
  formEdit.xzxs3 = ''
  formEdit.xzxs4 = ''
  formEdit.xzxs5 = ''
  formEdit.xzxs6 = ''
  formEdit.xzxsb = ''
  formEdit.xsbfh = ''
  formEdit.gbjyz = ''
  formEdit.dbjyz = ''
  formEdit.bjsn = ''
  formEdit.dyl485btl = ''
  formEdit.fw = ''
  formEdit.range = ''
  formEdit.csfs = ''
  formEdit.csfx = ''


  // formView.dylfsfx = ''
  // formView.dylbjzt = ''
  // formView.dyl1f2sADz = ''
  // formView.dyl2f1sADz = ''
  // formView.tfl = ''
  // formView.yjbb = ''
  // formView.rjbb = ''
  // formView.zxlxbz = ''
  // formView.rqsj = ''
}
//获取详细信息列表
async function getStationList() {
  let res = await get130List();
  tableData1.value = res.map((el: any) => {
    el.devInfoList = el.devInfoList.map(v => {
      return { ...v, ...v.param, stationId: el.id }
    })
    el.key = el.id
    el = { ...el, ...el.station130Param }
    return el
  });
}
// //传感器选项切换
// function changeCgq(val) {
//   formEdit.cgq = val;
// }
//编辑
async function handleEdit(record) {
  // cgqList.length = 0;
  visibleModalEdit1.value = true;
  devStationId.value = record.stationId;
  formEdit.id = record.id
  // let res = await get130StationDevices({ stationId: devStationId.value });
  // if (res.length != 0) {
  //   res.forEach((el) => {
  //     cgqList.push({ label: el.strinstallpos, value: el.id });
  //   });
  // }
}

//下发
async function handleClick(data) {
  switch (data) {
    case '地址':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'dz', value: formEdit.dz });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '测试模式':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'csms', value: formEdit.csms });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '探头距离':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'ttjl', value: formEdit.ttjl });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '基线距离':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'jxjl', value: formEdit.jxjl });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '角度':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'jd', value: formEdit.jd });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '开启时长':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'kqsz', value: formEdit.kqsz });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '平滑周期':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'phzq', value: formEdit.phzq });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '上行运放倍数':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'sxyfbs', value: formEdit.sxyfbs });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '下行运放倍数':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xxyfbs', value: formEdit.xxyfbs });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '风道截面积':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'fdjmj', value: formEdit.fdjmj });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '总体校准系数':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'ztxzxs', value: formEdit.ztxzxs });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数一':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs1', value: formEdit.xzxs1 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数二':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs2', value: formEdit.xzxs2 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数三':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs3', value: formEdit.xzxs3 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数四':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs4', value: formEdit.xzxs4 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数五':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs5', value: formEdit.xzxs5 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数六':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxs6', value: formEdit.xzxs6 });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '校准系数b':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xzxsb', value: formEdit.xzxsb });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '系数b符号':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'xsbfh', value: formEdit.xsbfh });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '高报警阈值':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'gbjyz', value: formEdit.gbjyz });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '低报警阈值':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'dbjyz', value: formEdit.dbjyz });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '报警使能':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'bjsn', value: formEdit.bjsn });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '第一路485波特率':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'dyl485btl', value: formEdit.dyl485btl });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '复位':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'fw', value: formEdit.fw });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '浮动范围':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'range', value: formEdit.range });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '测试风速':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'csfs', value: formEdit.csfs });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
    case '测试风向':
      await set130StationData({ stationId: devStationId.value, deviceId: formEdit.id, plcCode: 'csfx', value: formEdit.csfx });
      visibleModalEdit1.value = false;
      cancenModal()
      getStationList();
      break;
  }
}

//获取分站实时监测信息
async function getSubStationList() {
  let res = await subStationList({ strtype: 'modbus', reqparam: '130' });
  if (res.length != 0) {
    cardList.value = res;
    stationId.value = stationId.value ? stationId.value : cardList.value[0].id
    openNum.value = cardList.value?.filter((v) => v.linkstatus == 1)['length'];
    clsoeNum.value = cardList.value?.filter((v) => v.linkstatus == 0)['length'];
  } else {
    cardList.value = [];
  }
}
//分站站点选项点击
function cardClick(item, index) {
  clearTimeout(timer3)
  clearTimeout(timer)
  selectedIndex.value = index; // 更新选中索引
  activeIndex.value =  !item.linkstatus ? index : null;
  stationName.value = item.strname;
  stationStatus.value = item.linkstatus;
  stationId.value = item.id;
  isShow.value = true;
  getStationList1();
  getMonitor()
}
//分站站点名称编辑
function changeName(val) {
  getChangeStation();
}
async function getChangeStation() {
  let res = await getEdit({ id: stationId.value, strname: stationName.value, linkstatus: stationStatus.value });
  getSubStationList();
  isShow.value = false;
}
//站点连接状态修改
function changeStatus(val) {
  getChangeStation();
}
//获取详细信息列表
async function getStationList1() {
  let res = await get130List({ subId: stationId.value, pageNo: pagination.current, pageSize: pagination.pageSize });
  if (res && res.length != 0) {
    res.forEach((el) => {
      el.key = el.id;
      el.linkIdC = el.linkId || '';
      el.stripC = el.strip || '';
      el.linkstatusC = el.linkstatus ? '连接' : '断开';
      el.gdmsC = el.gdms == '1' ? '直流供电' : el.gdms == '0' ? '交流供电' : '';
      el.children = el.devInfoList;
      el.children.forEach((v) => {
        v.key = v.id;
        v.debugTitle = v.deviceType == 'windrect_ds_two_130' ? v.status === 1 ? '结束调试' : v.status === 0 ? '调试' : '' : ''
        v.stripC = v.strserno || '';
        v.linkstatusC = v.netStatus ? '连接' : '断开';
        v.linkIdC = v.linkId == '0' ? '未启用' : v.linkId == '1' ? '启用' : v.linkId == '2' ? '设备异常' : '';
        v.updateTime = v.time;
        v.gdmsC = v.gdms == '1' ? '直流供电' : v.gdms == '0' ? '交流供电' : '';
        v.valueJc = `风向:${v.forward || ''},风量:${v.m3 || ''}m³/min,风速:${v.windSpeed || ''}m/s,气压:${v.pa || ''}Pa,压差:${v.difPress || ''
          }Pa,温度:${v.temperature || ''}℃,湿度:${v.humidity || ''}%,断面积:${v.area || ''}㎡,ch4:${v.ch4 || ''}%`;
      });
    });
  }

  tableData.value = res;
  pagination.total = res.total;
}
async function getStationListAll() {
  let res = await get130List();
  res.forEach((el) => {
    el.key = el.id;
    el.linkIdC = el.linkId || '';
    el.stripC = el.strip || '';
    el.linkstatusC = el.linkstatus ? '连接' : '断开';
    el.gdmsC = el.gdms == '1' ? '直流供电' : el.gdms == '0' ? '交流供电' : '';
    el.children = el.devInfoList;
    el.children.forEach((v) => {
      v.key = v.id;
      v.debugTitle = v.deviceType == 'windrect_ds_two_130' ? v.status === 1 ? '结束调试' : v.status === 0 ? '调试' : '' : ''
      v.stripC = v.strserno || '';
      v.linkstatusC = v.netStatus ? '连接' : '断开';
      v.linkIdC = v.linkId == '0' ? '未启用' : v.linkId == '1' ? '启用' : v.linkId == '2' ? '设备异常' : '';
      v.updateTime = v.time;
      v.gdmsC = v.gdms == '1' ? '直流供电' : v.gdms == '0' ? '交流供电' : '';
      v.valueJc = `风向:${v.forward || ''},风量:${v.m3 || ''}m³/min,风速:${v.windSpeed || ''}m/s,气压:${v.pa || ''}Pa,压差:${v.difPress || ''
        }Pa,温度:${v.temperature || ''}℃,湿度:${v.humidity || ''}%,断面积:${v.area || ''}㎡,ch4:${v.ch4 || ''}%`;
    });
  });
  
  tableData.value = res;
  // pagination.total = res.total;
}
//显示全部
function getAllShow() {
  clearTimeout(timer)
  clearTimeout(timer3)
  getStationListAll()
  getMonitor3()
}
//启动新设备
async function handlerunDeviceMonitor(record, val) {
  devId.value = record.id;
  switch (val) {
    case '编辑':
      visibleModalEdit.value = true;
      startupDataEdit.address = record.strinstallpos;
      paramId.value = record.devInfoList ? 'subId' : 'devId';
      break;
    case '启动':
      visibleModal.value = true;
      startupData.address = record.strinstallpos;
      break;
    case '删除':
      await remove130Device({ devId: devId.value })
      getStationList1()
      break;
  }
}
//分站，设备调试
function debugClick(record) {
  if (record.debugTitle == '调试') {
    //正在调试中
    startupDataDebug.speed = '';
    startupDataDebug.direction = '';
    startupDataDebug.pwd = ''
    startupDataDebug.range = ''
    visibleModalDebug.value = true;
    debugFlag.value = 'device';
    debugDeviceId.value = record.id;
    tableData.value.forEach((el) => {
      el.devInfoList.forEach((v) => {
        if (v.id == debugDeviceId.value) {
          debugStationId.value = el.id;
        }
      });
    });
    // record.debugTitle = '结束调试';

  } else if (record.debugTitle == '结束调试') {
    debugFlag.value = 'device';
    debugDeviceId.value = record.id;
    visibleModalDebugEnd.value = true
    startupDataDebugEnd.pwd = ''
    tableData.value.forEach((el) => {
      el.devInfoList.forEach((v) => {
        if (v.id == debugDeviceId.value) {
          debugStationId.value = el.id;
        }
      });
    });
    // record.debugTitle = '调试';
  }
}
//确定结束调试
async function handleOkDebugEnd() {
  const encryption = new AesEncryption({ key: loginCipher.key, iv: loginCipher.iv });
  let res = await updateDebugStatus({ pwd: encryption.encryptByAES(startupDataDebugEnd.pwd), stationId: debugStationId.value, deviceId: debugDeviceId.value, debugFlag: '0' });
  if (res.code == 500) {
    message.error(res.message);
  } else {
    visibleModalDebugEnd.value = false;
    getStationList1();
  }
}
//取消结束调试
function handleCancelDebugEnd() {
  visibleModalDebugEnd.value = false;
  debugFlag.value = '';
  debugStationId.value = '';
  debugDeviceId.value = '';
}
async function handleOk() {
  let res = await runDeviceMonitor({ devId: devId.value, devName: startupData.address });
  visibleModal.value = false;
  getStationList1();
}
function handleCancel() {
  visibleModal.value = false;
  startupData.address = '';
}
//编辑
async function handleOkEdit() {
  if (paramId.value == 'subId') {
    let res = await update130DevName({ subId: devId.value, devName: startupDataEdit.address });
    visibleModalEdit.value = false;
    getStationList1();
  } else {
    let res = await update130DevName({ devId: devId.value, devName: startupDataEdit.address });
    visibleModalEdit.value = false;
    getStationList1();
  }
}
//取消编辑
function handleCancelEdit() {
  visibleModalEdit.value = false;
  startupDataEdit.address = '';
}
//调试确认
async function handleOkDebug() {
  const encryption = new AesEncryption({ key: loginCipher.key, iv: loginCipher.iv });
  let res = await updateDebugStatus({
    stationId: debugStationId.value,
    deviceId: debugDeviceId.value,
    speed: startupDataDebug.speed,
    direction: startupDataDebug.direction,
    range: startupDataDebug.range,
    pwd: encryption.encryptByAES(startupDataDebug.pwd),
    debugFlag: '1',
  });
  if (res.code == 500) {
    message.error(res.message);
  } else {
    visibleModalDebug.value = false;
    getStationList1();
  }
}
//调试取消
function handleCancelDebug() {
  visibleModalDebug.value = false;
  // tableData.value.forEach((el) => {
  //   el.devInfoList.forEach((v) => {
  //     //  v.debugTitle = v.status===1 ? '结束调试'  : v.status===0 ? '调试' : ''
  //     v.debugTitle = v.deviceType == 'windrect_ds_25x' || v.deviceType == 'windrect_ds_two' ? v.status === 1 ? '结束调试' : v.status === 0 ? '调试' : '' : ''
  //   });
  // });
  debugFlag.value = '';
  debugStationId.value = '';
  debugDeviceId.value = '';
}
//分页切换
function pageChange(val) {
  pagination.current = val.current;
  pagination.pageSize = val.pageSize;
  getStationList1();
}

//删除左侧分站
async function handleDelStation() {
  let res = await remove130Substation({ stationId: stationId.value })
  if (res) {
    getSubStationList();
    getStationList1();
  }
}
//取消删除左侧分站
function handleCancelDelStation() { }

//操作记录查询
async function getSearchRecord(param) {
  let res = await get158SetLog({ stationType: 130, ...param })
  if (res && res.length != 0) {
    operationData.value = res.records
  }
}
onMounted(async () => {
  await getSubStationList();
  await getStationList1();
  getMonitor()
});
onUnmounted(() => {
  if (timer) {
    clearTimeout(timer);
    timer = undefined;
  }

});
</script>

<style lang="less" scoped>
.safetyList {
  width: calc(100% - 20px);
  height: calc(100% - 80px);
  position: relative;
  margin: 70px 10px 10px 10px;

  .content {
    position: relative;
    width: 100%;
    height: 100%;

    .tab-box {
      display: flex;
      color: #fff;
      position: relative;
      background: linear-gradient(#001325, #012e4f);

      :deep(.zxm-tabs-nav) {
        margin: 0 !important;

        .zxm-tabs-tab {
          width: 180px;
          height: 45px;
          background: url('/@/assets/images/top-btn.png') center no-repeat;
          background-size: cover;
          display: flex;
          justify-content: center;
          font-size: 16px;
        }

        .zxm-tabs-tab-active {
          width: 180px;
          position: relative;
          background: url('/@/assets/images/top-btn-select.png') center no-repeat;
          background-size: cover;

          .zxm-tabs-tab-btn {
            color: #fff !important;
          }
        }

        .zxm-tabs-ink-bar {
          width: 0 !important;
        }

        .zxm-tabs-tab+.zxm-tabs-tab {
          margin: 0 !important;
        }
      }
    }

    .box-content {
      height: calc(100% - 50px);
      padding-top: 10px;
      box-sizing: border-box;

      .now-content {
        position: relative;
        width: 100%;
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;

        .left-box {
          width: 40%;
          height: 100%;
          margin-right: 15px;
          padding: 10px;
          box-sizing: border-box;
          background: url('/@/assets/images/fire/bj1.png') no-repeat center;
          background-size: 100% 100%;

          .left-title {
            display: flex;
            height: 30px;
            align-items: center;
            font-size: 14px;
            margin-bottom: 10px;

            span {
              color: #fff;
            }

            .zd-open {
              color: rgb(0, 242, 255);
            }

            .zd-close {
              color: #ff0000;
            }

            .title-fz {
              margin-right: 25px;
            }
          }

          .left-content {
            display: flex;
            justify-content: flex-start;
            align-items: flex-start;
            flex-wrap: wrap;
            height: calc(100% - 40px);
            overflow-y: auto;

            .card-box {
              position: relative;
              // width: 242px;
              width: 182px;
              height: 120px;
              margin-bottom: 15px;
              display: flex;
              justify-content: center;

              .card-itemN {
                position: relative;
                width: 85px;
                height: 120px;
                background: url('/@/assets/images/zd-2.png') no-repeat center;
                background-size: 100% 100%;
                cursor: pointer;

                .card-item-label {
                  width: 100%;
                  position: absolute;
                  bottom: 5px;
                  font-size: 12px;
                  color: #fff;
                  text-align: center;
                }
              }

              .card-itemL {
                position: relative;
                width: 85px;
                height: 120px;
                background: url('/@/assets/images/zd-3.png') no-repeat center;
                background-size: 100% 100%;
                cursor: pointer;

                .card-item-label {
                  width: 100%;
                  position: absolute;
                  bottom: 5px;
                  font-size: 12px;
                  color: #fff;
                  text-align: center;
                }
              }

              .card-itemD {
                position: relative;
                width: 85px;
                height: 120px;
                background: url('/@/assets/images/zd-1.png') no-repeat center;
                background-size: 100% 100%;
                cursor: pointer;

                .card-item-label {
                  width: 100%;
                  position: absolute;
                  bottom: 5px;
                  font-size: 12px;
                  color: #fff;
                  text-align: center;
                }
              }

              .card-modal {
                width: 86px;
                position: absolute;
                left: 140px;
                color: #fff;
                top: 50%;
                transform: translate(0, -50%);
                font-size: 12px;
              }

              .card-modal1 {
                width: 86px;
                position: absolute;
                left: -42px;
                color: #fff;
                top: 50%;
                transform: translate(0, -50%);
                font-size: 12px;
              }
            }
          }
        }

        .right-box {
          width: calc(60% - 15px);
          height: 100%;
          padding: 10px;
          box-sizing: border-box;
          background: url('/@/assets/images/fire/bj1.png') no-repeat center;
          background-size: 100% 100%;

          .right-title {
            display: flex;
            height: 30px;
            align-items: center;
            font-size: 14px;
            color: #fff;
            margin-bottom: 10px;
          }
        }
      }

      .detail-content {
        width: 100%;
        height: 100%;
      }


    }
  }
}

.down-btn {
  line-height: 15px;
  height: 20px;
  padding: 0px 17px;
  font-size: 12px;
}

.zxm-form {
  width: 100%;
  height: 100%;
  padding-top: 20px !important;
  box-sizing: border-box;

  .zxm-form-item {
    width: 50%;
    margin-right: 0px
  }
}

.zxm-picker,
.zxm-input {
  border: 1px solid #3ad8ff77 !important;
  background-color: #ffffff !important;
  color: #fff !important;
}

.card-item.selected {
  border: 2px solid #3ad8ff77;
  /* 选中时的边框颜色 */
}

::v-deep(.zxm-radio-wrapper) {
  font-size: 12px;
}

::v-deep(.zxm-input) {
  font-size: 12px;
}

::v-deep(.zxm-select:not(.zxm-select-customize-input) .zxm-select-selector) {
  border: 1px solid #3ad8ff77 !important;
}
</style>
<style>
div[aria-hidden='true'] {
  display: none !important;
}
</style>
