<script setup>
import { userTop } from '@/components';
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 { screenApi } from "@/api/screen"
import dayjs from 'dayjs'
import 'dayjs/locale/zh-cn';

dayjs.locale('zh-cn'); // 设置day.js使用中文/ 导入中文语言包
import { ref, onBeforeUnmount, onMounted } from "vue";
const gongyiData = ref([])
const reportData = ref({})
const screenData = ref({})
const time = ref('')
let timer = null
let timeHour = null

const dataTime = `数据更新时间：${dayjs().format('MM-DD HH:mm')}`

// 获取工艺管控 助手
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
    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()
}


init()

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

onBeforeUnmount(() => {
  clearInterval(timer)
  clearInterval(timeHour)
  timer = null;
  timeHour = null
})

</script>
<template>
  <div class="screen-view">
    <div class="screen-view-top">
      <div class="left">
        <span class="time">{{ time }}</span>
        <span class="line"></span>
        <div class="data">
          <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="screen-container">
      <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="/helper" class="item item4">智能助手</RouterLink>
      </div>
      <div class="screen-container-main">
        <div class="left">
          <shuizhi :screenData="screenData"></shuizhi>
          <liuliang :screenData="screenData"></liuliang>
        </div>
        <div class="middle">
          <middleBox :dataTime="dataTime"></middleBox>
        </div>
        <div class="right">
          <dataBox :reportData="reportData"></dataBox>
          <gongyi :gongyiData="gongyiData"></gongyi>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.screen-view {
  width: 100vw;
  height: 100vh;
  background: url(@/assets/images/home/home_bg.png) no-repeat;
  background-size: 100% 100%;
  overflow: hidden;

  &-top {
    display: flex;
    justify-content: space-between;
    padding: 3.8rem 2rem 3.2rem 3.2rem;
    height: 3.6rem;

    .left {
      display: flex;
      align-items: center;
      font-size: 1.6rem;
      background: #415B73;
      flex: 1;
    }

    .time {
      font-size: 3.6rem;
      font-weight: bold;
      color: #333;
      width: 14rem;
    }

    .line {
      margin: 0 1rem 0 1.5rem;
      display: inline-block;
      background: #767C82;
      height: 2.8rem;
      width: 0.2rem;
    }

    .right {
      flex: 1;
      display: flex;
    }
  }

  .screen-container {
    padding: 0 6rem;

    .menu {
      display: flex;
      align-content: center;
      justify-content: center;

      .item {
        width: 21.8rem;
        height: 5.2rem;
        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%;
        }
      }

    }

    &-main {
      display: flex;
    }

    .left,
    .right,
    .middle {
      flex: 1;
    }

    .right {
      flex: 1;
    }
  }



}
</style>

<style lang="scss">
.home-box {
  background: rgba(255, 255, 255, 0.8);
  width: 54rem;
  height: 42rem;
  border-radius: 0.8rem;
  border: 0.1rem solid #fff;
  margin-top: 2rem;

  &-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>