import { getPageData } from '../base';
import {
  SendResetPasswordEmailMsg,
  TenantAdminPageRequestParams,
  TenantDTO,
  TenantPageRequestParams,
  TenantRequestDTO,
  UserDTO,
  tenantProfileDTO,
} from './tenantInfo';
import { defHttp } from '/@/utils/http/axios';
import { BasicPageParams } from '/@/api/model/baseModel';
import { PaginationResult } from '/#/axios';
export type QueryTenantProfilesParam = BasicPageParams & OtherParams;
export type DeleteTenantProfilesParam = OtherParams;
export type OtherParams = {
  sortProperty?: string;
  sortOrder?: string;
  tenantProfield?: string;
  textSearch?: any;
};

enum Api {
  userPage = '/user/tenant/page',
  tenantPage = '/admin/tenant/page',
  saveTenantAdmin = '/user/save_tenant_admin',
  updateOrCreateTenant = '/admin/tenant/updateOrCreateTenant',
  deleteTenant = '/admin/tenant',
  resetTenantAdminPassword = '/tenant/reset_password/',
  sendMessageOrEmail = '/tenant/sendRestPasswordMsg',
  deleteTenantAdmin = '/admin/user/deleteTenantAdmin',
  getTenantRoles = '/admin/tenant/roles/',
  postAddTenantProfile = '/tenantProfile',
  getTenantProfile = '/tenant_profiles',
  deleteTenantProfile = '/tenantProfile',
  setTenantProfile = '/tenantProfile',
}

export async function deleteTenantProfileApi(ids: string) {
  await defHttp.delete(
    {
      url: Api.deleteTenantProfile + '/' + ids,
    },
    {
      joinPrefix: false,
    }
  );
}

export const getTableTenantProfileApi = (params?: QueryTenantProfilesParam) => {
  const param = {
    ...params,
    orderFiled: 'createdTime',
  };
  return defHttp.get<PaginationResult<{ default: boolean; id: { id: string } }>>({
    url: Api.getTenantProfile,
    params: param,
  });
};

//设置该租户为默认
export const setTenantProfileIsDefaultApi = (id: string, v, params?: {}) => {
  return defHttp.post(
    {
      url: Api.setTenantProfile + '/' + id + '/' + v,
      params,
    },
    {
      joinPrefix: false,
    }
  );
};

export const selectTenantProfileApi = async (params?: QueryTenantProfilesParam) => {
  const { items, total } = await getTableTenantProfileApi(params);
  items.forEach((item) => {
    item.label = item.name;
    item.value = item.id.id;
  });
  return { items, total };
};

export async function saveTenantProfileApi(params: tenantProfileDTO) {
  await defHttp.post(
    {
      params: params,
      url: Api.postAddTenantProfile,
    },
    {
      joinPrefix: false,
    }
  );
}

export function getTenantPage(params: TenantPageRequestParams) {
  return getPageData<TenantDTO>(params, Api.tenantPage);
}

//分页
export const getTenantAdminPage = (params: TenantAdminPageRequestParams) => {
  return defHttp.get<TenantAdminPageRequestParams>({
    url: Api.userPage,
    params,
  });
};

export async function saveTenantAdmin(params: UserDTO) {
  await defHttp.post({
    params,
    url: Api.saveTenantAdmin,
  });
}

export async function updateOrCreateTenant(params: TenantRequestDTO) {
  await defHttp.post({
    url: Api.updateOrCreateTenant,
    params,
  });
}

export async function deleteTenant(tenantIds: Array<string>) {
  await defHttp.delete({
    url: Api.deleteTenant,
    data: {
      ids: tenantIds,
    },
  });
}

export async function deleteTenantAdmin(adminIds: Array<string>) {
  await defHttp.delete({
    url: Api.deleteTenantAdmin,
    data: {
      ids: adminIds,
    },
  });
}

export async function resetPassword(params: string) {
  await defHttp.post({
    url: Api.resetTenantAdminPassword + params,
  });
}

export async function sendMessageOrEmail(params: SendResetPasswordEmailMsg) {
  await defHttp.post({
    params,
    url: Api.sendMessageOrEmail,
  });
}

export function getTenantRoles(tenantCode: string) {
  return defHttp.get({
    params: tenantCode,
    url: Api.getTenantRoles,
  });
}
