import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { CreditMpCustomer, CreditMpCustomerParam } from './model';

/**
 * 分页查询小程序端客户
 */
export async function pageCreditMpCustomer(params: CreditMpCustomerParam) {
  const res = await request.get<ApiResult<PageResult<CreditMpCustomer>>>(
    '/credit/credit-mp-customer/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询小程序端客户列表
 */
export async function listCreditMpCustomer(params?: CreditMpCustomerParam) {
  const res = await request.get<ApiResult<CreditMpCustomer[]>>(
    '/credit/credit-mp-customer',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加小程序端客户
 */
export async function addCreditMpCustomer(data: CreditMpCustomer) {
  const res = await request.post<ApiResult<unknown>>(
    '/credit/credit-mp-customer',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改小程序端客户
 */
export async function updateCreditMpCustomer(data: CreditMpCustomer) {
  const res = await request.put<ApiResult<unknown>>(
    '/credit/credit-mp-customer',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除小程序端客户
 */
export async function removeCreditMpCustomer(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-mp-customer/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除小程序端客户
 */
export async function removeBatchCreditMpCustomer(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/credit/credit-mp-customer/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询小程序端客户
 */
export async function getCreditMpCustomer(id: number) {
  const res = await request.get<ApiResult<CreditMpCustomer>>(
    '/credit/credit-mp-customer/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
