import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
// @ts-ignore
import type { HualalaCardBenefits, HualalaCardBenefitsParam } from './model';
/**
 * 分页查询会员权益
 */
export async function pageHualalaCardBenefits(
  params: HualalaCardBenefitsParam
) {
  const res = await request.get<ApiResult<PageResult<HualalaCardBenefits>>>(
    '/apps/hualala-card-benefits/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询会员权益列表
 */
export async function listHualalaCardBenefits(
  params?: HualalaCardBenefitsParam
) {
  const res = await request.get<ApiResult<HualalaCardBenefits[]>>(
    '/apps/hualala-card-benefits',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加会员权益
 */
export async function addHualalaCardBenefits(data: HualalaCardBenefits) {
  const merchantCode = localStorage.getItem('merchantCode');
  if (merchantCode !== 'null') {
    data.merchantCode = String(merchantCode);
  }
  const res = await request.post<ApiResult<unknown>>('/apps/hualala-card-benefits', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改会员权益
 */
export async function updateHualalaCardBenefits(data: HualalaCardBenefits) {
  const res = await request.put<ApiResult<unknown>>('/apps/hualala-card-benefits', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除会员权益
 */
export async function removeHualalaCardBenefits(id?: number) {
  const res = await request.delete<ApiResult<unknown>>('/apps/hualala-card-benefits/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除会员权益
 */
export async function removeBatchHualalaCardBenefits(
  data: (number | undefined)[]
) {
  const res = await request.delete<ApiResult<unknown>>(
    '/apps/hualala-card-benefits/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/apps/hualala-card-benefits/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
