<!--
  * 无处罚证明申请主表
  *
  * @Author:    wzh
  * @Date:      2026-01-07 21:36:44
  * @Copyright  1.0
-->
<template>
  <a-modal
      :title="form.id ? '编辑无处罚证明申请' : '新增无处罚证明申请'"
      :width="500"
      :open="visibleFlag"
      @cancel="onClose"
      :maskClosable="false"
      :destroyOnClose="true"
  >
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 6 }" >
        <a-form-item label="申请事由"  name="usePurpose">
          <a-textarea v-model:value="form.usePurpose" placeholder="请输入申请事由" :rows="4" />
        </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">提交申请</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, computed } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { penaltyApplyApi } from '/@/api/business/penalty-apply/penalty-apply-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import { useUserStore } from '/@/store/modules/system/user';
  import DictSelect from '/@/components/support/dict-select/index.vue';

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  // ------------------------ 用户信息 ------------------------
  const userStore = useUserStore();
  
  // 当前用户信息
  const currentUser = computed(() => ({
    userId: userStore.userId,
    userName: userStore.actualName,
    departmentName: userStore.departmentName
  }));

  function show(rowData) {
    Object.assign(form, formDefault);
    
    // 设置默认值
    if (!rowData || _.isEmpty(rowData)) {
      // 新增时设置默认值
      form.userId = currentUser.value.userId;
      form.userName = currentUser.value.userName;
      form.status = 0; // 默认未提交状态
    } else {
      // 编辑时加载数据
      Object.assign(form, rowData);
    }
    
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
      id: undefined, //申请ID
      userId: undefined, //申请人ID
      userName: undefined, //申请人姓名
      applyDate: undefined, //申请日期
      usePurpose: undefined, //申请事由
      proofMaterials: undefined, //证明材料
      status: undefined, //状态（0-未提交，1-已提交，2-审核中，3-已批准，5-已驳回）
      approvalRemark: undefined, //审批意见
      createTime: undefined, //创建时间
      updateTime: undefined, //更新时间
  };

  let form = reactive({ ...formDefault });

  const rules = {
      usePurpose: [{ required: true, message: '申请事由 必填' }],
  };

  // ------------------------ 提交申请 ------------------------

  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      SmartLoading.show();
      
      // 提交申请，状态为1（已提交）
      const submitData = { ...form };
      submitData.status = 1;
      
      if (submitData.id) {
        await penaltyApplyApi.update(submitData);
        message.success('提交申请成功');
      } else {
        await penaltyApplyApi.add(submitData);
        message.success('提交申请成功');
      }
      onClose();
      emits('reloadList');
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
