<script lang="ts" setup>
import { DESIGN_HEIGHT, DESIGN_WIDTH } from '@/utils/kb'
import hsummary from './kb/hsummary.vue'
import hdeviceType from './kb/hdeviceType.vue'
import dayfinish from './kb/dayfinish.vue'
import hsafe from './kb/hsafe.vue'
import hdeviceStatus from './kb/hdeviceStatus.vue'
import horderTrend from './kb/horderTrend.vue'

const company = ref('首页')

const wrapperRef = ref<HTMLDivElement>()
const scale = ref(1)

let resizeObserver: ResizeObserver | null = null
let resizeRaf = 0

provide('homeKbScale', scale)

const targetWrapperStyle = computed(() => ({
  width: `${DESIGN_WIDTH * scale.value}px`,
  height: `${DESIGN_HEIGHT * scale.value}px`
}))

const targetAreaStyle = computed(() => {
  return {
    '--kb-scale': scale.value,
    width: `${DESIGN_WIDTH * scale.value}px`,
    height: `${DESIGN_HEIGHT * scale.value}px`
  }
})

function updateScale() {
  cancelAnimationFrame(resizeRaf)

  resizeRaf = requestAnimationFrame(() => {
    const wrapper = wrapperRef.value
    if (!wrapper) return

    const { clientWidth, clientHeight } = wrapper
    if (!clientWidth || !clientHeight) return

    scale.value = Math.min(clientWidth / DESIGN_WIDTH, clientHeight / DESIGN_HEIGHT)
    nextTick(() => {
      window.dispatchEvent(new Event('homekb:resize'))
    })
  })
}

onMounted(() => {
  nextTick(updateScale)
  resizeObserver = new ResizeObserver(updateScale)
  if (wrapperRef.value) {
    resizeObserver.observe(wrapperRef.value)
  }
  window.addEventListener('resize', updateScale)
})

onUnmounted(() => {
  resizeObserver?.disconnect()
  window.removeEventListener('resize', updateScale)
  cancelAnimationFrame(resizeRaf)
})
</script>
<template>
  <div ref="wrapperRef" class="bg absolute top-0 left-0 size-full z-10">
    <div class="mx-a overflow-hidden" :style="targetWrapperStyle">
      <div class="bg kb-screen" :style="targetAreaStyle">
        <header class="header">{{ company }}</header>
        <div class="kb-content">
          <hsummary class="kb-stage-card kb-stage-card--1" />
          <div class="home-top-grid">
            <!-- <hmttr class="kb-stage-card kb-stage-card--2" /> -->
            <hsafe class="row-span-2 kb-stage-card kb-stage-card--3" />
            <hdeviceType class="row-span-2 kb-stage-card kb-stage-card--4" />
            <dayfinish class="row-span-2 kb-stage-card kb-stage-card--5" />
          </div>
          <div class="home-bottom-grid">
            <hdeviceStatus class="kb-stage-card kb-stage-card--6" />
            <horderTrend class="kb-stage-card kb-stage-card--7" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
@import url('@/styles/kb.scss');

.kb-screen {
  overflow: hidden;
}

.kb-content {
  padding: calc(12px * var(--kb-scale)) calc(20px * var(--kb-scale)) 0;
}

.home-top-grid,
.home-bottom-grid {
  display: grid;
  width: 100%;
  margin-top: calc(12px * var(--kb-scale));
  gap: calc(12px * var(--kb-scale));
}

.home-top-grid {
  height: calc(384px * var(--kb-scale));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.home-bottom-grid {
  height: calc(428px * var(--kb-scale));
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
</style>
