<template>
  <div class="app-container">
    <div class="qualification-header">
      <h3>资质管理</h3>
      <el-button type="primary" @click="handleAdd">新增资质</el-button>
    </div>

    <el-table :data="qualificationList" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="qualificationNo" label="资质编号" />
      <el-table-column prop="qualificationType" label="资质类型">
        <template #default="scope">
          {{ formatQualificationType(scope.row.qualificationType) }}
        </template>
      </el-table-column>
      <el-table-column prop="authority" label="签发机构" />
      <el-table-column prop="deadline" label="截止日期" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">修改</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
      <el-form :model="formData" label-width="120px">
        <el-form-item label="资质类型">
          <el-select v-model="formData.qualificationType" placeholder="请选择">
            <el-option label="营业执照" :value="1" />
            <el-option label="资质证书" :value="2" />
            <el-option label="其他" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="资质证书编号">
          <el-input v-model="formData.qualificationNo" />
        </el-form-item>
        <el-form-item label="签发机构">
          <el-input v-model="formData.authority" />
        </el-form-item>
        <el-form-item label="截止日期">
          <el-date-picker v-model="formData.deadline" type="date" placeholder="选择日期" style="width: 100%" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="PartnerQualification">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import {
  getCurrentPartnerInfo,
  getPartnerQualificationList,
  addPartnerQualification,
  updatePartnerQualification,
  deletePartnerQualification
} from '@/api/partner';

const qualificationList = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formData = ref({
  id: null,
  partnerId: null,
  qualificationType: null,
  qualificationNo: '',
  authority: '',
  deadline: null,
  remark: ''
});

// 资质类型字典转换
const formatQualificationType = (type: number) => {
  const typeMap: Record<number, string> = {
    1: '营业执照',
    2: '资质证书',
    3: '其他'
  };
  return typeMap[type] || type;
};

const fetchQualificationList = async () => {
  loading.value = true;
  try {
    const partnerResponse = await getCurrentPartnerInfo();
    if (partnerResponse.code === 200 && partnerResponse.data) {
      const partnerId = partnerResponse.data.id;
      formData.value.partnerId = partnerId;
      const response = await getPartnerQualificationList(partnerId);
      if (response.code === 200) {
        qualificationList.value = response.rows || response.data || [];
      }
    }
  } catch (error) {
    console.error('获取资质列表失败', error);
    ElMessage.error('获取资质列表失败');
  } finally {
    loading.value = false;
  }
};

const handleAdd = () => {
  dialogTitle.value = '新增资质';
  formData.value = {
    id: null,
    partnerId: formData.value.partnerId,
    qualificationType: null,
    qualificationNo: '',
    authority: '',
    deadline: null,
    remark: ''
  };
  dialogVisible.value = true;
};

const handleEdit = (row: any) => {
  dialogTitle.value = '编辑资质';
  formData.value = { ...row };
  dialogVisible.value = true;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该资质吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const response = await deletePartnerQualification([row.id]);
      if (response.code === 200) {
        ElMessage.success('删除成功');
        fetchQualificationList();
      } else {
        ElMessage.error(response.msg || '删除失败');
      }
    } catch (error) {
      console.error('删除失败', error);
      ElMessage.error('删除失败');
    }
  });
};

const handleSubmit = async () => {
  try {
    const apiCall = formData.value.id ? updatePartnerQualification : addPartnerQualification;
    const response = await apiCall(formData.value);
    if (response.code === 200) {
      ElMessage.success(formData.value.id ? '修改成功' : '新增成功');
      dialogVisible.value = false;
      fetchQualificationList();
    } else {
      ElMessage.error(response.msg || '操作失败');
    }
  } catch (error) {
    console.error('操作失败', error);
    ElMessage.error('操作失败');
  }
};

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

<style scoped lang="scss">
.app-container {
  padding: 20px;
}

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

  h3 {
    margin: 0;
    font-size: 16px;
    color: #333;
  }
}
</style>
