<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="serialNumber">
        <el-input v-model="dataForm.serialNumber" placeholder="请输入学号" clearable />
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model="dataForm.realName" placeholder="请输入姓名" clearable />
      </el-form-item>
      <el-form-item label="学生来源" prop="stuSource">
        <el-select v-model="dataForm.stuSource" placeholder="请选择学生来源" style="width: 100%">
          <el-option label="本地" value="1" />
          <el-option label="外地" value="2" />
          <el-option label="其他" value="3" />
        </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="phone">
        <el-input v-model="dataForm.phone" placeholder="请输入手机号" clearable />
      </el-form-item>
      <el-form-item label="缴费码" prop="payCode">
        <el-input v-model="dataForm.payCode" placeholder="请输入缴费码" clearable />
      </el-form-item>
      <el-form-item label="是否取消" prop="isCancal">
        <el-radio-group v-model="dataForm.isCancal">
          <el-radio label="0">正常</el-radio>
          <el-radio label="1">已取消</el-radio>
        </el-radio-group>
      </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="FinanceRecruitStuForm">
import { reactive, ref, nextTick } from 'vue'
import { getObj, addObj, editObj } from '/@/api/finance/recruitStu';
import { useMessage } from '/@/hooks/message';

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

const formRef = ref();
const dataForm = reactive({
  id: '',
  serialNumber: '',
  realName: '',
  stuSource: '1',
  year: '',
  phone: '',
  payCode: '',
  isCancal: '0',
  remarks: '',
});
const visible = ref(false);
const loading = ref(false);

const dataRules = ref({
  serialNumber: [{ required: true, message: '请输入学号', trigger: 'blur' }],
  realName: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  stuSource: [{ required: true, message: '请选择学生来源', trigger: 'change' }],
  year: [{ required: true, message: '请选择年份', trigger: 'change' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
});

const openDialog = async (type: string, rowData?: any) => {
  visible.value = true;
  dataForm.id = '';
  dataForm.serialNumber = '';
  dataForm.realName = '';
  dataForm.stuSource = '1';
  dataForm.year = '';
  dataForm.phone = '';
  dataForm.payCode = '';
  dataForm.isCancal = '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 || '',
            serialNumber: res.data.serialNumber || '',
            realName: res.data.realName || '',
            stuSource: res.data.stuSource || '1',
            year: res.data.year ? String(res.data.year) : '',
            phone: res.data.phone || '',
            payCode: res.data.payCode || '',
            isCancal: res.data.isCancal || '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>