import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopInvoiceTitle, ShopInvoiceTitleParam, ShopInvoiceRecord, ShopInvoiceRecordParam, InvoiceApplyRequest } from './model';

/**
 * 分页查询发票抬头
 */
export async function pageShopInvoiceTitle(params: ShopInvoiceTitleParam) {
  const res = await request.get<ApiResult<PageResult<ShopInvoiceTitle>>>(
    '/shop/shop-invoice-title/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询发票抬头列表
 */
export async function listShopInvoiceTitle(params?: ShopInvoiceTitleParam) {
  const res = await request.get<ApiResult<ShopInvoiceTitle[]>>(
    '/shop/shop-invoice-title',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 添加发票抬头
 */
export async function addShopInvoiceTitle(data: ShopInvoiceTitle) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-invoice-title',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 修改发票抬头
 */
export async function updateShopInvoiceTitle(data: ShopInvoiceTitle) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-invoice-title',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 删除发票抬头
 */
export async function removeShopInvoiceTitle(id?: number) {
  const res = await request.del<ApiResult<unknown>>(
    '/shop/shop-invoice-title/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询发票抬头
 */
export async function getShopInvoiceTitle(id: number) {
  const res = await request.get<ApiResult<ShopInvoiceTitle>>(
    '/shop/shop-invoice-title/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 设置默认发票抬头
 */
export async function setDefaultInvoiceTitle(id: number) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-invoice-title/set-default/' + id
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 分页查询发票记录
 */
export async function pageShopInvoiceRecord(params: ShopInvoiceRecordParam) {
  const res = await request.get<ApiResult<PageResult<ShopInvoiceRecord>>>(
    '/shop/shop-invoice-record/page',
    params
  );
  if (res.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 查询发票记录列表
 */
export async function listShopInvoiceRecord(params?: ShopInvoiceRecordParam) {
  const res = await request.get<ApiResult<ShopInvoiceRecord[]>>(
    '/shop/shop-invoice-record',
    params
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 申请发票
 */
export async function applyInvoice(data: InvoiceApplyRequest) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-invoice-record/apply',
    data
  );
  if (res.code === 0) {
    return res.message;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 根据id查询发票记录
 */
export async function getShopInvoiceRecord(id: number) {
  const res = await request.get<ApiResult<ShopInvoiceRecord>>(
    '/shop/shop-invoice-record/' + id
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}

/**
 * 获取用户可开票订单列表
 */
export async function listInvoiceableOrders() {
  const res = await request.get<ApiResult<ShopInvoiceRecord[]>>(
    '/shop/shop-invoice-record/invoiceable-orders'
  );
  if (res.code === 0 && res.data) {
    return res.data;
  }
  return Promise.reject(new Error(res.message));
}
