<template>
  <el-dialog 
    title="归档" 
    v-model="visible" 
    :width="500" 
    :close-on-click-modal="false" 
    draggable>
    <el-form 
      ref="dataFormRef" 
      :model="form" 
      :rules="dataRules" 
      label-width="120px" 
      v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="标题">
            <el-input v-model="form.title" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="作者">
            <el-input v-model="form.author" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="班号">
            <el-input v-model="form.classNo" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="归档级别" prop="belong">
            <el-select 
              v-model="form.belong" 
              placeholder="请选择归档级别" 
              clearable
              style="width: 100%">
              <el-option
                v-for="item in belongList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确 认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ClassHonorBelongDialog">
import { ref, reactive, nextTick, onMounted } from 'vue'
import { useMessage } from '/@/hooks/message'
import { editBelong, getDetail } from '/@/api/stuwork/classhonor'
import { getDicts } from '/@/api/admin/dict'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const belongList = ref<any[]>([])

// 提交表单数据
const form = reactive({
  id: '',
  title: '',
  author: '',
  classNo: '',
  belong: ''
})

// 定义校验规则
const dataRules = {
  belong: [
    { required: true, message: '请选择归档级别', trigger: 'change' }
  ]
}

// 打开弹窗
const openDialog = async (row: any) => {
  visible.value = true
  
  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.title = ''
    form.author = ''
    form.classNo = ''
    form.belong = ''
    
    if (row) {
      form.id = row.id
      form.title = row.title || ''
      form.author = row.author || ''
      form.classNo = row.classNo || ''
      form.belong = row.belong || ''
      
      // 如果没有归档级别，尝试获取详情
      if (row.id && !row.belong) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.belong = res.data.belong || ''
          }
        }).catch((err) => {
        }).finally(() => {
          loading.value = false
        })
      }
    }
  })
}

// 提交表单
const onSubmit = async () => {
  if (!dataFormRef.value) return
  
  await dataFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    
    loading.value = true
    try {
      await editBelong({
        id: form.id,
        belong: form.belong
      })
      useMessage().success('归档成功')
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || '归档失败')
    } finally {
      loading.value = false
    }
  })
}

// 获取归档级别字典
const getBelongDict = async () => {
  try {
    const res = await getDicts('honor_belong_type')
    if (res.data && Array.isArray(res.data)) {
      belongList.value = res.data.map((item: any) => ({
        label: item.label || item.dictLabel || item.name,
        value: item.value || item.dictValue || item.code
      }))
    }
  } catch (err) {
  }
}

// 初始化
onMounted(() => {
  getBelongDict()
})

// 暴露方法
defineExpose({
  openDialog
})
</script>

<style scoped lang="scss">
.mb20 {
  margin-bottom: 20px;
}
</style>

