<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>AI配置快速操作</span>
        <el-button type="text" @click="$router.push('/aiconfig/list')">
          查看全部
        </el-button>
      </div>
    </template>
    
    <div class="quick-actions">
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="action-item">
            <el-icon class="action-icon"><Setting /></el-icon>
            <div class="action-content">
              <div class="action-title">配置管理</div>
              <div class="action-desc">管理AI接口配置</div>
            </div>
            <el-button type="primary" size="small" @click="$router.push('/aiconfig/list')">
              管理
            </el-button>
          </div>
        </el-col>
        
        <el-col :span="12">
          <div class="action-item">
            <el-icon class="action-icon"><Star /></el-icon>
            <div class="action-content">
              <div class="action-title">默认配置</div>
              <div class="action-desc">{{ defaultConfig ? defaultConfig.configName : '未设置' }}</div>
            </div>
            <el-button type="warning" size="small" @click="handleViewDefault" :disabled="!defaultConfig">
              查看
            </el-button>
          </div>
        </el-col>
      </el-row>
      
      <el-row :gutter="20" style="margin-top: 15px;">
        <el-col :span="12">
          <div class="action-item">
            <el-icon class="action-icon"><CircleCheck /></el-icon>
            <div class="action-content">
              <div class="action-title">启用配置</div>
              <div class="action-desc">{{ stats.enabled }} 个配置已启用</div>
            </div>
            <el-button type="success" size="small" @click="handleViewEnabled">
              查看
            </el-button>
          </div>
        </el-col>
        
        <el-col :span="12">
          <div class="action-item">
            <el-icon class="action-icon"><Plus /></el-icon>
            <div class="action-content">
              <div class="action-title">新增配置</div>
              <div class="action-desc">添加新的AI配置</div>
            </div>
            <el-button type="primary" size="small" @click="$router.push('/aiconfig/list')">
              新增
            </el-button>
          </div>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Setting, Star, CircleCheck, Plus } from '@element-plus/icons-vue'
import { getDefaultAiConfigs, countEnabledConfigs, countDisabledConfigs, countDefaultConfigs } from '@/api/aiconfig'
import type { AiConfig } from '@/types/aiconfig'

const router = useRouter()

const stats = reactive({
  enabled: 0,
  disabled: 0,
  default: 0
})

const defaultConfig = ref<AiConfig | null>(null)

// 获取统计信息
const fetchStats = async () => {
  try {
    const [enabledRes, disabledRes, defaultRes] = await Promise.all([
      countEnabledConfigs(),
      countDisabledConfigs(),
      countDefaultConfigs()
    ])
    stats.enabled = enabledRes.data
    stats.disabled = disabledRes.data
    stats.default = defaultRes.data
  } catch (error) {
    console.error('获取统计信息失败:', error)
  }
}

// 获取默认配置
const fetchDefaultConfig = async () => {
  try {
    const res = await getDefaultAiConfigs()
    if (res.data && res.data.length > 0) {
      defaultConfig.value = res.data[0]
    }
  } catch (error) {
    console.error('获取默认配置失败:', error)
  }
}

// 查看默认配置
const handleViewDefault = () => {
  router.push('/aiconfig/list')
}

// 查看启用的配置
const handleViewEnabled = () => {
  router.push('/aiconfig/list')
}

onMounted(() => {
  fetchStats()
  fetchDefaultConfig()
})
</script>

<style scoped lang="scss">
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.quick-actions {
  .action-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: var(--ls-radius-md);
    background: rgba(255, 255, 255, 0.02);
    transition: all 0.3s;
    
    &:hover {
      border-color: rgba(255, 171, 145, 0.28);
      background: rgba(255, 171, 145, 0.06);
    }
    
    .action-icon {
      font-size: 24px;
      color: var(--ls-accent-orange);
    }
    
    .action-content {
      flex: 1;
      
      .action-title {
        font-size: 14px;
        font-weight: 500;
        color: rgba(226, 232, 240, 0.92);
        margin-bottom: 4px;
      }
      
      .action-desc {
        font-size: 12px;
        color: rgba(226, 232, 240, 0.6);
      }
    }
  }
}
</style>