<template>
  <div class="container">
    <!-- 统计卡片 -->
    <a-row :gutter="16" class="stats-row">
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-primary">
              <icon-eye />
            </div>
            <div class="stats-info">
              <div class="stats-title">监控总数</div>
              <div class="stats-value">{{ stats.total }}</div>
              <div class="stats-desc">活跃监控项目</div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-success">
              <icon-check-circle-fill />
            </div>
            <div class="stats-info">
              <div class="stats-title">正常运行</div>
              <div class="stats-value">{{ stats.normal }}</div>
              <div class="stats-desc">
                <a-tag color="green" size="small">{{ stats.normalPercent }}%</a-tag>
              </div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-warning">
              <icon-exclamation-circle-fill />
            </div>
            <div class="stats-info">
              <div class="stats-title">异常告警</div>
              <div class="stats-value">{{ stats.warning }}</div>
              <div class="stats-desc text-success">较昨日 -2</div>
            </div>
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :sm="12" :lg="6">
        <a-card class="stats-card" :bordered="false">
          <div class="stats-content">
            <div class="stats-icon stats-icon-primary">
              <icon-clock-circle />
            </div>
            <div class="stats-info">
              <div class="stats-title">平均响应</div>
              <div class="stats-value">{{ stats.avgResponse }}<span class="stats-unit">ms</span></div>
              <div class="stats-desc text-success">较昨日 -15ms</div>
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>

    <!-- 图表区域 -->
    <a-row :gutter="16" class="chart-row">
      <a-col :xs="24" :lg="12">
        <a-card title="响应时间趋势" :bordered="false">
          <template #extra>
            <a-space>
              <span class="legend-item">
                <span class="legend-dot legend-dot-1"></span>
                <span>平均</span>
              </span>
              <span class="legend-item">
                <span class="legend-dot legend-dot-2"></span>
                <span>P95</span>
              </span>
            </a-space>
          </template>
          <div class="chart-container">
            <Chart :options="responseTimeChartOptions" height="280px" />
          </div>
        </a-card>
      </a-col>
      <a-col :xs="24" :lg="12">
        <a-card title="可用性报告" :bordered="false">
          <template #extra>
            <span class="text-muted">本月可用性统计</span>
          </template>
          <div class="availability-list">
            <div v-for="item in availabilityData" :key="item.name" class="availability-item">
              <div class="availability-header">
                <span class="availability-name">{{ item.name }}</span>
                <span :class="['availability-value', `status-${item.status}`]">{{ item.uptime }}%</span>
              </div>
              <a-progress
                :percent="item.uptime"
                :status="item.progressStatus"
                :stroke-width="8"
                :show-text="false"
              />
            </div>
          </div>
        </a-card>
      </a-col>
    </a-row>

    <!-- 监控列表 -->
    <a-card title="监控列表" :bordered="false">
      <a-table
        :data="tableData"
        :columns="columns"
        :loading="loading"
        :pagination="false"
        row-key="name"
      >
        <!-- 状态列 -->
        <template #status="{ record }">
          <a-tag :color="getStatusColor(record.statusValue)" bordered>
            {{ record.statusText }}
          </a-tag>
        </template>

        <!-- URL列 -->
        <template #url="{ record }">
          <a :href="record.url" target="_blank" class="url-link">
            {{ record.url }}
            <icon-launch class="link-icon" />
          </a>
        </template>

        <!-- 趋势列 -->
        <template #trend="{ record }">
          <span :class="['trend-icon', record.trend > 0 ? 'trend-up' : 'trend-down']">
            <icon-arrow-rise v-if="record.trend > 0" />
            <icon-arrow-fall v-else />
          </span>
        </template>
      </a-table>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, computed, onMounted } from 'vue'
import {
  IconEye,
  IconCheckCircleFill,
  IconExclamationCircleFill,
  IconClockCircle,
  IconPlus,
  IconArrowRise,
  IconArrowFall,
  IconLaunch,
} from '@arco-design/web-vue/es/icon'
import Breadcrumb from '@/components/breadcrumb/index.vue'
import Chart from '@/components/chart/index.vue'
import type { TableColumnData } from '@arco-design/web-vue/es/table/interface'

// 统计数据
const stats = ref({
  total: 48,
  normal: 45,
  normalPercent: 94,
  warning: 3,
  avgResponse: 123,
})

const loading = ref(false)

// 表格列配置
const columns: TableColumnData[] = [
  {
    title: '名称',
    dataIndex: 'name',
    width: 150,
  },
  {
    title: 'URL',
    dataIndex: 'url',
    slotName: 'url',
    width: 280,
  },
  {
    title: '状态',
    dataIndex: 'status',
    slotName: 'status',
    width: 100,
    align: 'center',
  },
  {
    title: '响应时间',
    dataIndex: 'responseTime',
    width: 100,
    align: 'center',
  },
  {
    title: '可用率',
    dataIndex: 'uptime',
    width: 100,
    align: 'center',
  },
  {
    title: '最后检查',
    dataIndex: 'lastCheck',
    width: 120,
    align: 'center',
  },
  {
    title: '趋势',
    dataIndex: 'trend',
    slotName: 'trend',
    width: 80,
    align: 'center',
  },
]

// 表格数据
const tableData = ref([
  {
    name: '官网首页',
    url: 'https://www.example.com',
    statusValue: 'success',
    statusText: '正常',
    responseTime: '89ms',
    uptime: '99.98%',
    lastCheck: '1分钟前',
    trend: 1,
  },
  {
    name: 'API服务',
    url: 'https://api.example.com/health',
    statusValue: 'success',
    statusText: '正常',
    responseTime: '45ms',
    uptime: '99.99%',
    lastCheck: '1分钟前',
    trend: 1,
  },
  {
    name: '用户中心',
    url: 'https://user.example.com',
    statusValue: 'warning',
    statusText: '响应慢',
    responseTime: '2.3s',
    uptime: '98.5%',
    lastCheck: '1分钟前',
    trend: -1,
  },
  {
    name: '支付网关',
    url: 'https://pay.example.com',
    statusValue: 'success',
    statusText: '正常',
    responseTime: '120ms',
    uptime: '99.95%',
    lastCheck: '1分钟前',
    trend: 1,
  },
  {
    name: '管理后台',
    url: 'https://admin.example.com',
    statusValue: 'error',
    statusText: '不可达',
    responseTime: '超时',
    uptime: '95.2%',
    lastCheck: '2分钟前',
    trend: -1,
  },
  {
    name: '文件服务',
    url: 'https://files.example.com',
    statusValue: 'success',
    statusText: '正常',
    responseTime: '156ms',
    uptime: '99.8%',
    lastCheck: '1分钟前',
    trend: 1,
  },
])

// 可用性数据
const availabilityData = ref([
  { name: '官网首页', uptime: 99.98, status: 'success', progressStatus: 'success' as const },
  { name: 'API服务', uptime: 99.99, status: 'success', progressStatus: 'success' as const },
  { name: '用户中心', uptime: 98.5, status: 'warning', progressStatus: 'warning' as const },
  { name: '管理后台', uptime: 95.2, status: 'error', progressStatus: 'danger' as const },
])

// 响应时间图表配置
const responseTimeChartOptions = ref({
  tooltip: {
    trigger: 'axis',
  },
  grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true,
  },
  xAxis: {
    type: 'category',
    boundaryGap: false,
    data: ['00:00', '04:00', '08:00', '12:00', '16:00', '20:00', '24:00'],
  },
  yAxis: {
    type: 'value',
    name: 'ms',
  },
  series: [
    {
      name: '平均',
      type: 'line',
      smooth: true,
      data: [95, 88, 145, 220, 180, 130, 92],
      areaStyle: {
        opacity: 0.1,
      },
      lineStyle: {
        width: 2,
        color: '#165DFF', // ArcoDesign primary color
      },
      itemStyle: {
        color: '#165DFF',
      },
    },
    {
      name: 'P95',
      type: 'line',
      smooth: true,
      data: [120, 105, 180, 250, 210, 155, 115],
      areaStyle: {
        opacity: 0.1,
      },
      lineStyle: {
        width: 2,
        color: '#FF7D00', // ArcoDesign warning color
      },
      itemStyle: {
        color: '#FF7D00',
      },
    },
  ],
})

// 获取状态颜色
const getStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    success: 'green',
    warning: 'orange',
    error: 'red',
  }
  return colorMap[status] || 'gray'
}

// 获取数据
const fetchData = async () => {
  // TODO: 从API获取数据
  loading.value = false
}

// 初始化
onMounted(() => {
  fetchData()
})
</script>

<script lang="ts">
export default {
  name: 'URLMonitor',
}
</script>

<style scoped lang="less">
.container {
  padding: 16px;
}

.stats-row {
  margin-bottom: 16px;
}

.stats-card {
  height: 100%;

  :deep(.arco-card-body) {
    padding: 16px;
  }

  .stats-content {
    display: flex;
    align-items: flex-start;
    gap: 12px;
  }

  .stats-icon {
    width: 48px;
    height: 48px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    flex-shrink: 0;

    &.stats-icon-primary {
      background-color: rgba(var(--primary-1), 1);
      color: rgb(var(--primary-6));
    }

    &.stats-icon-success {
      background-color: rgba(var(--green-1), 1);
      color: rgb(var(--green-6));
    }

    &.stats-icon-warning {
      background-color: rgba(var(--orange-1), 1);
      color: rgb(var(--orange-6));
    }
  }

  .stats-info {
    flex: 1;
    min-width: 0;
  }

  .stats-title {
    font-size: 14px;
    color: var(--color-text-3);
    margin-bottom: 4px;
  }

  .stats-value {
    font-size: 24px;
    font-weight: 600;
    color: var(--color-text-1);
    line-height: 1.2;
  }

  .stats-unit {
    font-size: 14px;
    font-weight: normal;
    margin-left: 2px;
  }

  .stats-desc {
    margin-top: 4px;
    font-size: 12px;
    color: var(--color-text-3);
  }
}

.text-success {
  color: rgb(var(--success-6));
}

.text-muted {
  color: var(--color-text-3);
  font-size: 12px;
}

.chart-row {
  margin-bottom: 16px;

  .arco-card {
    height: 100%;
  }

  .chart-container {
    width: 100%;
    height: 280px;
  }
}

.legend-item {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  color: var(--color-text-3);
  margin-left: 16px;

  .legend-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 6px;

    &.legend-dot-1 {
      background-color: #165DFF;
    }

    &.legend-dot-2 {
      background-color: #FF7D00;
    }
  }
}

.availability-list {
  height: 280px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 8px 0;

  .availability-item {
    .availability-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 8px;

      .availability-name {
        font-size: 14px;
        color: var(--color-text-2);
      }

      .availability-value {
        font-size: 14px;
        font-weight: 500;

        &.status-success {
          color: rgb(var(--success-6));
        }

        &.status-warning {
          color: rgb(var(--warning-6));
        }

        &.status-error {
          color: rgb(var(--danger-6));
        }
      }
    }
  }
}

.url-link {
  color: rgb(var(--primary-6));
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 4px;

  &:hover {
    text-decoration: underline;
  }

  .link-icon {
    font-size: 12px;
    opacity: 0;
    transition: opacity 0.2s;
  }

  &:hover .link-icon {
    opacity: 1;
  }
}

.trend-icon {
  font-size: 16px;

  &.trend-up {
    color: rgb(var(--success-6));
  }

  &.trend-down {
    color: rgb(var(--danger-6));
  }
}
</style>