<template>
  <el-dialog
    v-model="visible"
    title="打回卷分类管理"
    width="800px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="10vh"
    append-to-body
    @close="handleClose"
    @open="modalBeforeOpen"
  >
    <div class="reject-type-content">
      <!-- 操作按钮 -->
      <div class="action-bar">
        <el-button type="primary" @click="onAdd">新增</el-button>
      </div>

      <!-- 表格 -->
      <el-table
        :data="typeList"
        :loading="loading"
        class="type-table"
        border
        stripe
      >
        <el-table-column prop="id" label="编号" width="80" />
        <el-table-column prop="name" label="分类名称" />
        <el-table-column prop="custom" label="类型" width="80">
          <template #default="scope">
            {{ scope.row.custom ? '自定义' : '默认' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button
              v-if="scope.row.custom"
              type="primary"
              link
              @click="onEdit(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              v-if="scope.row.custom"
              link
              type="danger"
              @click="onDelete(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="close">关闭</el-button>
      </span>
    </template>
  </el-dialog>

  <!-- 新增/编辑打回卷分类弹窗 -->
  <ModifyRejectType
    ref="modifyTypeRef"
    :row-data="curRow"
    :exam-id="examId"
    @modified="getTypeList"
  />
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import type { RejectTypeItem } from '@/api/types/reject';
  import { getRejectTypeList, deleteRejectType } from '@/api/reject';
  import useModal from '@/hooks/modal';
  import useLoading from '@/hooks/loading';
  import { modalConfirm } from '@/utils/ui';

  import ModifyRejectType from './ModifyRejectType.vue';

  defineOptions({
    name: 'RejectTypeDialog',
  });

  interface Props {
    examId: number;
  }

  const props = withDefaults(defineProps<Props>(), {
    examId: 0,
  });

  /* modal */
  const { visible, open, close } = useModal();
  defineExpose({ open, close });

  const { loading, setLoading } = useLoading();

  // 打回卷分类列表
  const typeList = ref<RejectTypeItem[]>([]);

  // 获取打回卷分类列表
  async function getTypeList() {
    if (!props.examId) return;

    setLoading(true);
    try {
      const result = await getRejectTypeList(props.examId);
      typeList.value = result;
    } catch (error) {
      console.error('获取打回卷分类列表失败:', error);
      ElMessage.error('获取打回卷分类列表失败');
    } finally {
      setLoading(false);
    }
  }

  // 新增/编辑相关
  const curRow = ref({} as RejectTypeItem);
  const modifyTypeRef = ref();

  function onAdd() {
    curRow.value = {} as RejectTypeItem;
    modifyTypeRef.value?.open();
  }

  function onEdit(row: RejectTypeItem) {
    curRow.value = row;
    modifyTypeRef.value?.open();
  }

  // 删除
  async function onDelete(row: RejectTypeItem) {
    const confirm = await modalConfirm(
      `确定要删除分类"${row.name}"吗？`,
      '删除确认'
    ).catch(() => false);
    if (!confirm) return;

    try {
      setLoading(true);
      await deleteRejectType(row.id);
      ElMessage.success('删除成功');
      getTypeList();
    } catch (error) {
      console.log('删除失败:', error);
    } finally {
      setLoading(false);
    }
  }

  const handleClose = () => {
    // 清理数据
  };

  /* init modal */
  function modalBeforeOpen() {
    getTypeList();
  }
</script>

<style scoped>
  .reject-type-content {
    min-height: 400px;
  }

  .action-bar {
    margin-bottom: 16px;
    display: flex;
    justify-content: flex-start;
  }

  .type-table {
    width: 100%;
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
</style>
