<template>
  <div class="progress_container">
    <div class="header_wrap">
      <div class="left_progress">
        <div class="left_time_tag">剩余时间</div>
        <p class="time">
          {{
            operatorStore.estimatedRemainingTimeS == 0
              ? '已结束'
              : `${operatorStore.estimatedRemainingTimeS} S`
          }}
        </p>
        <!-- <div class="progress_bg">
          <div
            class="pro"
            :style="{
              '--width': '325px',
            }"
          ></div>
        </div> -->
      </div>
      <div class="right_btns">
        <div
          :class="operatorStore.disinfectStatus ? 'btn active' : 'btn'"
          @click="stopDisinfect"
        >
          停止消毒
        </div>
        <div
          :class="operatorStore.disinfectStatus ? 'btn active ml' : 'btn ml'"
          @click="pauseDisinfect"
        >
          暂停消毒
        </div>
        <div
          :class="operatorStore.disinfectStatus ? 'btn' : 'btn active'"
          @click="continueDisinfect"
        >
          继续消毒
        </div>
      </div>
    </div>
    <div class="echarts_wrap">
      <div class="single_wrap">
        <p class="title">设备温度/湿度/过氧化氢浓度</p>
        <div
          class="echarts_box"
          id="bin"
          v-if="operatorStore.disinfectStatus || localStorage.getItem('bin')"
        ></div>
      </div>
      <div class="single_wrap">
        <p class="title">环境1/湿度/过氧化氢浓度</p>
        <div
          class="echarts_box"
          id="envir1"
          v-if="operatorStore.disinfectStatus || localStorage.getItem('envir1')"
        ></div>
      </div>
      <div class="single_wrap">
        <p class="title">环境2/湿度/过氧化氢浓度</p>
        <div
          class="echarts_box"
          id="envir2"
          v-if="operatorStore.disinfectStatus || localStorage.getItem('envir2')"
        ></div>
      </div>
    </div>
    <div class="detail_wrap">
      <div class="detail" @click="showDetail">详情</div>
    </div>
  </div>
</template>

<script setup>
import { useOperatorStore, useWebSocketStore, useEchartsStore } from '@/store'
import {
  stopDisinfectionJSON,
  getStateJSON,
  continueDisinfectionJSON,
  pauseDisinfectionJSON,
} from '@/mock/command'
import { onMounted, onUnmounted, ref } from 'vue'
import * as echarts from 'echarts'
import { storeToRefs } from 'pinia'

const echartsStore = useEchartsStore()

const binOption = ref({
  tooltip: {
    trigger: 'axis',
  },
  dataZoom: {
    show: true, // 为true 滚动条出现
    realtime: true,
    type: 'slider', // 有type这个属性，滚动条在最下面，也可以不行，写y：36，这表示距离顶端36px，一般就是在图上面。
    height: 10, // 表示滚动条的高度，也就是粗细
    start: 40, // 表示默认展示0%～80%这一段。
    end: 100,
    backgroundColor: 'transparent',
    bottom: 10,
    textStyle: {
      color: 'transparent',
    },
  },
  legend: {
    data: ['温度', '湿度', '过氧化氢浓度'],
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: Object.keys(echartsStore.binCharts),
  },
  yAxis: {
    type: 'value',
  },
  series: [
    {
      name: '温度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.binTemp,
    },
    {
      name: '湿度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.binHumidity,
    },
    {
      name: '过氧化氢浓度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.binHP,
    },
  ],
})
const envir1Option = ref({
  tooltip: {
    trigger: 'axis',
  },
  dataZoom: {
    show: true, // 为true 滚动条出现
    realtime: true,
    type: 'slider', // 有type这个属性，滚动条在最下面，也可以不行，写y：36，这表示距离顶端36px，一般就是在图上面。
    height: 10, // 表示滚动条的高度，也就是粗细
    start: 40, // 表示默认展示0%～80%这一段。
    end: 100,
    backgroundColor: 'transparent',
    bottom: 10,
    textStyle: {
      color: 'transparent',
    },
  },
  legend: {
    data: ['温度', '湿度', '过氧化氢浓度'],
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: Object.keys(echartsStore.envir1Charts),
  },
  yAxis: {
    type: 'value',
  },
  series: [
    {
      name: '温度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir1Temp,
    },
    {
      name: '湿度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir1Humidity,
    },
    {
      name: '过氧化氢浓度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir1HP,
    },
  ],
})
const envir2Option = ref({
  tooltip: {
    trigger: 'axis',
  },
  dataZoom: {
    show: true, // 为true 滚动条出现
    realtime: true,
    type: 'slider', // 有type这个属性，滚动条在最下面，也可以不行，写y：36，这表示距离顶端36px，一般就是在图上面。
    height: 10, // 表示滚动条的高度，也就是粗细
    start: 40, // 表示默认展示0%～80%这一段。
    end: 100,
    backgroundColor: 'transparent',
    bottom: 10,
    textStyle: {
      color: 'transparent',
    },
  },
  legend: {
    data: ['温度', '湿度', '过氧化氢浓度'],
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: Object.keys(echartsStore.envir2Charts),
  },
  yAxis: {
    type: 'value',
  },
  series: [
    {
      name: '温度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir2Temp,
    },
    {
      name: '湿度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir2Humidity,
    },
    {
      name: '过氧化氢浓度',
      type: 'line',
      stack: 'Total',
      data: echartsStore.envir2HP,
    },
  ],
})

const operatorStore = useOperatorStore()
const webSocketStore = useWebSocketStore()
const props = defineProps({
  changeShowOperator: {
    type: Function,
  },
})
const showDetail = () => {
  props.changeShowOperator(true)
}

const timer = ref(null)
onMounted(() => {
  timer.value = setInterval(() => {
    webSocketStore.sendCommandMsg(getStateJSON)
  }, 1000)
  let a = echarts.getInstanceByDom(document.getElementById('bin'))
  if (a == undefined) {
    var binCharts = echarts.init(document.getElementById('bin'))
    binCharts.setOption(binOption.value)
  }
  let b = echarts.getInstanceByDom(document.getElementById('envir1'))
  if (b == undefined) {
    var envir1Charts = echarts.init(document.getElementById('envir1'))
    envir1Charts.setOption(envir1Option.value)
  }
  let c = echarts.getInstanceByDom(document.getElementById('envir2'))
  if (c == undefined) {
    var envir2Charts = echarts.init(document.getElementById('envir2'))
    envir2Charts.setOption(envir2Option.value)
  }
})
onUnmounted(() => {
  timer.value = null
})

const pauseDisinfect = () => {
  if (operatorStore.disinfectStatus) {
    webSocketStore.sendCommandMsg(pauseDisinfectionJSON)
    operatorStore.updateDisinfectStatus(false)
  }
}

const stopDisinfect = () => {
  if (operatorStore.disinfectStatus) {
    webSocketStore.sendCommandMsg(stopDisinfectionJSON)
    operatorStore.updateDisinfectStatus(false)
  }
}

const continueDisinfect = () => {
  if (!operatorStore.disinfectStatus) {
    webSocketStore.sendCommandMsg(continueDisinfectionJSON)
    operatorStore.updateDisinfectStatus(true)
  }
}
</script>

<style lang="scss" scoped>
.progress_container {
  margin-bottom: 30px;
  height: 580px;
  box-sizing: border-box;
  background: #ffffff;
  border-radius: 16px;
  padding: 20px;
  padding-bottom: 30px;
  .header_wrap {
    display: flex;
    align-items: center;
    margin-bottom: 49px;
    .left_progress {
      // width: 860px;
      flex: 1;
      height: 80px;
      border-radius: 14px;
      background: #f6f6f6;
      box-sizing: border-box;
      padding: 0 23px;
      display: flex;
      align-items: center;
      .left_time_tag {
        width: 158.66px;
        height: 45px;
        border-radius: 23px;
        opacity: 1;
        background: #06518b;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Source Han Sans CN;
        font-size: 14px;
        font-weight: normal;
        letter-spacing: 0.1em;
        color: #ffffff;
      }
      .time {
        // width: 90px;
        flex: 1;
        height: 20px;
        font-family: Source Han Sans CN;
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 0.1em;
        color: #000000;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .progress_bg {
        width: 396px;
        height: 14px;
        border-radius: 7px;
        background: #ffffff;
        position: relative;
        overflow: hidden;
        .pro {
          position: absolute;
          left: 0;
          top: 0;
          height: 14px;
          border-radius: 7px;
          background: #06518b;
          width: var(--width);
        }
      }
    }
    .right_btns {
      display: flex;
      align-items: center;
      box-sizing: border-box;
      margin-left: 20px;
      .btn {
        width: 140px;
        height: 45px;
        border-radius: 23px;
        background: #f6f6f6;
        display: flex;
        align-items: center;
        justify-content: center;
        font-family: Source Han Sans CN;
        font-size: 14px;
        font-weight: normal;
        letter-spacing: 0.1em;
        color: #d8d8d8;
      }
      .active {
        color: #ffffff;
        background: #06518b;
      }
      .ml {
        margin: 0 20px;
      }
    }
  }
  .echarts_wrap {
    height: 351px;
    display: flex;
    align-items: center;
    margin-bottom: 19px;
    .single_wrap {
      flex: 1;
      height: 351px;
      .title {
        font-family: Source Han Sans CN;
        font-size: 14px;
        font-weight: 500;
        letter-spacing: 0.1em;
        color: #000000;
        margin-bottom: 24px;
        padding-left: 11px;
      }
      .echarts_box {
        width: 380px;
        height: 308px;
      }
    }
  }
  .detail_wrap {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    padding-right: 16px;
    .detail {
      width: 105px;
      height: 31px;
      border-radius: 16px;
      background: #06518b;
      display: flex;
      align-items: center;
      justify-content: center;
      font-family: Source Han Sans CN;
      font-size: 12px;
      font-weight: normal;
      letter-spacing: 0.1em;
      color: #ffffff;
    }
  }
}
</style>
