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

/**
 * 分页查询结算
 */
export async function pageInvoicingSettle(params: InvoicingSettleParam) {
  const res = await request.get<ApiResult<PageResult<InvoicingSettle>>>(
    '/tower/tower-invoicing-settle/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询合同列表
 */
export async function listInvoicingSettle(params?: InvoicingSettleParam) {
  const res = await request.get<ApiResult<InvoicingSettle[]>>(
    '/tower/tower-invoicing-settle',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询合同
 */
export async function getInvoicingSettle(id: number) {
  const res = await request.get<ApiResult<InvoicingSettle>>(
    '/tower/tower-invoicing-settle/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加合同
 */
export async function addInvoicingSettle(data: InvoicingSettle) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-invoicing-settle',
    data
  );
  if (res.data.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改合同
 */
export async function updateInvoicingSettle(data: InvoicingSettle) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-invoicing-settle',
    data
  );
  if (res.data.code === 0) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改合同
 */
export async function updateBatchInvoicingSettle(data: InvoicingSettle[]) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-invoicing-settle/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
export async function addBatchInvoicingSettle(data: InvoicingSettle[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-invoicing-settle/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除合同
 */
export async function removeInvoicingSettle(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-InvoicingSettle/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除合同
 */
export async function removeBatchInvoicingSettle(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-invoicing-settle/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
