<template>
  <div
    v-show="activeKey == 'monitor' && !loading" 
    class="bg"
    style="width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; overflow: hidden">
    <a-spin :spinning="loading" />
    
    <div id="workFace3D" style="width: 100%; height: 100%; position: absolute; overflow: hidden; z-index: 1; top: 0"> </div>
    <div
          id="workFace3DCSS"
          class="threejs-Object-CSS"
          v-show="!loading"
          style="width: 100%; height: 100%; position: absolute; pointer-events: none; overflow: hidden; z-index: 1; top: 0"
        >
      </div>
  </div>
  <div class="scene-box">
    <customHeader :fieldNames="{ label: 'systemname', value: 'id', options: 'children' }" :options = 'options' @change="getSelectRow" :optionValue="optionValue">回采工作面智能管控</customHeader>
    <div class="center-container">
      <template v-if="activeKey == 'monitor'">
        <div class="monitor-nav">
          <template  v-for="(nav, index) in monitorNavData" :key="index">
            <div class="nav-item" :class="{'nav-item-active': nav.isShow}" @click="changeMonitor(nav)">{{ nav.title }} </div>
            <a-divider v-if="index !== monitorNav.length - 1" type="vertical" style="height: 10px; background-color: #00e1ff;" />
          </template>
        </div>
        <workFaceHome v-if="monitorActive == 0" :deviceId = 'optionValue' />
        <workFaceVentHome v-if="monitorActive == 1" :deviceId = 'optionValue' />
        <workFaceFireHome v-if="monitorActive == 2" :deviceId = 'optionValue'/>
        <workFaceDustHome v-if="monitorActive == 3" :deviceId = 'optionValue'/>
        <workFaceGasHome v-if="monitorActive == 4" :deviceId = 'optionValue' :gas-unit-num="gasUnitNum"/>
      </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">
          <workFaceHistory v-if="activeKey == 'monitor_history' && isRefresh" ref="historyTable" class="vent-margin-t-20" :deviceId = 'optionValue' :device-type="deviceType"/>
          <workFaceHandleHistory v-if="activeKey == 'handler_history' && isRefresh" ref="alarmHistoryTable" class="vent-margin-t-20" :deviceId = 'optionValue' :device-type="deviceType" />
          <workFaceAlarmHistory v-if="activeKey == 'faultRecord' && isRefresh" 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, onUnmounted, nextTick } from 'vue';
import { mountedThree, destroy, setModelType, clearCss3D, showOrHideGasPlane } from './wokeFace.threejs';
import { getTableList, systemList } from './workFace.api';
import { monitorNav } from './workFace.data'
import customHeader from '/@/components/vent/customHeader.vue';
import BottomMenu from '/@/views/vent/comment/components/bottomMenu.vue';
import workFaceVentHome from './components/workFaceVentHome.vue';
import workFaceHome from './components/workFaceHome.vue';
import workFaceFireHome from './components/workFaceFireHome.vue'
import workFaceDustHome from './components/workFaceDustHome.vue';
import workFaceGasHome from './components/workFaceGasHome.vue';
import workFaceHistory from './components/workFaceHistory.vue';
import workFaceHandleHistory from './components/workFaceHandleHistory.vue';
import workFaceAlarmHistory from './components/workFaceAlarmHistory.vue';
import { useRouter } from 'vue-router';

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

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

const monitorNavData = ref(monitorNav)
const monitorActive = ref(0)

let modalType = '' //模型类型

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

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

const options = ref()
const optionValue = ref('')
const gasUnitNum = ref(0)

const isRefresh = ref(true)

function changeActive(activeValue) {
  activeKey.value = activeValue
  loading.value = true
  if(activeKey.value === 'monitor'){
    // gasUnitNum.value = Math.ceil(Math.random() * 10)
    gasUnitNum.value = 4
    setTimeout(() =>{
      loading.value = false
    }, 600)
    
  }else{
    loading.value = false
    clearCss3D()
  }
  nextTick(() => {
    setModelType(modalType, gasUnitNum.value, monitorActive.value == 4 ? true : false)
  })
}

function deviceChange(index) {
  deviceActive.value = deviceType.value = deviceList.value[index].deviceType
  isRefresh.value = false
  nextTick(() => {
    isRefresh.value = true
  })
}

async function getDeviceList() {
  const res = await systemList({ devicetype: 'sys', systemID: optionValue.value });
  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_caimei', pagetype: 'normal' });
  if (!options.value) {
    // 初始时选择第一条数据
    options.value = res.records || [];
    if (!optionValue.value) {
      optionValue.value = options.value[0]['id']
      getDeviceList()
      changeModalType(options.value[0])
    }
  }
};

// 切换检测数据
async function getSelectRow(deviceID) {

  const currentData = options.value.find((item: any) => {
    return item.id == deviceID
  })
  optionValue.value = deviceID
  changeModalType(currentData)
  getDeviceList()
}

// 获取模型类型
function changeModalType(currentData) {
  if (currentData['strsystype'] === 'sys_surface_caimei_modal_1') {
    // 单进单回
    modalType = 'workFace1'
  } else if (currentData['strsystype'] === 'sys_surface_caimei_modal_2') {
    // 单进双回
    modalType = 'workFace2'
  } else if (currentData['strsystype'] === 'sys_surface_caimei_modal_3') {
    // 双进单回
    modalType = 'workFace3'
  } else if (currentData['strsystype'] === 'sys_surface_caimei_modal_4') {
    // 双进双回
    modalType = 'workFace4'
  }
  // gasUnitNum.value = Math.ceil(Math.random() * 4)
  gasUnitNum.value = 4
  setModelType(modalType, gasUnitNum.value, monitorActive.value == 4 ? true : false)
}

function changeMonitor(nav) {
  nav.isShow = true
  monitorNav.forEach((item, index) => {
    if(item.title !== nav.title) {
      item.isShow = false
    }else{
      monitorActive.value = index
      if (monitorActive.value != 4) {
        clearCss3D()
      }
    }
  })
  showOrHideGasPlane(monitorActive.value == 4 ? true : false)
}

onBeforeMount(() => {

});

onMounted(async() => {
  if (currentRoute.value && currentRoute.value['query'] && currentRoute.value['query']['id']) optionValue.value = currentRoute.value['query']['id']
  await getSysDataSource()
  loading.value = true;
  mountedThree().then(async () => {
    // gasUnitNum.value = Math.ceil(Math.random() * 10)
    gasUnitNum.value = 4
    loading.value = false;
    nextTick(() => {
      setModelType(modalType, gasUnitNum.value, monitorActive.value == 4 ? true : false)
    })
  });
});

onUnmounted(() => {
  destroy();
});

</script>
<style lang="less" scoped>
@import '/@/design/vent/modal.less';
@ventSpace: zxm;

:deep(.@{ventSpace}-tabs-tabpane-active) {
  overflow: auto;
}
.scene-box{
  width: 100%;
  height: 100%;
}
.monitor-nav{
  width: 860px;
  height: 42px;
  display: flex;
  justify-content: center;
  align-items: center;
  // border: 1px solid #73e8fe;
  color: #fff;
  position: absolute;
  top: 88px;
  left: 50% !important;
  transform: translateX(-50%) !important;
  pointer-events: auto;
  background: linear-gradient(45deg, #96c5ca38, #156c7d4a);
  clip-path: polygon(
    14px 0,
    calc(100% - 14px) 0,
    100% 14px,
    100% calc(100% - 14px),
    calc(100% - 14px) 100%,
    14px 100%,
    0 calc(100% - 14px),
    0 14px
  );
  // background: url('/@/assets/images/vent/wokeFaca-nav.png') no-repeat !important;

  .nav-item{
    padding: 1px 10px;
    cursor: pointer;
    
  }
  .nav-item-active{
    color: #00fbff;
  }
}

.history-group{
  padding: 0 20px;
  margin-top: 90px;
  .history-container{
    position: relative;
    background: #6195af1a;
    width: calc(100% + 10px);
    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 #0efcff;
  }
  .device-button{
    padding: 4px 15px;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 14px;
    
    color: #fff;
    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;
    }
  }
}

.input-box {
  display: flex;
  align-items: center;
  padding-left: 10px;

  .input-title {
    color: #73e8fe;
    width: auto;
  }

  .@{ventSpace}-input-number {
    border-color: #ffffff88 !important;
  }

  margin-right: 10px;
}
.monitor-msg-box {
  width: 170px;
  margin-top: 100px;

  .monitor-msg-container {
    width: 170px;
    height: 150px;
    box-shadow: rgba(128, 128, 128, 0.3) 0px 0px 40px inset;
    border: 1px solid rgba(128, 128, 128, 0.3);
    background-color: transparent;
  }

  .errorColor {
    box-shadow: #F73B2440 0px 0px 40px inset;
    border: 1px solid #F73B2440;
  }

  .warningColor {
    box-shadow: #FF9B1740 0px 0px 40px inset;
    border: 1px solid #FF9B1740;
  }

  .monitor-item {
    padding: 10px 10px 0px 10px;
    color: #fff;
    letter-spacing: 2px;

    .item-title {
      color: #73e8fe;
    }

    .num {
      color: #FFA500;
    }
  }
}
</style>
