<template>
  <a-spin tip="Loading..." :spinning="loading">
    <div class="monitor-container">
      <div class="lr left-box">
         <ventBox1>
          <template #title>
            <div>通防综合监测</div>
          </template>
          <template #container>
            <div class="vent-param">
              <div class="light-group">
                <div class="param-item">
                  <div class="param">
                    <div class="param-icon">
                      <div class="param-title">局扇吸风量</div>
                      <div class="param-unit">(m³/min)</div>
                    </div>

                    <div class="param-val-box">
                      <div class="param-val-icon"></div>
                      <div class="param-val">{{ workFaceSource['jin'] || '-' }}</div>
                    </div>
                  </div>
                </div>
                <div class="param-item">
                  <div class="param">
                    <div class="param-icon">
                      <div class="param-title">局扇供风量</div>
                      <div class="param-unit">(m³/min)</div>
                    </div>
                    <div class="param-val-box">
                      <div class="param-val-icon"></div>
                      <div class="param-val">{{ workFaceSource['hui']  || '-' }}</div>
                    </div>
                  </div>
                </div>
                <div class="param-item">
                  <div class="param">
                    <div class="param-icon">
                      <div class="param-title">总需风量</div>
                      <div class="param-unit">(m³/min)</div>
                    </div>
                    <div class="param-val-box">
                      <div class="param-val-icon"></div>
                      <div class="param-val">-</div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <BarAndLineCustom xAxisPropType="time" :chartData="workFaceHistorySource" height="220px"
                  :propTypeArr="['jin', 'hui']" :option="echartsOption" />
          </template>
        </ventBox1>
        <ventBox1 class="vent-margin-t-10"> 
          <template #title>
            <div>预警报警实时监测</div>
          </template>
          <template #container>
            <div class="warning-monitor">
              <div class="warning-item">
                <div class="title" :class="{ 'active-title': warningType == 'ventS' }" @click="showWarningList('ventS')">通风</div>
                <div class="state-box">
                  <span class="signal-round signal-round-blue"></span><span class="state vent-margin-l-8">正常</span>
                </div>
              </div>
              <div class="warning-item">
                <div class="title" :class="{ 'active-title': warningType == 'fireS' }" @click="showWarningList('fireS')">火灾</div>
                <div class="state-box">
                  <span class="signal-round signal-round-blue"></span><span class="state vent-margin-l-8">正常</span>
                </div>
              </div>
              <div class="warning-item">
                <div class="title" :class="{ 'active-title': warningType == 'dustS' }" @click="showWarningList('dustS')">粉尘</div>
                <div class="state-box">
                  <span class="signal-round signal-round-blue"></span><span class="state vent-margin-l-8">正常</span>
                </div>
              </div>
              <div class="warning-item">
                <div class="title" :class="{ 'active-title': warningType == 'gasS' }" @click="showWarningList('gasS')">瓦斯</div>
                <div class="state-box">
                  <span class="signal-round signal-round-blue"></span><span class="state vent-margin-l-8">正常</span>
                </div>
              </div>
            </div>
            <dv-scroll-board ref="scrollBoard" :config="warningConfig" style="width: 100%; height: 240px; overflow-y: auto; " />
          </template>
        </ventBox1>
      </div>
      <div class="lr right-box">
        <div class="item-box sensor-container">
          <ventBox1>
            <template #title>
              <div>风排瓦斯应急管控</div>
            </template>
            <template #container>
              <div class="parameter-title group-parameter-title"><SvgIcon class="icon" size="22" name="ytws"/><span>迎头瓦斯分析</span></div>
              <div class="gas-analyze">
                <div>
                  <p>迎头瓦斯含量（%）</p>
                  <div class="barnav">
                    <div class="bar2">
                      <span :style="{ 'width': gasContent+'%' }"></span>
                    </div>
                    <span class="num">{{ gasContent }}</span>
                  </div>
                </div>
                <div>
                  <div class="zaf">
                    <p>增长率</p>
                    <p class="vent-flex-row"><span class="rate">34<i>%</i></span><img src="/src/assets/images/vent/up.png"></p>
                  </div>
                </div>
                 <div>
                  <div class="zaf">
                    <p>增速分析</p>
                    <div class="vent-row-center result">正常</div>
                  </div>
                </div>
              </div>
              <div class="parameter-title group-parameter-title"><SvgIcon class="icon" size="22" name="shandian"/><span>局扇应急管控</span></div>
              <div class="input-item">
                <div class="title-auto">风量调控策略；</div>
                <div class="">
                  <a-radio-group v-model:value="compressorMode">
                    <a-radio :value="1">手动</a-radio>
                    <a-radio :value="2">自动</a-radio>
                  </a-radio-group>
                </div>
              </div>
              <div class="input-item" v-for="(item, index) in compressorParam" :key="index">
                <div class="title-auto">{{ item.title }}：</div>
                <div>
                  <a-input class="input-value" v-model="item.value" />
                  <span class="btn btn1 vent-margin-l-8">保存</span>
                </div>
              </div>
              <template v-if="compressorMode === 1">
                <div class="input-item" v-for="(item, index) in compressorDeviceParam" :key="index">
                  <div class="title-auto">{{ item.title }}：</div>
                  <div>
                    <a-input class="input-value" v-model="item.value" />
                    <span class="btn btn1 vent-margin-l-8">保存</span>
                  </div>
                </div>
              </template>
            </template>
          </ventBox1>
          <ventBox1 class="vent-margin-t-10">
            <template #title>
              <div>工作面人员信息</div>
            </template>
            <template #container>
              <div class="vent-flex-row-between vent-padding-lr-5 person-num">
                <div class="vent-row-center">
                  <SvgIcon class="icon-style" size="20" name="person"/>
                  <div class="vent-margin-l-8">当前工作面人数</div>
                </div>
                <div class="">0</div>
              </div>
              <div style="height: 180px; overflow-y: auto; position: relative;">
                <dv-scroll-board v-if="warningType" ref="scrollBoard" :config="locationConfig" style="width: 100%; height: 200px; overflow-y: auto; " />
              </div>
            </template>
          </ventBox1>
          <div class="item-box vent-margin-t-10" >
            <LivePlayer id="fm-player1" style="height: 220px;" ref="player1" :videoUrl="flvURL1()" muted live loading controls />
          </div>
        </div>
      </div>
    </div>
  </a-spin>
</template>

<script setup lang="ts">

import { nextTick, onBeforeMount, ref, onMounted, onUnmounted, reactive, defineProps } from 'vue';
import { list } from '../tunFace.api';
import ventBox1 from '/@/components/vent/ventBox1.vue'
import { ScrollBoard as DvScrollBoard } from '@kjgl77/datav-vue3';
import { echartsOption, warningConfig, locationConfig, compressorParam, compressorDeviceParam } from '../tunFace.data'
import { SvgIcon } from '/@/components/Icon';
import BarAndLineCustom from '/@/components/chart/BarAndLineCustom.vue';
import LivePlayer from '@liveqing/liveplayer-v3';

const props = defineProps({
  deviceId: {
    type: String,
    require: true
  }
})
const player1 = ref(null)
const loading = ref(false)

// 默认初始是第一行
const workFaceSource = ref({});
const workFaceHistorySource = ref([])
const gasContent = ref(24)
const compressorMode = ref(1)


// 监测数据
const selectData = reactive({});
const warnInfo = reactive({
  dustS: { warningList: [], dataList: [] },
  fireS: { warningList: [], dataList: [] },
  gasS: { warningList: [], dataList: [] },
  synthesizeS: { warningList: [], dataList: [] },
  ventS: { warningList: [], dataList: [] },
})
const warningType = ref('ventS')


const flvURL1 = () => {
  return `https://sf1-hscdn-tos.pstatp.com/obj/media-fe/xgplayer_doc_video/flv/xgplayer-demo-360p.flv`;
};

function showWarningList(type) {
  warningType.value = ''
  nextTick(() => {
    warningType.value = type
    warningConfig.data = warnInfo[type]['dataList']
  })

}

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

async function getDataSource(systemID) {
  const res = await list({ devicetype: 'sys', systemID, type: 'all' });
  const result = res.deviceInfo;
  for (const key in result) {
    const item = result[key]
    // ''.startsWith
    if (item.type === 'location') {
      const locationData = <any[]>[]
      item['datalist'].filter((data: any) => {
        locationData.push([data['strname'], data['strinstallpos'], data['readData']['flen']])
        const readData = data.readData;
        return Object.assign(data, readData);
      })
      locationConfig.data = locationData
    }
    loading.value = false;
  }
  workFaceHistorySource.value = res['sysInfo']['history']
  workFaceSource.value = Object.assign(res['sysInfo'], res['sysInfo']['readData']);
  warnInfo.dustS = { warningList: res['warnInfo']['dustS']['devices'] || [], dataList: initWarningData(res['warnInfo']['dustS']['devices']) }
  warnInfo.fireS = { warningList: res['warnInfo']['fireS']['devices'] || [], dataList: initWarningData(res['warnInfo']['fireS']['devices']) }
  warnInfo.gasS = { warningList: res['warnInfo']['gasS']['devices'] || [], dataList: initWarningData(res['warnInfo']['gasS']['devices']) }
  warnInfo.synthesizeS = { warningList: res['warnInfo']['synthesizeS']['devices'] || [], dataList: initWarningData(res['warnInfo']['synthesizeS']['devices']) }
  warnInfo.ventS = { warningList: res['warnInfo']['ventS']['devices'] || [], dataList: initWarningData(res['warnInfo']['ventS']['devices']) }
}

function initWarningData(devices: any[]) {
  const dataArr = <any[]>[]
  for (let i = 0; i < devices.length; i++) {
    const item = devices[i]
    dataArr.push([item['typeName'], item['warnLevel_str'], item['warnTime']])
  }
  return dataArr
}


function toDetail() {

}


onBeforeMount(() => {

});

onMounted(async () => {
  loading.value = true;
  timer = null
  await getMonitor(true)
});
onUnmounted(() => {
  if (timer) {
    clearTimeout(timer);
    timer = undefined;
  }
});
</script>

<style lang="less">
  @import '/@/design/vent/modal.less';

  .@{ventSpace}-select-dropdown {
    background: transparent !important;
    border-bottom: 1px solid rgba(236, 236, 236, 0.4);
    backdrop-filter: blur(10px);
    .@{ventSpace}-select-item {
      color: #fff !important;
    }

    .@{ventSpace}-select-item-option-selected,
    .@{ventSpace}-select-item-option-active {
      background-color: #00678b66 !important;
    }

    .@{ventSpace}-select-item:hover {
      background-color: #008fc366 !important;
    }
  }
</style>

<style lang="less" scoped>
  @import '../../comment/less/workFace.less';
  @ventSpace: zxm;
  
  .person-num{
    width: 100%;
    height: 28px;
    padding: 0 10px;
    background: linear-gradient(to right, #00ffff10, #00ccff02, #00ffff10);
    position: relative;
    margin-bottom: 5px;
    &::before{
      position: absolute;
      content: '';
      left: 0;
      width: 8px;
      height: 30px;
      background: url('/src/assets/images/vent/title-boder-left.png');
    }
    &::after{
      position: absolute;
      content: '';
      right: 0;
      width: 8px;
      height: 30px;
      background: url('/src/assets/images/vent/title-boder-right.png');
    }
  }
  .gas-analyze{
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 10px;
    margin-bottom: 5px;
    p{
      color: #acf9ff;
      height: 20px;
      font-size: 14px;
    }
    .barnav{
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;
      top: -5px;
      .bar2 {
        width: 120px;
        display: inline-block;
        border: 1px solid #00deff;
        height: 20px;
        padding: 4px;
        border-radius: 10px;
        margin-right: 10px;
        span{
          display: block;
          height: 100%;
          background: linear-gradient(to right,#14b6ff,#9cefff);
          border-radius: 8px
        }
      }
      .zaf{
        p{
          text-align: center;
        }
      }
    }
    
    .num{
      color: rgb(247, 232, 13);
      font-size: 18px;
    }
    .rate{
      color: rgb(0, 211, 255);
      font-family: Impact;
      font-size: 20px;
      margin-right: 2px;
    }
    .result{
      font-family: 'electronicFont';
      font-size: 18px;
      color: #61ddb1;
    }
  }
  

</style>
