<template>
  <div class="p-2">
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="600px" :draggable="draggable">
      <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="80px">
        <el-form-item label="补助设置Id" prop="mainId">
          <el-input v-model="formData.mainId" placeholder="请输入补助设置Id" />
        </el-form-item>
        <el-form-item label="补助人员Id" prop="userId">
          <el-input v-model="formData.userId" placeholder="请输入补助人员Id" />
        </el-form-item>
        <el-form-item label="补助类型" prop="subsidyType">
          <el-select v-model="formData.subsidyType" placeholder="请选择补助类型">
            <el-option
                v-for="dict in SUBTYPE"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="补助金额" prop="fillMoney">
          <el-input v-model="formData.fillMoney" placeholder="请输入补助金额" />
        </el-form-item>
        <el-form-item label="预计到账时间" prop="fillDate">
          <el-date-picker v-model="formData.fillDate" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择预计到账时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取状态" prop="fillStatus">
          <el-radio-group v-model="formData.fillStatus">
            <el-radio
                v-for="dict in sys_yes_no"
                :key="dict.value"
                :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="领取时间" prop="getDate">
          <el-date-picker v-model="formData.getDate" clearable type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择领取时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="领取补助之后钱包余额" prop="beforeMoney">
          <el-input v-model="formData.beforeMoney" placeholder="请输入领取补助之后钱包余额" />
        </el-form-item>
        <el-form-item label="领取补助之后钱包余额" prop="afterMoney">
          <el-input v-model="formData.afterMoney" placeholder="请输入领取补助之后钱包余额" />
        </el-form-item>
        <el-form-item label="原始记录Id,一般是指写入的充值备份表主键" prop="originalId">
          <el-input v-model="formData.originalId" placeholder="请输入原始记录Id,一般是指写入的充值备份表主键" />
        </el-form-item>
        <el-form-item label="用户编号/工号" prop="userNumb">
          <el-input v-model="formData.userNumb" placeholder="请输入用户编号/工号" />
        </el-form-item>
        <el-form-item label="用户姓名" prop="realName">
          <el-input v-model="formData.realName" placeholder="请输入用户姓名" />
        </el-form-item>
        <el-form-item label="部门名称" prop="detpName">
          <el-input v-model="formData.detpName" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="formData.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="formLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="dialog.visible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PtSubsidyitemForm" lang="ts">
import * as PtSubsidyitemApi from '@/api/cardCenter/subsidy/ptSubsidyitem';
import { PtSubsidyitemForm } from '@/api/cardCenter/subsidy/ptSubsidyitem/types';
import { useI18n } from 'vue-i18n';

defineOptions({ name: 'PtSubsidyitemForm' });

// 接收父组件传入属性
// const props = defineProps({})

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 国际化
const { t } = useI18n();

/** 数据字典 */
const { sys_yes_no, SUBTYPE } = toRefs<any>(proxy?.useDict('sys_yes_no', 'SUBTYPE'));
// 对话框
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
// 窗体是否可以拖动
const draggable = ref(true);
// 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formLoading = ref(false);
// 表单的类型：create - 新增；update - 修改
const formType = ref('');
// 表单引用
const formRef = ref<ElFormInstance>();
// 表单数据
const formData = ref<PtSubsidyitemForm>({
  itemId: undefined,
  mainId: undefined,
  userId: undefined,
  subsidyType: '',
  fillMoney: undefined,
  fillDate: undefined,
  fillStatus: '',
  getDate: undefined,
  beforeMoney: undefined,
  afterMoney: undefined,
  originalId: '',
  userNumb: '',
  realName: '',
  detpName: '',
  remark: '',
});
// 表单校验规则
const formRules = reactive({
  itemId: [{ required: true, message: '明细Id,主键不能为空', trigger: 'blur' }],
  mainId: [{ required: true, message: '补助设置Id不能为空', trigger: 'blur' }],
  userId: [{ required: true, message: '补助人员Id不能为空', trigger: 'blur' }],
  subsidyType: [{ required: true, message: '补助类型不能为空', trigger: 'change' }],
  fillMoney: [{ required: true, message: '补助金额不能为空', trigger: 'blur' }],
  fillDate: [{ required: true, message: '预计到账时间不能为空', trigger: 'blur' }],
  userNumb: [{ required: true, message: '用户编号/工号不能为空', trigger: 'blur' }],
  realName: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }],
  detpName: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
});
/** 当前组件方法 */
/** 窗体打开事件 */
const open = async (type: string, id?: number) => {
  dialog.visible = true;
  dialog.title = t('action.' + type);
  formType.value = type;
  resetForm();
  // 修改时，设置数据
  if (id) {
    formLoading.value = true;
    try {
      const res = await PtSubsidyitemApi.getPtSubsidyitem(id);
      Object.assign(formData.value, res.data);
    } finally {
      formLoading.value = false;
    }
  }
};
/** 表单重置 */
const resetForm = () => {
  formData.value = {
    itemId: undefined,
    mainId: undefined,
    userId: undefined,
    subsidyType: '',
    fillMoney: undefined,
    fillStatus: '',
    getDate: undefined,
    beforeMoney: undefined,
    afterMoney: undefined,
    originalId: '',
    userNumb: '',
    realName: '',
    detpName: '',
    remark: '',
  };
  formRef.value?.resetFields();
};
// 传回给父组件的属性与方法
// 提供 open 方法，用于打开弹窗
defineExpose({ open });
/** 触发父组件的事件 */
/** 定义 success 事件，用于操作成功后的回调 */
const emit = defineEmits(['success']);
/** 提交按钮 */
const submitForm = async () => {
  // 校验表单
  if (!formRef) return;
  const valid = await formRef.value?.validate();
  if (!valid) return;
  // 提交请求
  formLoading.value = true;
  try {
    const data = formData.value as unknown as PtSubsidyitemForm;
    if (formType.value === 'create') {
      await PtSubsidyitemApi.addPtSubsidyitem(data);
      proxy?.$modal.msgSuccess('新增成功');
    } else {
      await PtSubsidyitemApi.updatePtSubsidyitem(data);
      proxy?.$modal.msgSuccess('修改成功');
    }
    dialog.visible = false;
    // 发送操作成功的事件
    emit('success');
  } finally {
    formLoading.value = false;
  }
};

/** 初始化 */
onMounted(() => {});
</script>
