<template>
  <BasicModal
    v-bind="$attrs"
    destroyOnClose
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
    width="900px"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { dataFormSchema } from './data';

  // import {
  //   // addObj,
  //   // editObj,
  //   // viewObj,
  //   selectMenuTreeAndExcludeNode,
  // } from '/@/api/modules/system/sysMenuApi';

  import {
    rbacMenuAdd,
    rbacMenuEdit,
    rbacMenuDetailId,
    rbacMenuQueryTreeAbbr,
  } from '/@/api/sys/rbacMenuApi';
  import { listDictModel } from '/@/api/common';
  import { transformDict } from '/@/utils';

  const emit = defineEmits(['success', 'register']);

  const getTitle = computed(() => (!unref(isUpdate) ? '新增菜单' : '编辑菜单'));
  const isUpdate = ref(false);
  const rowId = ref();

  const { createMessage } = useMessage();
  const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
    labelWidth: 100,
    schemas: dataFormSchema,
    showActionButtonGroup: false,
    baseColProps: { lg: 12, md: 24 },
    actionColOptions: {
      span: 23,
    },
  });
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async data => {
    await resetFields();
    setModalProps({ confirmLoading: false });
    isUpdate.value = !!data?.isUpdate;

    if (unref(isUpdate)) {
      rowId.value = data.record.id;
      const resData = await rbacMenuDetailId(data.record.id);
      await setFieldsValue({
        ...resData,
        // 设置默认值
        parentId: resData.parentId === '0' ? null : resData.parentId,
        visible: resData.visible,
        disable: resData.disable,
        keepalive: resData.keepalive ? 0 : 1,
        linkExternal: resData.linkExternal,
        frame: resData.frame,
        tenantMenu: !resData.tenantMenu ? '0' : resData.tenantMenu.toString(),
      });
    } else {
      await setFieldsValue({
        parentId: data.record.parentId,
      });
    }

    const sysMenuTypeOptions = transformDict(await listDictModel({ dictCode: 'sys_menu_type' }));
    const treeData = await rbacMenuQueryTreeAbbr({ excludeNodeIds: [data.record.id] });
    await updateSchema([
      {
        field: 'menuType',
        componentProps: { options: sysMenuTypeOptions },
      },
      // {
      //   field: 'parentId',
      //   componentProps: {
      //     treeData: treeData,
      //     fieldNames: {
      //       label: 'name',
      //       key: 'id',
      //       value: 'id',
      //     },
      //   },
      // },
    ]);
    await updateSchema({ field: 'parentId', componentProps: { treeData } });
  });

  // 提交按钮事件
  async function handleSubmit() {
    try {
      const values = await validate();
      setModalProps({ confirmLoading: true });
      !unref(isUpdate)
        ? await rbacMenuAdd({ ...values })
        : await rbacMenuEdit({ ...values, id: rowId.value });
      !unref(isUpdate) ? createMessage.success('新增成功！') : createMessage.success('编辑成功！');
      closeModal();
      emit('success', { isUpdate: unref(isUpdate), values: { ...values, id: rowId.value } });
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>
