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

/**
 * 分页查询合同设备
 */
export async function pageContract(params: ContractSettleEquipmentParam) {
  const res = await request.get<ApiResult<PageResult<ContractSettleEquipment>>>(
    '/tower/tower-contract-settle-equipment/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询合同设备列表
 */
export async function listContractSettleEquipment(params?: ContractSettleEquipmentParam) {
  const res = await request.get<ApiResult<ContractSettleEquipment[]>>(
    '/tower/tower-contract-settle-equipment',
    {
      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 getContractSettleEquipment(id: number) {
  const res = await request.get<ApiResult<ContractSettleEquipment>>(
    '/tower/tower-contract-settle-equipment/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

/**
 * 批量添加合同设备
 */
export async function addBatchContractSettleEquipment(data: ContractSettleEquipment[]) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-contract-settle-equipment/batch',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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