<template>
  <div class="dashboard-container" ref="dashboardContainer">
    <!-- DataV顶部标题栏 -->
    <DashboardHeader :active-tab="activeTab" @tab-change="handleTabChange" />

    <!-- 全屏按钮 -->
    <div class="fullscreen-btn" @click="toggleFullscreen">
      <svg v-if="!isFullscreen" viewBox="0 0 24 24" width="24" height="24">
        <path fill="currentColor" d="M7 14H5v5h5v-2H7v-3zm-2-4h2V7h3V5H5v5zm12 7h-3v2h5v-5h-2v3zM14 5v2h3v3h2V5h-5z"/>
      </svg>
      <svg v-else viewBox="0 0 24 24" width="24" height="24">
        <path fill="currentColor" d="M5 16h3v3h2v-5H5v2zm3-8H5v2h5V5H8v3zm6 11h2v-3h3v-2h-5v5zm2-11V5h-2v5h5V8h-3z"/>
      </svg>
    </div>

    <!-- 根据当前标签显示不同内容 -->
    <div v-if="activeTab === 'overview'" class="main-content">
      <!-- 原有数据总览内容 -->
      <div class="left-section">
        <dv-border-box-1>
          <MainKPI />
        </dv-border-box-1>
      </div>
      <div class="center-section">
        <AlarmTrend />
        <CameraStats />
      </div>
      <div class="right-section">
        <dv-border-box-1>
          <EventHotSpots />
          <AlgorithmStats />
        </dv-border-box-1>
      </div>
    </div>

    <!-- 监控视角内容 -->
    <div v-if="activeTab === 'monitor'" class="monitor-content">
      <!-- 监控列表 -->
      <MonitorDetail :visible="showMonitorDetail" :monitor="currentMonitor" @close="closeMonitorDetail" />
      <div class="monitor-list">
        <div class="monitor-grid">
          <div v-for="monitor in monitors" :key="monitor.id">
            <MonitorThumbnail :monitor="monitor" @click="selectMonitor(monitor)" />
          </div>
        </div>
      </div>
    </div>
    <!-- 算法中心内容 -->
    <div v-if="activeTab === 'algorithms'" class="algorithm-content">
      <AlgorithmCenter />
    </div>

    <!-- 告警管理 -->
    <div v-if="activeTab === 'alarms'" class="alarm-content">
      <AlarmManagement />
    </div>

    <!-- 事件中心  -->
    <div v-if="activeTab === 'events'" class="event-content">
      <EventCenter />
    </div>
    <!-- 设备管理 -->
     <div v-if="activeTab === 'devices'" class="device-content">
      <DeviceManagement />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import MainKPI from './components/MainKPI.vue'
import AlarmTrend from './components/AlarmTrend.vue'
import EventHotSpots from './components/EventHotSpots.vue'
import AlgorithmStats from './components/AlgorithmStats.vue'
import CameraStats from './components/CameraStats.vue'
import DashboardHeader from './components/DashboardHeader.vue'
import MonitorThumbnail from './components/monitor/MonitorThumbnail.vue'
import MonitorDetail from './components/monitor/MonitorDetail.vue'
import AlgorithmCenter from './components/algorithm/AlgorithmCenter.vue'
import AlarmManagement from './components/alarm/AlarmManagement.vue'
import EventCenter from './components/event/EventCenter.vue'
import DeviceManagement from './components/device/DeviceManagement.vue' 

const activeTab = ref('overview')
const dashboardContainer = ref(null)
const isFullscreen = ref(false)

const showMonitorDetail = ref(false)

const currentMonitor = ref({
  id: 1,
  name: '港口区主监控',
  location: '港口区', // 与图片一致
  status: 'online',
  videoSrc: '/videos/port-main.mp4'
})

const monitors = ref([
  {
    id: 1,
    name: '港口区监控1',
    location: '港口A区',
    status: 'online',
    videoSrc: '/videos/port-1.mp4',
    detections: [
      { type: 'person', x: 25, y: 35, width: 40, height: 80 }
    ]
  },
  {
    id: 2,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },
  {
    id: 3,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },
  {
    id: 4,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },
  {
    id: 5,
    name: '港口区监控1',
    location: '港口A区',
    status: 'online',
    videoSrc: '/videos/port-1.mp4',
    detections: [
      { type: 'person', x: 25, y: 35, width: 40, height: 80 }
    ]
  },
  {
    id: 6,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },
  {
    id: 7,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },
  {
    id: 8,
    name: '港口区监控2',
    location: '港口B区',
    status: 'online',
    videoSrc: '/videos/port-2.mp4',
    detections: [
      { type: 'vehicle', x: 40, y: 50, width: 80, height: 60 }
    ]
  },

])


// 切换标签
const handleTabChange = (tab) => {
  console.log(tab)
  activeTab.value = tab
}

// 选择监控画面
const selectMonitor = (monitor) => {
  currentMonitor.value = monitor
  showMonitorDetail.value = true
}

// 关闭大屏监控
const closeMonitorDetail = () => {
  showMonitorDetail.value = false
}

// 切换全屏
const toggleFullscreen = () => {
  if (!document.fullscreenElement) {
    dashboardContainer.value.requestFullscreen().catch(err => {
      console.error(`全屏错误: ${err.message}`)
    })
  } else {
    if (document.exitFullscreen) {
      document.exitFullscreen()
    }
  }
}

// 监听全屏状态变化
const handleFullscreenChange = () => {
  isFullscreen.value = !!document.fullscreenElement
}

// 添加事件监听
onMounted(() => {
  document.addEventListener('fullscreenchange', handleFullscreenChange)
})

// 移除事件监听
onUnmounted(() => {
  document.removeEventListener('fullscreenchange', handleFullscreenChange)
})
</script>

<style lang="scss">
.dashboard-container {
  width: 100%;
  height: 100vh;
  background-color: #0a1d3c;
  color: #fff;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  position: relative;

  .fullscreen-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    background: rgba(16, 42, 87, 0.6);
    border: 1px solid #7cd0ff;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 1000;
    color: #7cd0ff;
    transition: all 0.3s;

    &:hover {
      background: rgba(124, 208, 255, 0.2);
      color: white;
    }
  }

  .main-content {
    display: flex;
    flex: 1;
    gap: 10px;
    margin-top: 10px;

    .left-section {
      width: 25%;
      display: flex;
      flex-direction: column;
      gap: 20px;
      height: 84vh;
    }

    .center-section {
      height: 84vh;
      display: flex;
      flex-direction: column;
      flex: 1;
    }

    .right-section {
      height: 84vh;
      width: 25%;
      display: flex;
      flex-direction: column;
      gap: 20px;
    }
  }

  /* 监控视角样式 */
  .monitor-content {
    overflow-y: auto;
    scrollbar-width: none;

    &::-webkit-scrollbar {
      display: none;
    }

    .monitor-list {
      width: 100%;
      padding: 20px;

      .monitor-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 15px;

        >div {
          aspect-ratio: 16/9;
          border-radius: 4px;
          overflow: hidden;
        }
      }
    }
  }

  /* 设备管理样式 */
  .device-content {
    overflow: hidden;
    background-color: rgba(16, 42, 87, 0.3);
  }


  .algorithm-content {
    overflow-y: auto;
    scrollbar-width: none;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  .alarm-content {
    height: calc(100vh - 60px);
    overflow: hidden;
  }
  .event-content {
    height: calc(100vh - 60px);
    overflow: hidden;
  }
}

/* 全屏样式 */
:fullscreen .dashboard-container {
  background-color: #0a1d3c;
  padding: 20px;
}

:fullscreen .main-content {
  height: calc(100vh - 80px);
}

:fullscreen .monitor-content,
:fullscreen .algorithm-content,
:fullscreen .alarm-content,
:fullscreen .event-content {
  height: calc(100vh - 80px);
}
</style>