<template>
  <div class="home-page">
    <div class="page-header">
      <h1>太湖流域水环境综合治理信息共享平台</h1>
      <p class="subtitle">汇聚八家单位监测数据，实现多方数据共享与决策支持</p>
    </div>

    <div class="stats-row">
      <div class="stat-card">
        <div class="stat-icon">
          <svg-icon icon-class="server" />
        </div>
        <div class="stat-content">
          <div class="stat-value">8</div>
          <div class="stat-label">合作单位</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">
          <svg-icon icon-class="chart" />
        </div>
        <div class="stat-content">
          <div class="stat-value">40+</div>
          <div class="stat-label">业务类型</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">
          <svg-icon icon-class="table" />
        </div>
        <div class="stat-content">
          <div class="stat-value">2000+</div>
          <div class="stat-label">监测站点</div>
        </div>
      </div>
      <div class="stat-card">
        <div class="stat-icon">
          <svg-icon icon-class="monitor" />
        </div>
        <div class="stat-content">
          <div class="stat-value">实时</div>
          <div class="stat-label">数据更新</div>
        </div>
      </div>
    </div>

    <div class="section">
      <h2>共享清单</h2>
      <div class="unit-grid">
        <div class="unit-card" v-for="unit in units" :key="unit.code">
          <div class="unit-header">
            <div class="unit-icon" :style="{ background: unit.color }">
              <svg-icon :icon-class="unit.icon" />
            </div>
            <div class="unit-info">
              <h3>{{ unit.name }}</h3>
              <p class="unit-code">{{ unit.code }}</p>
            </div>
          </div>
          <div class="unit-stats">
            <div class="unit-stat">
              <span class="stat-num">{{ unit.dataCount }}</span>
              <span class="stat-text">共享数据集</span>
            </div>
            <div class="unit-stat">
              <span class="stat-num">{{ unit.stationCount }}</span>
              <span class="stat-text">监测站点</span>
            </div>
          </div>
          <div class="unit-tags">
            <el-tag v-for="tag in unit.tags" :key="tag" size="small" :type="getTagType(tag)">{{ tag }}</el-tag>
          </div>
          <el-button type="primary" size="small" @click="viewUnit(unit)">查看详情</el-button>
        </div>
      </div>
    </div>

    <el-dialog :title="unitDetailTitle" v-model="unitDialogVisible" width="900px" append-to-body>
      <div class="unit-detail-content">
        <div class="unit-detail-header">
          <div class="detail-icon" :style="{ background: selectedUnit?.color }">
            <svg-icon :icon-class="selectedUnit?.icon" />
          </div>
          <div class="detail-info">
            <h3>{{ selectedUnit?.name }}</h3>
            <p>{{ selectedUnit?.code }}</p>
          </div>
          <div class="detail-stats">
            <div class="detail-stat">
              <span class="num">{{ bizList.length }}</span>
              <span class="label">业务域数量</span>
            </div>
            <div class="detail-stat">
              <span class="num">{{ selectedUnit?.stationCount }}</span>
              <span class="label">监测站点</span>
            </div>
          </div>
        </div>
        
        <div class="biz-section">
          <h4>业务域列表</h4>
          <div class="biz-card-grid">
            <div class="biz-card" v-for="biz in bizList" :key="biz.bizCode">
              <div class="biz-header">
                <span class="biz-name">{{ biz.bizName }}</span>
                <el-tag size="small" :type="getBizTypeTag(biz.bizType)">{{ biz.bizType }}</el-tag>
              </div>
              <div class="biz-info">
                <div class="info-item">
                  <span class="info-label">共享频率</span>
                  <span class="info-value">{{ biz.dataFreq || '-' }}</span>
                </div>
              </div>
              <div class="biz-note">
                <span class="note-label">数据说明</span>
                <p class="note-content">{{ biz.bizNote || '暂无说明' }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import request from '@/utils/request'

const units = ref([
  {
    code: 'TBA',
    name: '水利部太湖流域管理局',
    icon: 'server',
    color: '#217ff4',
    dataCount: 12,
    stationCount: 156,
    tags: ['水文', '水质', '工情']
  },
  {
    code: 'THDH',
    name: '生态环境部太湖流域东海海域生态环境监督管理局',
    icon: 'chart',
    color: '#10b981',
    dataCount: 8,
    stationCount: 98,
    tags: ['水质', '生态']
  },
  {
    code: 'JS_SL',
    name: '江苏省水利厅',
    icon: 'table',
    color: '#f59e0b',
    dataCount: 15,
    stationCount: 420,
    tags: ['水文', '工情']
  },
  {
    code: 'JS_ST',
    name: '江苏省生态环境厅',
    icon: 'monitor',
    color: '#ef4444',
    dataCount: 10,
    stationCount: 280,
    tags: ['水质', '生态']
  },
  {
    code: 'ZJ_SL',
    name: '浙江省水利厅',
    icon: 'server',
    color: '#8b5cf6',
    dataCount: 11,
    stationCount: 350,
    tags: ['水文', '工情']
  },
  {
    code: 'ZJ_ST',
    name: '浙江省生态环境厅',
    icon: 'chart',
    color: '#ec4899',
    dataCount: 9,
    stationCount: 220,
    tags: ['水质', '生态']
  },
  {
    code: 'SH_SW',
    name: '上海市水务局',
    icon: 'table',
    color: '#06b6d4',
    dataCount: 6,
    stationCount: 180,
    tags: ['水文', '工情']
  },
  {
    code: 'SH_ST',
    name: '上海市生态环境局',
    icon: 'monitor',
    color: '#84cc16',
    dataCount: 7,
    stationCount: 150,
    tags: ['水质', '生态']
  }
])

const unitDialogVisible = ref(false)
const selectedUnit = ref(null)
const bizList = ref([])

const unitDetailTitle = computed(() => {
  return selectedUnit.value ? `${selectedUnit.value.name} - 业务域详情` : '单位详情'
})

function getTagType(tag) {
  const types = {
    '水文': 'primary',
    '水质': 'success',
    '工情': 'warning',
    '生态': 'danger'
  }
  return types[tag] || 'info'
}

function getBizTypeTag(bizType) {
  const types = {
    '水位': 'primary',
    '水质': 'success',
    '调度': 'warning',
    '工程': 'danger'
  }
  return types[bizType] || 'info'
}

function viewUnit(unit) {
  selectedUnit.value = unit
  loadBizList(unit.code)
  unitDialogVisible.value = true
}

function loadBizList(unitCode) {
  request({
    url: '/gx/biz/front/list',
    method: 'get',
    params: { bizUnit: unitCode }
  }).then(res => {
    bizList.value = res.data || []
  })
}
</script>

<style scoped>
.home-page {
  max-width: 1400px;
  margin: 0 auto;
}

.page-header {
  text-align: center;
  padding: 30px 0;
  margin-bottom: 30px;
  background: linear-gradient(135deg, #0066cc 0%, #0099ff 100%);
  border-radius: 12px;
  color: #fff;
}

.page-header h1 {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 10px 0;
}

.subtitle {
  font-size: 16px;
  opacity: 0.9;
  margin: 0;
}

.stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.stat-icon {
  width: 60px;
  height: 60px;
  background: #e8f4ff;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stat-icon :deep(svg) {
  width: 30px;
  height: 30px;
  color: #0066cc;
}

.stat-content {
  flex: 1;
}

.stat-value {
  font-size: 28px;
  font-weight: bold;
  color: #0066cc;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.section {
  margin-bottom: 30px;
}

.section h2 {
  font-size: 20px;
  font-weight: bold;
  margin: 0 0 20px 0;
  padding-left: 10px;
  border-left: 4px solid #0066cc;
}

.unit-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
}

.unit-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.unit-header {
  display: flex;
  align-items: center;
  gap: 15px;
  margin-bottom: 15px;
}

.unit-icon {
  width: 50px;
  height: 50px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.unit-icon :deep(svg) {
  width: 25px;
  height: 25px;
  color: #fff;
}

.unit-info h3 {
  font-size: 15px;
  font-weight: bold;
  margin: 0 0 5px 0;
  color: #333;
}

.unit-code {
  font-size: 12px;
  color: #999;
  margin: 0;
}

.unit-stats {
  display: flex;
  gap: 20px;
  margin-bottom: 15px;
}

.unit-stat {
  text-align: center;
}

.stat-num {
  display: block;
  font-size: 20px;
  font-weight: bold;
  color: #0066cc;
}

.stat-text {
  font-size: 12px;
  color: #999;
}

.unit-tags {
  margin-bottom: 15px;
}

.data-overview {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
}

.overview-card {
  background: #fff;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  border-top: 4px solid #0066cc;
}

.overview-card.water-quality {
  border-top-color: #10b981;
}

.overview-card.water-level {
  border-top-color: #3b82f6;
}

.overview-card.ecology {
  border-top-color: #f59e0b;
}

.overview-card h3 {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 15px 0;
}

.overview-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.indicator {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background: #f8fafc;
  border-radius: 8px;
}

.indicator .label {
  font-size: 14px;
  color: #666;
}

.indicator .value {
  font-size: 14px;
  font-weight: bold;
}

.value.normal {
  color: #10b981;
}

.value.warning {
  color: #f59e0b;
}

.value.danger {
  color: #ef4444;
}

.unit-detail-content {
  padding: 10px 0;
}

.unit-detail-header {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px;
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  border-radius: 12px;
  margin-bottom: 20px;
}

.detail-icon {
  width: 60px;
  height: 60px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.detail-icon :deep(svg) {
  width: 30px;
  height: 30px;
  color: #fff;
}

.detail-info h3 {
  font-size: 18px;
  font-weight: bold;
  margin: 0 0 5px 0;
  color: #1f1f1f;
}

.detail-info p {
  font-size: 14px;
  color: #646464;
  margin: 0;
}

.detail-stats {
  margin-left: auto;
  display: flex;
  gap: 30px;
}

.detail-stat {
  text-align: center;
}

.detail-stat .num {
  display: block;
  font-size: 24px;
  font-weight: bold;
  color: #1c97e7;
}

.detail-stat .label {
  font-size: 12px;
  color: #909399;
}

.biz-section h4 {
  font-size: 16px;
  font-weight: bold;
  margin: 0 0 15px 0;
  color: #1f1f1f;
}

.biz-card-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 15px;
}

.biz-card {
  background: #f8fafc;
  border-radius: 10px;
  padding: 16px;
  border: 1px solid #e4e7ed;
  transition: all 0.3s ease;
}

.biz-card:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  border-color: #dcdfe6;
}

.biz-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.biz-name {
  font-size: 15px;
  font-weight: bold;
  color: #303133;
}

.biz-info {
  display: flex;
  gap: 20px;
  margin-bottom: 12px;
}

.info-item {
  flex: 1;
}

.info-label {
  font-size: 12px;
  color: #909399;
  margin-right: 8px;
}

.info-value {
  font-size: 13px;
  color: #606266;
  font-weight: 500;
}

.biz-note {
  padding-top: 12px;
  border-top: 1px dashed #e4e7ed;
}

.note-label {
  font-size: 12px;
  color: #909399;
  margin-right: 8px;
}

.note-content {
  font-size: 13px;
  color: #606266;
  margin: 0;
  line-height: 1.6;
  word-break: break-all;
}
</style>
