import { HModal, message, Icon } from '@crami/ui';
import { createVNode } from 'vue';
import { useI18n } from '@crami/locale';

const checkDataPermission = async (data: any, fn, options = {}) => {
  const { t } = useI18n();

  const triggerConfirm = (mode, content) => {
    const defaultModalOptions = {
      closable: true,
      icon: createVNode(Icon, {
        name: 'icp-exclamation-circle-filled',
        size: 24,
        style: {
          float: 'left',
          marginRight: '8px',
        },
      }),
      title: t('platform.common.prompt'),
      okText: t('platform.common.ok'),
      cancelText: t('platform.common.cancel'),
      class: 'data-permission-treenodedelete-modal',
      onOk: () => fn(data),
    };

    HModal.confirm(
      Object.assign({}, defaultModalOptions, options.modalProps || {}, {
        content,
      }),
    );
  };

  // 逻辑流存在数据权限校验
  if (data && data.dataAuthAjaxData) {
    const res = data.dataAuthAjaxData;
    if (res.code === 200) {
      // 提交的数据都没有数据权限
      if (!res.data?.authorized?.length) {
        message.warning(res.data.msg);
      } else {
        options.customize
          ? fn(data)
          : triggerConfirm('0', options?.modalProps?.content || res.data.msg);
      }
    } else {
      message.error(res.msg);
    }
  }
  // 执行原先默认逻辑
  else {
    options.customize
      ? fn(data)
      : triggerConfirm('1', options?.modalProps?.content || t('platform.common.deleteMsg'));
  }
};

// 删除
export const checkDeleteDataPermission = checkDataPermission;
// 启用
export const checkEnableDataPermission = checkDataPermission;
// 禁用
export const checkDisableDataPermission = checkDataPermission;
// 其它
export const checkCommonDataPermission = checkDataPermission;
