<template>
  <div class="bg" style="width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; overflow: hidden">
    <a-spin :spinning="loading" />

    <div id="fanLocal3D" style="width: 100%; height: 100%; position: absolute; overflow: hidden"> </div>
    <div id="fanLocal3DCSS" class="threejs-Object-CSS" style="width: 100%; height: 100%; position: absolute; overflow: hidden">
      <div style="z-index: -1; position: relative">
        <div class="elementTag" id="inputBox">
          <div class="elementContent">
            <p>风筒入口风速：{{ selectData.windSpeed1 ? selectData.windSpeed1 : '-' }}</p>
            <p>风筒入口瓦斯浓度 {{ selectData.gas1 ? selectData.gas1 : '-' }}</p>
          </div>
        </div>
        <div class="elementTag" id="outBox">
          <div class="elementContent elementContent-r">
            <p>迎头供风量：{{ selectData.windQuantity1 ? selectData.windQuantity2 : '-' }}</p>
            <p>迎头瓦斯浓度：{{ selectData.gas1 ? selectData.gas1 : '-' }}</p>
          </div>
        </div>
        <div class="elementTag" id="returnBox">
          <div class="elementContent">
            <p>回风流瓦斯浓度：{{ selectData.gas3 ? selectData.gas3 : '-' }}</p>
          </div>
        </div>
        <div class="elementTag" id="gateBox">
          <div class="elementContent">
            <p>风门状态：关</p>
            <p>风门过风面积：{{ selectData.gas1 ? selectData.gas1 : '-' }}</p>
          </div>
        </div>
        <div  class="elementTag" id="windownBox">
          <div class="elementContent">
            <p>风窗全风压回风流：0</p>
          </div>
        </div>
      </div>
    </div>
  </div>

  <div class="scene-box">
    <div class="title-text">
      {{ selectData.strinstallpos }}
    </div>
    <div class="bottom-tabs-box" @mousedown="setDivHeight($event, 240, scroll)">
      <dv-border-box8 :dur="5"  :style="`padding: 5px; height: ${scroll.y + 100}px`" >
      
        <a-tabs class="tabs-box" v-model:activeKey="activeKey" @change="tabChange">
          <a-tab-pane key="1" tab="实时监测">
            <GroupMonitorTable ref="MonitorDataTable"  v-if="activeKey === '1' " :dataSource="dataSource" columnsType="sys_surface_junya" @selectRow="getSelectRow" :scroll="scroll"/>
          </a-tab-pane>
          <a-tab-pane key="2" tab="实时曲线图" force-render>
            <div class="tab-item" v-if="activeKey === '2' ">
              <div class="vent-flex-row-between" style="height: 100%">
                <BarSingle
                  :xAxisData="[
                    { key: 'T1', valueKey: 'gas1' },
                    { key: 'T2', valueKey: 'gas2' },
                    { key: 'T3', valueKey: 'gas3' },
                    { key: 'T4', valueKey: 'gas4' },
                  ]"
                  :dataSource="dataSource[selectRowIndex]"
                  height="100%"
                  :chartsColumns="chartsColumns"
                  style="flex: 3"
                />
                <BarSingle
                  :xAxisData="[
                    { key: 'F1', valueKey: 'windQuantity1' },
                    { key: 'F2', valueKey: 'windQuantity2' },
                  ]"
                  :dataSource="dataSource[selectRowIndex]"
                  height="100%"
                  :chartsColumns="chartsColumns1"
                  style="flex: 2"
                />
              </div>
            </div>
          </a-tab-pane>
          <a-tab-pane key="3" tab="历史数据">
            <div class="tab-item" v-if="activeKey === '3' ">
              <HistoryTable columns-type="sys_surface_junya" device-type="sys_surface_junya" :device-list-api="baseList" designScope="sys_surface_junya-history" />
            </div>
          </a-tab-pane>
          <a-tab-pane key="4" tab="报警历史">
            <div class="tab-item" v-if="activeKey === '4' ">
              <AlarmHistoryTable columns-type="alarm" device-type="sys_surface_junya" :device-list-api="baseList" designScope="alarm-history" />
            </div>
          </a-tab-pane>
          <a-tab-pane key="5" tab="操作历史">
            <div class="tab-item" v-if="activeKey === '5' ">
              <HandlerHistoryTable columns-type="operator_history" device-type="sys_surface_junya" :device-list-api="baseList" designScope="alarm-history" />
            </div>
          </a-tab-pane>
        </a-tabs>
      </dv-border-box8>
    </div>
  </div>
  <div style="z-index: -1; position: absolute; top: 50px; right: 10px; width: 300px; height: 280px; margin: auto" class="player1">
    <LivePlayer id="jb-player1" ref="player1" :videoUrl="flvURL1()" muted live loading controls />
  </div>
</template>

<script setup lang="ts">
  import { onBeforeMount, ref, onMounted, nextTick, toRaw, reactive, onUnmounted, watch } from 'vue';
  import BarSingle from '../../../../components/chart/BarSingle.vue';
  import GroupMonitorTable from '../comment/GroupMonitorTable.vue';
  import HistoryTable from '../comment/HistoryTable.vue';
  import AlarmHistoryTable from '../comment/AlarmHistoryTable.vue';
  import HandlerHistoryTable from '../comment/HandlerHistoryTable.vue';
  import { mountedThree, setModelType, destroy, addCssText, addText, playSmoke } from './balancePress.three';
  import lodash from 'lodash';
  import { getTableList, list } from '/@/views/vent/monitorManager/fanLocalMonitor/fanLocal.api';
  import { list as baseList } from '../../deviceManager/fanTabel/fan.api';
  import { chartsColumns, chartsColumns1 } from './balancePress.data';
  import LivePlayer from '@liveqing/liveplayer-v3';
  import { setDivHeight } from '/@/utils/event';
  import { BorderBox8 as DvBorderBox8 } from '@kjgl77/datav-vue3';

  const scroll = reactive({
    y: 180
  })
  
  const loading = ref(false);
  const activeKey = ref('1');
  const player1 = ref();
  // 默认初始是第一行
  const selectRowIndex = ref(0);

  // 监测数据
  const initData = {
    deviceID: '',
    deviceType: '',
    strname: '',
    dataDh: '-', //压差
    dataDtestq: '-', //测试风量
    sourcePressure: '-', //气源压力
    dataDequivalarea: '-',
    netStatus: '0', //通信状态
    fault: '气源压力超限',
  };
  const flvURL1 = () => {
    // return `https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv`;
    return ''
  };
  // 监测数据
  const selectData = reactive(lodash.cloneDeep(initData));

  const tabChange = (activeKeyVal) => {
    activeKey.value = activeKeyVal;
    // if (activeKeyVal == 1) {
    //   nextTick(() => {
    //     MonitorDataTable.value.setSelectedRowKeys([selectData.deviceID])
    //   })
    // }
  };

  const dataSource = ref([]);

  //
  async function getDataSource(){
    const res = await list({ devicetype: 'sys_surface_junya', pagetype: 'normal' });
    const dataArr = res.msgTxt[0].datalist || [];
    dataSource.value = [];
    dataArr.forEach((data) => {
      const readData = data.readData;
      data = Object.assign(data, readData);
      dataSource.value.push(data);
    });
    const data: any = toRaw(dataSource.value[selectRowIndex.value]); //maxarea
    return data;
  };

  // https获取监测数据
  let timer: null | NodeJS.Timeout = null;
  function getMonitor(flag?) {
    if (Object.prototype.toString.call(timer) === '[object Null]') {
      timer = setTimeout(async () => {
        await getDataSource();
        Object.assign(selectData, deviceBaseList.value, dataSource.value[selectRowIndex.value]);
        addText(selectData);
        // playAnimation(data, selectData.maxarea);
        if (timer) {
          timer = null;
        }
        getMonitor();
      }, flag ? 0 :1000);
    }
  };

  // 获取设备基本信息列表
  const deviceBaseList = ref([]);
  function getDeviceBaseList() {
    getTableList({ pageSize: 1000 }).then((res) => {
      deviceBaseList.value = res.records;
    });
  };

  // 切换检测数据
  function getSelectRow(id) {
    if (!id) return;
    loading.value = true;
    const baseDataIndex: any = dataSource.value.findIndex((baseData: any) => baseData.deviceID === id);
    selectRowIndex.value = baseDataIndex;
    const type = baseDataIndex > 0 ? 'fm' : 'fc';
    setModelType(type).then(() => {
      loading.value = false;
    });
    // const data = dataSource.value[baseDataIndex];
    
    // addCssText(selectRowIndex.value);
    return;
  };

  function addPlayVideo() {
    if (player1.value && player1.value.play) {
      if (!player1.value.paused()) player1.value.play();
      document.body.removeEventListener('mousedown', addPlayVideo);
    }
  };


  onBeforeMount(() => {
    getDeviceBaseList();
  });

  onMounted(() => {
    
    // loading.value = true;
    mountedThree(player1.value).then(() => {
      nextTick(() => {
        getMonitor(true);
        // addCssText(selectRowIndex.value);

        playSmoke('startSmoke', 'top', 30, 'open');
      });
    });
    document.body.addEventListener('mousedown', addPlayVideo, false);
  });

  onUnmounted(() => {
    destroy();
    if (timer) {
      clearTimeout(timer);
      timer = undefined;
    }
  });
</script>
<style scoped lang="less">
  @import '/@/design/vent/modal.less';
  :deep(.@{ventSpace}-tabs-tabpane-active) {
    overflow: auto;
  }
  .input-box {
    display: flex;
    align-items: center;
    .input-title {
      color: #73e8fe;
      width: auto;
    }
    margin-right: 10px;
  }
  .label {
    max-width: 220px;
  }
  .@{ventSpace}-input {
    width: 150px;
  }
</style>
