<template>
  <Mask :is-show="show" :width="600" :height="400">
    <CloseBtn @click="close"></CloseBtn>
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px" class="form">
      <el-form-item label="人员编号" prop="psnNo">
        <el-input v-model="formData.psnNo" placeholder="请输入人员编号"></el-input>
      </el-form-item>
      <el-form-item label="发送方编号" prop="omsgid">
        <el-input v-model="formData.omsgid" placeholder="请输入发送方编号"></el-input>
      </el-form-item>
      <el-form-item label="原交易编号" prop="oinfno">
        <el-input v-model="formData.oinfno" placeholder="请输入原交易编号（2102药店结算，2103药店结算撤销）"></el-input>
      </el-form-item>
      <el-form-item label="确认字段" prop="confirmWord">
        <el-input v-model="formData.confirmWord" placeholder="请输入确认冲正"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </el-form-item>
    </el-form>
  </Mask>
</template>
<script setup lang="ts">
  import {ref} from "vue";
  import Mask from "@/components/Mask.vue";
  import CloseBtn from "@/components/CloseBtn.vue";
  import {post} from "@/utils/request.ts";
  import {ElMessage, ElMessageBox} from "element-plus";
  import {API} from "@/assets/config/API.ts";

  const show = ref(false)
  const close = ()=>{
    show.value = false
  }
  const open = (data:any)=>{
    formData.value = data
    show.value = true
  }
  defineExpose({open})
  const formData = ref<any>()
  const formRef = ref()
  const rules = ref({
    psnNo: [
      {required: true, message: '请输入人员编号', trigger: 'blur'},
    ],
    omsgid: [
      {required: true, message: '请输入发送方编号', trigger: 'blur'},
    ],
    oinfno: [
      {required: true, message: '请输入原交易编号', trigger: 'blur'},
    ],
    confirmWord: [
      {required: true, message: '请输入确认冲正', trigger: 'blur'},
    ],
  })
  const submitForm = ()=>{
    formRef.value?.validate((valid: boolean) => {
      if (!valid) {
        return;
      }
      if (formData.value.confirmWord != "确认冲正"){
        ElMessage({
          message: "需要正确输入确认冲正",
          type: 'error',
          plain: true
        })
        return;
      }
      ElMessageBox.confirm(
          `冲正后无法撤销，是否确定继续进行冲正？`,
          'Warning',
          {
            confirmButtonText: '确认继续？',
            cancelButtonText: '取消',
            type: 'warning',
          }
      ).then(() => {
        // 如果验证通过，发送请求
        // alert("提交冲正，代码注释了，防止在正式库冲正")
        post(API.Social.Reconciliation.Reversal, {data: formData.value}).then((res: any) => {
          ElMessage({
            message: "冲正成功",
            type: 'success',
            plain: true
          })
          formRef.value?.resetFields();
          close()
        });
      });
      })


  }
</script>
<style scoped lang="scss">
.form{
  margin-top: 30px;
}
</style>