<template>
  <j-modal :title="title" :width="width" :visible="visible" @ok="handleOk" :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭">
    <a-alert v-if="title === '新增'" message="可以多次添加反馈信息" type="info" showIcon style="margin-bottom: 12px" />
    <BgXiSpeakFeedbackForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></BgXiSpeakFeedbackForm>
  </j-modal>
</template>

<script lang="ts" setup>
  import { ref, nextTick, defineExpose, inject, unref } from 'vue';
  import BgXiSpeakFeedbackForm from './BgXiSpeakFeedbackForm.vue';
  import JModal from '/@/components/Modal/src/JModal/JModal.vue';

  const mainId = inject('mainId');
  const title = ref<string>('');
  const width = ref<number>(800);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);

  function add() {
    title.value = '新增';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add();
      registerForm.value.setMainId(unref(mainId));
    });
  }

  function edit(record) {
    title.value = disableSubmit.value ? '详情' : '编辑';
    visible.value = true;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  function handleOk() {
    return registerForm.value.submitForm();
  }

  function submitCallback() {
    handleCancel();
    emit('success');
  }

  function handleCancel() {
    visible.value = false;
  }

  defineExpose({
    add,
    edit,
    disableSubmit,
  });
</script>

<style lang="less">
  .jee-hidden {
    display: none !important;
  }
</style>
