<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    showFooter
    destroyOnClose
    :title="getTitle"
    width="70%"
    @ok="handleSubmit"
    @close="handleClose"
  >
    <div class="padding-box" v-if="open">
      <ModalModal v-if="open" ref="modalModalRef" :editId="editId" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import ModalModal from './Modal.vue';
  import { add, edit } from '/@/api/system/generator/treeStructure';
  const { t } = useI18n();
  const isUpdate = ref<boolean>(false);
  const open = ref(false);
  const editId = ref('');
  const modalModalRef = ref();
  export default defineComponent({
    name: 'FormModal',
    components: {
      BasicModal,
      ModalModal,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { notification } = useMessage();

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        isUpdate.value = !!data?.isUpdate;
        setModalProps({ confirmLoading: false });
        open.value = false;
        editId.value = '';
        if (isUpdate.value) {
          editId.value = data.id;
        }
        setTimeout(() => {
          open.value = true;
        }, 50);
      });

      const getTitle = computed(() => (!isUpdate.value ? t('新增树结构') : t('编辑树结构')));
      function handleClose() {
        closeModal();
        open.value = false;
      }
      async function handleSubmit() {
        try {
          let formState = await modalModalRef.value.handleSubmit();
          if (formState) {
            setModalProps({ confirmLoading: true });
            let { columns, config, ...otherParams } = formState;
            let params = {
              ...otherParams,
              columns: JSON.stringify(columns),
              config: JSON.stringify(config),
            };
            console.log('params: ', params);
            if (!isUpdate.value) {
              //新增
              await add(params);
              notification.success({
                message: t('提示'),
                description: t('新增树结构成功'),
              }); //提示消息
            } else {
              // 编辑
              await edit({ ...params, id: editId.value });
              notification.success({
                message: t('提示'),
                description: t('修改树结构成功'),
              }); //提示消息
            }
            closeModal();
            emit('success');
          } else {
            setModalProps({ confirmLoading: false });
          }
        } catch (error) {
          setModalProps({ confirmLoading: false });
        }
      }
      return {
        registerModal,
        getTitle,
        handleSubmit,
        handleClose,
        t,
        open,
        editId,
        modalModalRef,
      };
    },
  });
</script>
<style lang="less" scoped>
  .padding-box {
    padding: 16px 0 16px 16px;
  }
</style>
