<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="settingName">
        <el-input v-model="dataForm.settingName" placeholder="请输入批次名称" clearable />
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="dataForm.type" placeholder="请选择类型" style="width: 100%">
          <el-option label="普通类型" value="0" />
          <el-option label="新生收费" value="6" />
        </el-select>
      </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="lastTime">
        <el-date-picker v-model="dataForm.lastTime" type="datetime" placeholder="选择截止时间" value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="dataForm.state" placeholder="请选择状态" style="width: 100%">
          <el-option label="未开始" value="0" />
          <el-option label="进行中" value="30" />
          <el-option label="已结束" value="100" />
        </el-select>
      </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="FinanceRecruitSettingForm">
import { reactive, ref, nextTick } from 'vue'
import { getObj, addObj, editObj } from '/@/api/finance/recruitSetting';
import { useMessage } from '/@/hooks/message';

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

const formRef = ref();
const dataForm = reactive({
  id: '',
  settingName: '',
  type: '6',
  year: '',
  lastTime: '',
  state: '0',
  remarks: '',
});
const visible = ref(false);
const loading = ref(false);

const dataRules = ref({
  settingName: [{ required: true, message: '请输入批次名称', trigger: 'blur' }],
  type: [{ required: true, message: '请选择类型', trigger: 'change' }],
  year: [{ required: true, message: '请选择年份', trigger: 'change' }],
  lastTime: [{ required: true, message: '请选择截止时间', trigger: 'change' }],
  state: [{ required: true, message: '请选择状态', trigger: 'change' }],
});

const openDialog = async (type: string, rowData?: any) => {
  visible.value = true;
  dataForm.id = '';
  dataForm.settingName = '';
  dataForm.type = '6';
  dataForm.year = '';
  dataForm.lastTime = '';
  dataForm.state = '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 || '',
            settingName: res.data.settingName || '',
            type: res.data.type || '6',
            year: res.data.year ? String(res.data.year) : '',
            lastTime: res.data.lastTime || '',
            state: res.data.state || '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>