<script setup>
import { ref, onBeforeUnmount, onMounted } from "vue";
import autofit from 'autofit.js'

import { userTop } from '@/components';
import WaterYield from "./components/WaterYield";
import WaterQuality from "./components/WaterQuality";
import ContinueData from "./components/ContinueData";

import shuizhi from "./components/shuizhi";
import dataBox from "./components/dataBox";
import liuliang from "./components/liuliang";
import gongyi from "./components/gongyi";
import middleBox from "./components/middleBox";
import EchartBar from "./components/EchartBar";
import ControlHelper from "./components/ControlHelper";
import { screenApi } from "@/api/screen"
import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';

dayjs.locale('zh-cn'); // 设置day.js使用中文/ 导入中文语言包

const gongyiData = ref([])
const reportData = ref({})
const screenData = ref({})
const time = ref('')
let timer = null
let timeHour = null

const dataTime = ref("");

// 获取工艺管控 助手
const getWarningList = () => {
  screenApi.warningList().then(res => {
    gongyiData.value = res.data || []
  })
}

// 获取数据费助手
const getLeastShortReport = () => {
  screenApi.getLeastShortReport().then(res => {
    reportData.value = res.data
  })
}

// 获取大屏分析数据
const getRealTimeData = () => {
  screenApi.realTimeData().then(res => {
    screenData.value = res.data
    dataTime.value = `更新时间：${res.data.testHour}`
    time.value = dayjs(res.data.timestamp).format('HH:mm:ss')
    timeHour = setInterval(() => {
      time.value = updateCurrentTime(res.data.timestamp)
    }, 1000)
  })
}

const updateCurrentTime = (timestamp) => {
  // 计算从初始时间戳到现在经过的时间
  const now = dayjs();
  const initialTime = dayjs(timestamp);
  const elapsedTime = now.diff(initialTime);

  // 使用初始时间戳加上经过的时间来更新当前时间
  const currentTime = initialTime.add(elapsedTime, 'millisecond');
  return currentTime.format('HH:mm:ss');
}
const init = () => {
  getRealTimeData()
  getWarningList()
  getLeastShortReport()
}


function changeSize () {
  // 基于设计稿 1920px 宽度的计算
  document.documentElement.style.fontSize = (document.documentElement.clientWidth * 10) / 1920 + 'px';
}


changeSize();
init()

onMounted(() => {
  autofit.init({
    dw: 1920,
    dh: 1080,
    el:"#screen-view",
    resize: true,
  })

  window.addEventListener('resize', changeSize, false);

  //每5s刷新数据
  timer = setInterval(() => {
    init()
  }, 1000 * 60 * 60)
})

onBeforeUnmount(() => {
  document.documentElement.style.fontSize = '';
  clearInterval(timer)
  clearInterval(timeHour)
  timer = null;
  timeHour = null
})

</script>
<template>
  <div class="screen-view" id="screen-view">
    <div class="screen-view-top">
      <div class="left">
        <span class="time">{{ time }}</span>
        <span class="line"></span>
        <div class="date">
          <div>{{ dayjs(screenData.timestamp).format("dddd") }}</div>
          <div class="text-[12px]">{{ dayjs(screenData.timestamp).format('YYYY-MM-DD') }}</div>
        </div>
      </div>
      <div class="chat-header flex items-center justify-end pr-[18px] space-x-[16px]">
        <userTop></userTop>
      </div>
    </div>
    <div class="menu">
      <RouterLink to="/" class="item item1">智慧总控</RouterLink>
      <RouterLink to="/answer" class="item item2">专家问答</RouterLink>
      <RouterLink to="/water-warn" class="item item3">工艺管控</RouterLink>
      <RouterLink to="/work" class="item item4">智能办公</RouterLink>
    </div>
    <div class="screen-container">
      <div class="screen-container-main">
        <div class="left">
          <WaterYield :screenData="screenData"></WaterYield>
          <WaterQuality :screenData="screenData"></WaterQuality>
        </div>
        <div class="middle">
          <middleBox :dataTime="dataTime"></middleBox>
        </div>
        <div class="right">
          <ContinueData :screenData="screenData"></ContinueData>
          <dataBox :reportData="reportData"></dataBox>
          <gongyi :gongyiData="gongyiData"></gongyi>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.screen-view {
  position: relative;
  display: flex;
  // justify-content: space-around;
  flex-flow: column;
  width: 100vw;
  height: 100vh;
  min-width: 1200px;
  min-height: 700px;
  background: url(@/assets/images/home/home_bg.png) center center no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  .screen-view-top {
    flex-shrink: 0;
    display: flex;
    align-items: end;
    justify-content: space-between;
    height: 90px;
    padding: 0 50px;

    .left {
      display: flex;
      align-items: center;
      font-size: 16px;
    }

    .time {
      width: 150px;
      font-size: 36px;
      font-weight: bold;
      text-align: left;
      color: #333;
    }

    .line {
      margin: 0 10px 0 10px;
      display: inline-block;
      background: #767C82;
      height: 28px;
      width: 2px;
    }

    .date {
      text-align: center;
      color: #415B73;
    }
  }

  .menu {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    height: 68px;

    .item {
      width: 218px;
      height: 52px;
      display: inline-block;
      font-size: 0;
    }

    .item1 {
      background: url('@/assets/images/home/menu_1_hover.png') no-repeat;
      background-size: 100% 100%;
    }

    .item2 {
      background: url('@/assets/images/home/menu_2.png') no-repeat;
      background-size: 100% 100%;
      margin-right: 20rem;

      &:hover {
        background: url('@/assets/images/home/menu_2_hover.png') no-repeat;
        background-size: 100% 100%;
      }
    }

    .item3 {
      background: url('@/assets/images/home/menu_3.png') no-repeat;
      background-size: 100% 100%;

      &:hover {
        background: url('@/assets/images/home/menu_3_hover.png') no-repeat;
        background-size: 100% 100%;
      }
    }

    .item4 {
      background: url('@/assets/images/home/menu_4.png') no-repeat;
      background-size: 100% 100%;

      &:hover {
        background: url('@/assets/images/home/menu_4_hover.png') no-repeat;
        background-size: 100% 100%;
      }
    }
  }

  .screen-container {
    position: relative;
    display: flex;
    flex-flow: column;
    padding: 0 60px;
    z-index: 2;

    &-main {
      position: relative;
      height: 100%;
      display: flex;
      justify-content: space-between;

      .left {
        display: flex;
        flex-flow: column;
      }

      .middle {
        position: relative;
        flex: 1;
      }
    }
  }

  .img-card {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 79rem;
    height: 59rem;
  }
}

@media screen and (max-width: 1440px) {
  .screen-view {
    justify-content: center;
  }
}
</style>

<style lang="scss">
.home-box {
  width: 54rem;
  border-radius: 0.8rem;

  &-top {
    padding: 0.7rem 2.4rem 0.7rem 1.2rem;
    display: flex;
    justify-content: space-between;
    position: relative;
    align-items: center;

    &::after {
      content: '';
      height: 0.2rem;
      background: url('@/assets/images/home/line.png') no-repeat;
      background-size: 100% 100%;
      width: 100%;
      position: absolute;
      left: 1.2rem;
      bottom: 0;
    }

    .title {
      font-size: 1.8rem;
      font-weight: bold;
      display: flex;
      align-items: center;

      &::before {
        content: '';
        width: 2rem;
        height: 2rem;
        background: url('@/assets/images/home/mark.png') no-repeat;
        background-size: cover;
        margin-right: 0.8rem;
        display: inline-block;
      }
    }
  }
}
</style>