<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <!-- 把 dedust3D 放到这里则模型可以正常旋转 -->
  <div id="dedust3D" v-show="activeKey == 'monitor'" style="width: 100%; height: 100%; position: absolute; overflow: hidden"> </div>
  <div class="scene-box">
    <customHeader
      :fieldNames="{ label: 'systemname', value: 'id', options: 'children' }"
      :options="options"
      :optionValue="optionValue"
      @change="getSelectRow"
    >
      除尘风机智能管控
    </customHeader>
    <div class="center-container">
      <template v-if="activeKey == 'monitor'">
        <DedustHome :deviceId="optionValue" />
      </template>
      <div v-else class="history-group">
        <div class="device-button-group" v-if="deviceList.length > 0 && activeKey !== 'faultRecord'">
          <div
            class="device-button"
            :class="{ 'device-active': deviceActive == device.deviceType }"
            v-for="(device, index) in deviceList"
            :key="index"
            @click="deviceChange(index)"
            >{{ device.deviceName }}</div
          >
        </div>
        <div class="history-container">
          <DedustHistory
            v-if="activeKey == 'monitor_history'"
            ref="historyTable"
            class="vent-margin-t-20"
            :deviceId="optionValue"
            :device-type="deviceType"
          />
          <HandleHistory
            v-if="activeKey == 'handler_history'"
            ref="alarmHistoryTable"
            class="vent-margin-t-20"
            :deviceId="optionValue"
            :device-type="deviceType"
          />
          <AlarmHistory
            v-if="activeKey == 'faultRecord'"
            ref="handlerHistoryTable"
            class="vent-margin-t-20"
            :deviceId="optionValue"
            :device-type="deviceType"
          />
        </div>
      </div>
    </div>
    <BottomMenu @change="changeActive" />
  </div>
</template>

<script setup lang="ts">
  import { onBeforeMount, ref, onMounted, reactive } from 'vue';
  import { systemList, getTableList } from './dedust.api';
  import customHeader from '/@/components/vent/customHeader.vue';
  import BottomMenu from '/@/views/vent/comment/components/bottomMenu.vue';
  import DedustHome from './components/DedustHome.vue';
  import DedustHistory from './components/DedustHistory.vue';
  import HandleHistory from './components/HandleHistory.vue';
  import AlarmHistory from './components/AlarmHistory.vue';
  import { useRouter } from 'vue-router';

  type DeviceType = { deviceType: string; deviceName: string; datalist: any[] };

  const { currentRoute } = useRouter();
  const activeKey = ref('monitor');

  const historyTable = ref();
  const alarmHistoryTable = ref();
  const handlerHistoryTable = ref();

  //关联设备
  const deviceList = ref<DeviceType[]>([]);
  const deviceActive = ref('');
  const deviceType = ref('');

  const options = ref();
  // 默认初始是第一行
  // const selectRowIndex = ref(0);
  const dataSource = ref([]);

  const optionValue = ref('');

  // 监测数据
  const selectData = reactive({});

  function changeActive(activeValue) {
    activeKey.value = activeValue;
  }

  function deviceChange(index) {
    deviceActive.value = deviceType.value = deviceList.value[index].deviceType;
  }

  // 查询关联设备列表
  async function getDeviceList() {
    const res = await systemList({ devicetype: 'sys', systemID: optionValue.value });
    if (res && res.msgTxt && res.msgTxt.length) {
      const result = res.msgTxt;
      const deviceArr: DeviceType[] = [];
      result.forEach((item) => {
        const data = item['datalist'].filter((data: any) => {
          const readData = data.readData;
          return Object.assign(data, readData);
        });
        if (item.type != 'sys') {
          deviceArr.unshift({
            deviceType: item.type,
            deviceName: item['typeName'] ? item['typeName'] : item['datalist'][0]['typeName'],
            datalist: data,
          });
        }
      });
      deviceList.value = deviceArr;
      deviceActive.value = deviceArr[0].deviceType;
      deviceChange(0);
    }
  }

  async function getSysDataSource() {
    const res = await getTableList({ strtype: 'sys_surface_juejin', pagetype: 'normal' });
    if (!options.value && res) {
      // 初始时选择第一条数据
      options.value = res.records || [];
      if (!optionValue.value) {
        optionValue.value = options.value[0]['id'];
        getDeviceList();
      }
    }
  }

  // 切换检测数据
  async function getSelectRow(deviceID) {
    const currentData = dataSource.value.find((item: any) => {
      return item.deviceID == deviceID;
    });
    if (currentData) {
      optionValue.value = currentData['deviceID'];
      Object.assign(selectData, currentData);
      await getDeviceList();
    }
  }

  onBeforeMount(() => {});

  onMounted(async () => {
    if (currentRoute.value && currentRoute.value['query'] && currentRoute.value['query']['id']) {
      optionValue.value = currentRoute.value['query']['id'] as string;
    }
    await getSysDataSource();
    await getDeviceList();
  });
</script>
<style lang="less" scoped>
  @import '/@/design/theme.less';
  @import '/@/design/vent/modal.less';

  .history-group {
    padding: 0 20px;
    margin-top: 90px;
    .history-container {
      position: relative;
      background: #6195af1a;
      width: calc(100% + 10px);
      top: 0px;
      left: -10px;
      border: 1px solid #00fffd22;
      padding: 10px 0;
      box-shadow: 0 0 20px #44b4ff33 inset;
    }
  }
  .device-button-group {
    // margin: 0 20px;
    display: flex;
    pointer-events: auto;
    position: relative;
    &::after {
      position: absolute;
      content: '';
      width: calc(100% + 10px);
      height: 2px;
      top: 30px;
      left: -10px;
      border-bottom: 1px solid var(--vent-base-border);
    }
    .device-button {
      padding: 4px 15px;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 14px;

      color: var(--vent-font-color);
      cursor: pointer;
      margin: 0 3px;

      &::before {
        content: '';
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        border: 1px solid #6176af;
        transform: skewX(-38deg);
        background-color: rgba(0, 77, 103, 85%);
        z-index: -1;
      }
    }
    .device-active {
      // color: #0efcff;
      &::before {
        border-color: #0efcff;
        box-shadow: 1px 1px 3px 1px #0efcff inset;
      }
    }
  }
</style>
