<template>
  <div>
    <div class="page-header">
      <h2>资质荣誉管理</h2>
      <el-button type="primary" @click="showDialog()">新增</el-button>
    </div>

    <el-table :data="list" border stripe v-loading="loading">
      <el-table-column prop="id" label="ID" width="60" />
      <el-table-column prop="title" label="名称" show-overflow-tooltip />
      <el-table-column prop="issuer" label="颁发机构" width="160" />
      <el-table-column prop="year" label="年份" width="100" />
      <el-table-column prop="category" label="分类" width="100" />
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button type="primary" text size="small" @click="showDialog(row)">编辑</el-button>
          <el-popconfirm title="确认删除？" @confirm="handleDelete(row.id)">
            <template #reference>
              <el-button type="danger" text size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="editingId ? '编辑资质' : '新增资质'" width="580px">
      <el-form :model="form" label-width="90px">
        <el-form-item label="名称">
          <el-input v-model="form.title" placeholder="资质/荣誉名称" />
        </el-form-item>
        <el-form-item label="颁发机构">
          <el-input v-model="form.issuer" placeholder="颁发机构" />
        </el-form-item>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="年份">
              <el-input v-model="form.year" placeholder="如：2024" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分类">
              <el-select v-model="form.category" placeholder="选择分类" style="width:100%">
                <el-option label="资质认证" value="资质认证" />
                <el-option label="荣誉奖项" value="荣誉奖项" />
                <el-option label="行业认可" value="行业认可" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="证书图片">
          <el-input v-model="form.image" placeholder="图片URL" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.description" type="textarea" :autosize="{ minRows: 2, maxRows: 6 }" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave" :loading="saving">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getQualifications, createQualification, updateQualification, deleteQualification } from '../../api/qualifications'

const loading = ref(false)
const saving = ref(false)
const list = ref([])
const dialogVisible = ref(false)
const editingId = ref(null)

const defaultForm = () => ({
  title: '', issuer: '', year: '', image: '', description: '', category: '',
})
const form = ref(defaultForm())

const loadData = async () => {
  loading.value = true
  try {
    const res = await getQualifications()
    list.value = res.data
  } catch {
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const showDialog = (row) => {
  if (row) {
    editingId.value = row.id
    form.value = { ...row }
  } else {
    editingId.value = null
    form.value = defaultForm()
  }
  dialogVisible.value = true
}

const handleSave = async () => {
  if (!form.value.title) { ElMessage.warning('请填写名称'); return }
  saving.value = true
  try {
    if (editingId.value) {
      await updateQualification(editingId.value, form.value)
    } else {
      await createQualification(form.value)
    }
    ElMessage.success('保存成功')
    dialogVisible.value = false
    loadData()
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '保存失败')
  } finally {
    saving.value = false
  }
}

const handleDelete = async (id) => {
  try {
    await deleteQualification(id)
    ElMessage.success('删除成功')
    loadData()
  } catch {
    ElMessage.error('删除失败')
  }
}

onMounted(loadData)
</script>
