<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import axios from 'axios'

const configs = ref<any[]>([])
const loading = ref(false)

onMounted(async () => {
  await loadConfigs()
})

async function loadConfigs() {
  loading.value = true
  try {
    const response = await axios.get('/api/admin/configs')
    if (response.data.code === 0) {
      configs.value = response.data.data || []
    }
  } catch (error) {
    console.error('Load configs error:', error)
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

function formatTime(timeStr: string) {
  if (!timeStr) return '-'
  return timeStr.split(' +')[0] || timeStr
}

// 按类型分组
const groupedConfigs = computed(() => {
  const groups: Record<string, any[]> = {}
  configs.value.forEach(config => {
    const type = config.config_type || '其他'
    if (!groups[type]) {
      groups[type] = []
    }
    groups[type].push(config)
  })
  return groups
})
</script>

<script lang="ts">
import { computed } from 'vue'
</script>

<template>
  <div class="configs-page">
    <!-- 页面头部 -->
    <div class="page-header">
      <h2>配置管理</h2>
      <el-button type="primary" @click="loadConfigs" :loading="loading">
        <el-icon><Refresh /></el-icon>
        刷新
      </el-button>
    </div>

    <!-- 配置列表 -->
    <div class="configs-grid" v-loading="loading">
      <template v-if="Object.keys(groupedConfigs).length > 0">
        <el-card 
          v-for="(items, type) in groupedConfigs" 
          :key="type"
          class="config-group"
        >
          <template #header>
            <div class="group-header">
              <el-icon><Setting /></el-icon>
              <span>{{ type }}</span>
              <el-tag size="small" type="info">{{ items.length }} 项</el-tag>
            </div>
          </template>

          <div class="config-items">
            <div 
              v-for="item in items" 
              :key="item.config_id"
              class="config-item"
            >
              <div class="item-header">
                <span class="item-name">{{ item.item_name }}</span>
                <span class="item-time">{{ formatTime(item.bstudio_create_time) }}</span>
              </div>
              <div class="item-content">{{ item.content }}</div>
            </div>
          </div>
        </el-card>
      </template>

      <el-empty v-else description="暂无配置数据" />
    </div>
  </div>
</template>

<style scoped>
.configs-page {
  animation: fadeIn 0.5s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

.page-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #1a1a2e;
  margin: 0;
}

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

.config-group {
  border-radius: 16px;
}

.group-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: #1a1a2e;
}

.config-items {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.config-item {
  padding: 16px;
  background: #f8fafc;
  border-radius: 12px;
}

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

.item-name {
  font-weight: 500;
  color: #334155;
}

.item-time {
  font-size: 12px;
  color: #94a3b8;
}

.item-content {
  font-size: 14px;
  color: #64748b;
  line-height: 1.6;
  white-space: pre-wrap;
}

@media (max-width: 1200px) {
  .configs-grid {
    grid-template-columns: 1fr;
  }
}
</style>
