<template>
  <el-dialog
    :title="form.id ? '编辑考核奖项' : '新增考核奖项'"
    v-model="visible"
    :width="600"
    :close-on-click-modal="false"
    draggable>
    <el-form
      ref="dataFormRef"
      :model="form"
      :rules="dataRules"
      label-width="100px"
      v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="考核项名称" prop="category">
            <el-input
              v-model="form.category"
              placeholder="请输入考核项名称"
              clearable
              style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="考核类型" prop="type">
            <el-select
              v-model="form.type"
              placeholder="请选择考核类型"
              clearable
              style="width: 100%">
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="mb20">
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-model="form.remarks"
              type="textarea"
              :rows="3"
              placeholder="请输入备注"
              maxlength="250"
              show-word-limit
              style="width: 100%" />
          </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="AssessmentCategoryFormDialog">
import { ref, reactive, nextTick } from 'vue'
import { useMessage } from '/@/hooks/message'
import { addObj, editObj, getDetail } from '/@/api/stuwork/assessmentcategory'

const emit = defineEmits(['refresh'])

// 定义变量内容
const dataFormRef = ref()
const visible = ref(false)
const loading = ref(false)
const operType = ref('add')

// 考核类型列表
const typeList = ref([
  { label: '常规考核', value: '0' },
  { label: '顶岗考核', value: '1' }
])

// 提交表单数据
const form = reactive({
  id: '',
  category: '',
  type: '',
  remarks: ''
})

// 定义校验规则
const dataRules = {
  category: [
    { required: true, message: '请输入考核项名称', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择考核类型', trigger: 'change' }
  ]
}

// 打开弹窗
const openDialog = async (type: string = 'add', row?: any) => {
  visible.value = true
  operType.value = type
  
    // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields()
    form.id = ''
    form.category = ''
    form.type = ''
    form.remarks = ''
    
    // 编辑时填充数据
    if (type === 'edit' && row) {
      form.id = row.id
      form.category = row.category || ''
      form.type = row.type || ''
      form.remarks = row.remarks || ''
      
      // 如果需要获取详情
      if (row.id && (!row.category || !row.type)) {
        loading.value = true
        getDetail(row.id).then((res: any) => {
          if (res.data) {
            form.category = res.data.category || ''
            form.type = res.data.type || ''
            form.remarks = res.data.remarks || ''
          }
        }).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 {
      const submitData = {
        category: form.category,
        type: form.type,
        remarks: form.remarks
      }
      
      if (operType.value === 'add') {
        await addObj(submitData)
        useMessage().success('新增成功')
      } else {
        await editObj({
          id: form.id,
          ...submitData
        })
        useMessage().success('编辑成功')
      }
      visible.value = false
      emit('refresh')
    } catch (err: any) {
      useMessage().error(err.msg || (operType.value === 'add' ? '新增失败' : '编辑失败'))
    } finally {
      loading.value = false
    }
  })
}

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

<style scoped lang="scss">
</style>

