<template>
  <div class="cache-page">
    <div class="info-grid">
      <div class="info-cell" v-for="item in infoItems" :key="item.label">
        <span class="info-cell-label">{{ item.label }}</span>
        <span v-if="cache.info || cache.dbSize !== undefined" class="info-cell-value">{{ item.value() }}</span>
        <el-skeleton v-else animated :rows="1" />
      </div>
    </div>

    <el-row :gutter="20" class="chart-row">
      <el-col :span="12">
        <el-card shadow="hover" class="chart-card">
          <template #header>
            <div class="chart-card-header">
              <el-icon class="chart-card-icon">
                <PieChart />
              </el-icon>
              <span>命令统计</span>
            </div>
          </template>
          <div ref="commandstats" class="chart-container" />
        </el-card>
      </el-col>
      <el-col :span="12">
        <el-card shadow="hover" class="chart-card">
          <template #header>
            <div class="chart-card-header">
              <el-icon class="chart-card-icon">
                <Odometer />
              </el-icon>
              <span>内存信息</span>
            </div>
          </template>
          <div ref="usedmemory" class="chart-container" />
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup name="Cache" lang="ts">
import { getCache } from '@/api/monitor/cache';
import * as echarts from 'echarts';
import { CacheVO } from '@/api/monitor/cache/types';

const cache = ref<Partial<CacheVO>>({});
const commandstats = ref();
const usedmemory = ref();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const infoItems = computed(() => [
  { label: 'Redis版本', value: () => cache.value.info?.redis_version || '-' },
  { label: '运行模式', value: () => cache.value.info?.redis_mode === 'standalone' ? '单机' : '集群' },
  { label: '端口', value: () => cache.value.info?.tcp_port || '-' },
  { label: '客户端数', value: () => cache.value.info?.connected_clients ?? '-' },
  { label: '运行时间(天)', value: () => cache.value.info?.uptime_in_days ?? '-' },
  { label: '使用内存', value: () => cache.value.info?.used_memory_human || '-' },
  { label: '使用CPU', value: () => cache.value.info ? parseFloat(cache.value.info.used_cpu_user_children).toFixed(2) : '-' },
  { label: '内存配置', value: () => cache.value.info?.maxmemory_human || '-' },
  { label: 'AOF是否开启', value: () => cache.value.info?.aof_enabled === '0' ? '否' : '是' },
  { label: 'RDB是否成功', value: () => cache.value.info?.rdb_last_bgsave_status || '-' },
  { label: 'Key数量', value: () => cache.value.dbSize ?? '-' },
  { label: '网络入口/出口', value: () => cache.value.info ? `${cache.value.info.instantaneous_input_kbps}kps/${cache.value.info.instantaneous_output_kbps}kps` : '-' }
]);

const getList = async () => {
  proxy?.$modal.loading('正在加载缓存监控数据，请稍候！');
  const res = await getCache();
  proxy?.$modal.closeLoading();
  cache.value = res.data;
  const commandstatsIntance = echarts.init(commandstats.value, 'macarons');
  commandstatsIntance.setOption({
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    series: [
      {
        name: '命令',
        type: 'pie',
        roseType: 'radius',
        radius: [15, 95],
        center: ['50%', '38%'],
        data: res.data.commandStats,
        animationEasing: 'cubicInOut',
        animationDuration: 1000
      }
    ]
  });
  const usedmemoryInstance = echarts.init(usedmemory.value, 'macarons');
  usedmemoryInstance.setOption({
    tooltip: {
      formatter: '{b} <br/>{a} : ' + cache.value.info.used_memory_human
    },
    series: [
      {
        name: '峰值',
        type: 'gauge',
        min: 0,
        max: 1000,
        detail: {
          formatter: cache.value.info.used_memory_human
        },
        data: [
          {
            value: parseFloat(cache.value.info.used_memory_human),
            name: '内存消耗'
          }
        ]
      }
    ]
  });
  window.addEventListener('resize', () => {
    commandstatsIntance.resize();
    usedmemoryInstance.resize();
  });
};

onMounted(() => {
  getList();
});
</script>

<style scoped lang="scss">
.cache-page {
  padding: 24px 28px;
  background: #f7f8fc;
  min-height: calc(100vh - 100px);
}

/* ==================== 信息网格 ==================== */
.info-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 28px;
}

.info-cell {
  background: #fff;
  border-radius: 14px;
  padding: 18px 22px;
  border: 1px solid #f0f2f5;
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: box-shadow 0.25s, border-color 0.25s;

  &:hover {
    border-color: #e0e5ea;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.05);
  }
}

.info-cell-label {
  font-size: 12px;
  font-weight: 500;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-cell-value {
  font-size: 18px;
  font-weight: 700;
  color: #1f2937;
  word-break: break-all;
}

/* ==================== 图表卡片 ==================== */
.chart-row {
  margin: 0 !important;
}

.chart-card {
  border-radius: 14px !important;
  border: 1px solid #f0f2f5 !important;

  :deep(.el-card__header) {
    padding: 18px 24px;
    border-bottom: 1px solid #f3f4f6;
  }

  :deep(.el-card__body) {
    padding: 16px 20px 20px;
  }
}

.chart-card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  font-weight: 600;
  color: #1f2937;
}

.chart-card-icon {
  font-size: 18px;
  color: #3b82f6;
}

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

/* ==================== Skeleton ==================== */
:deep(.el-skeleton) {
  padding: 0;
}

/* ==================== Responsive ==================== */
@media (max-width: 1200px) {
  .info-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .cache-page {
    padding: 16px;
  }

  .info-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }

  .chart-row {
    .el-col {
      flex: 0 0 100%;
      max-width: 100%;
      margin-bottom: 16px;
    }
  }
}
</style>
