import { defHttp } from '/src/utils/http/axios';
import { useMessage } from '/src/hooks/web/useMessage';
import { getTenantById } from '@/views/system/tenant/tenant.api';
import { getAuthCache } from '@/utils/auth';
import { TENANT_ID } from '@/enums/cacheEnum';
import { RoleEnum } from '@/enums/roleEnum';
import { useUserStore } from '@/store/modules/user';
import { LoginInfo } from '#/store';
const userStore = useUserStore();
const { createConfirm } = useMessage();

enum Api {
  list = '/adweb/adwebSite/list',
  save = '/adweb/adwebSite/add',
  edit = '/adweb/adwebSite/edit',
  deleteOne = '/adweb/adwebSite/delete',
  deleteBatch = '/adweb/adwebSite/deleteBatch',
  importExcel = '/adweb/adwebSite/importExcel',
  exportXls = '/adweb/adwebSite/exportXls',
  gtmListUrl = 'gtm/add/list',
  getGoogleAdsAccount = '/marketing/googleads/account/get',
  bindGoogleAdsAccount = '/marketing/googleads/account/add', // 添加绑定接口地址
  getFaceBookAccount = '/marketing/facebook/account/get',
  bindFaceBookAccount = '/marketing/facebook/account/add',
  getSubscribePlan = '/adweb/adwebSite/getSubscribePlanBySite',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

export const gtmListUrl = Api.gtmListUrl;

/**
 * 列表接口
 * @param params
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 * @param params
 * @param handleSuccess
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 * @param handleSuccess
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新
 * @param params
 * @param isUpdate
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 获取Google Ads账号信息
 * @param params
 */
export const getGoogleAdsAccount = (params) => defHttp.get({ url: Api.getGoogleAdsAccount, params });

/**
 * 绑定Google Ads账号
 * @param params
 */
export const bindGoogleAdsAccount = (params) => defHttp.post({ url: Api.bindGoogleAdsAccount, params }, { joinParamsToUrl: true });

/**
 * 获取Google Ads账号信息
 * @param params
 */
export const getFaceBookAccount = (params) => defHttp.get({ url: Api.getFaceBookAccount, params });

/**
 * 绑定Google Ads账号
 * @param params
 */
export const bindFaceBookAccount = (params) => defHttp.post({ url: Api.bindFaceBookAccount, params }, { joinParamsToUrl: true });

export const isSohoeb2b = () => {
  return (
    userStore.getRoleList.includes(RoleEnum.ADWEB_CHANNEL_ADMIN) && userStore.getLoginInfo.tenantList.some((obj) => obj.name.includes('苏豪纺织集团'))
  );
};
export const getSubscribePlan = () => {
  const params: Record<string, any> = {
    siteId: localStorage.getItem('siteID'),
  };

  return defHttp.get({ url: Api.getSubscribePlan, params });
};
