<template>
  <el-dialog :title="dataForm.id ? '编辑' : '新增'" v-model="visible" width="600px" :close-on-click-modal="false" draggable>
    <el-form ref="formRef" :model="dataForm" :rules="dataRules" label-width="100px" v-loading="loading">
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="dataForm.projectName" placeholder="请输入项目名称" clearable />
      </el-form-item>
      <el-form-item label="项目代码" prop="projectCode">
        <el-input v-model="dataForm.projectCode" placeholder="请输入项目代码" clearable />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-date-picker v-model="dataForm.year" type="year" placeholder="选择年份" value-format="YYYY" style="width: 100%" />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="0" placeholder="请输入排序" style="width: 100%" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" type="textarea" :rows="3" placeholder="请输入备注" clearable />
      </el-form-item>
    </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="FinanceRecruitProjectForm">
import { reactive, ref, nextTick } from 'vue'
import { getObj, addObj, editObj } from '/@/api/finance/recruitProject';
import { useMessage } from '/@/hooks/message';

const emit = defineEmits(['refresh']);

const formRef = ref();
const dataForm = reactive({
  id: '',
  projectName: '',
  projectCode: '',
  year: '',
  sort: 0,
  remarks: '',
});
const visible = ref(false);
const loading = ref(false);

const dataRules = ref({
  projectName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
  projectCode: [{ required: true, message: '请输入项目代码', trigger: 'blur' }],
  year: [{ required: true, message: '请选择年份', trigger: 'change' }],
});

const openDialog = async (type: string, rowData?: any) => {
  visible.value = true;
  dataForm.id = '';
  dataForm.projectName = '';
  dataForm.projectCode = '';
  dataForm.year = '';
  dataForm.sort = 0;
  dataForm.remarks = '';

  nextTick(() => {
    formRef.value?.resetFields();
    if (type === 'edit' && rowData?.id) {
      loading.value = true;
      getObj(rowData.id).then((res: any) => {
        if (res.data) {
          Object.assign(dataForm, {
            id: res.data.id || '',
            projectName: res.data.projectName || '',
            projectCode: res.data.projectCode || '',
            year: res.data.year ? String(res.data.year) : '',
            sort: res.data.sort || 0,
            remarks: res.data.remarks || '',
          });
        }
        loading.value = false;
      }).catch((err: any) => {
        useMessage().error(err.msg || '获取详情失败');
        loading.value = false;
      });
    }
  });
};

const onSubmit = async () => {
  if (loading.value) return;
  loading.value = true;

  try {
    const valid = await formRef.value.validate().catch(() => {});
    if (!valid) {
      loading.value = false;
      return false;
    }

    const submitData = {
      ...dataForm,
      year: dataForm.year ? parseInt(dataForm.year) : null,
    };

    if (dataForm.id) {
      await editObj(submitData);
      useMessage().success('编辑成功');
    } else {
      await addObj(submitData);
      useMessage().success('新增成功');
    }
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg || (dataForm.id ? '编辑失败' : '新增失败'));
  } finally {
    loading.value = false;
  }
};

defineExpose({ openDialog });
</script>