<template>
  <j-modal
    :fullscreen="true"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    :title="title"
    :visible="visible"
    :width="900"
    cancelText="关闭"
    maxHeight="700px"
    @cancel="handleCancel"
    @ok="handleOk"
  >
    <!-- 详情模式：展示FocusPersonnelDetail -->
    <FocusPersonnelDetail v-if="isDetailMode" :record="currentRecord" />
    <!-- 编辑/新增模式：展示FocusPersonnelForm -->
    <FocusPersonnelForm v-else ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></FocusPersonnelForm>
    <template #footer>
      <a-button @click="handleCancel">{{ isDetailMode ? '关闭' : '取消' }}</a-button>
      <a-button v-if="!isDetailMode" type="primary" @click="handleOk">确认</a-button>
    </template>
  </j-modal>
</template>

<script lang="ts" setup>
  import { defineExpose, nextTick, ref } from 'vue';
  import FocusPersonnelForm from './FocusPersonnelForm.vue';
  import FocusPersonnelDetail from './FocusPersonnelDetail.vue';
  import JModal from '/@/components/Modal/src/JModal/JModal.vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const title = ref<string>('');
  const width = ref<number>(900);
  const visible = ref<boolean>(false);
  const disableSubmit = ref<boolean>(false);
  const isDetailMode = ref<boolean>(false);
  const currentRecord = ref<any>({});
  const registerForm = ref();
  const emit = defineEmits(['register', 'success']);

  /**
   * 新增
   */
  function add() {
    isDetailMode.value = false;
    title.value = '新增重点关注人员';
    visible.value = true;
    nextTick(() => {
      registerForm.value.add();
    });
  }

  /**
   * 编辑
   */
  function edit(record) {
    isDetailMode.value = false;
    title.value = '编辑重点关注人员';
    visible.value = true;
    currentRecord.value = record;
    nextTick(() => {
      registerForm.value.edit(record);
    });
  }

  /**
   * 详情
   */
  function detail(record) {
    isDetailMode.value = true;
    title.value = '重点关注人员详情';
    visible.value = true;
    currentRecord.value = record;
  }

  /**
   * 确定按钮点击事件
   */
  function handleOk() {
    if (isDetailMode.value) {
      handleCancel();
      return;
    }
    registerForm.value.submitForm();
  }

  /**
   * form保存回调事件
   */
  function submitCallback() {
    handleCancel();
    emit('success');
  }

  /**
   * 取消按钮回调事件
   */
  function handleCancel() {
    visible.value = false;
  }

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

<style lang="less">
  /**隐藏样式-modal确定按钮 */
  .jee-hidden {
    display: none !important;
  }
</style>
<style lang="less" scoped></style>
