import request from "@/utils/request";
import type { ApiResult, PageResult } from "@/api";
import type { ContractFile, ContractFileParam } from "./model";

/**
 * 分页查询合同文件
 */
export async function pageContract(params: ContractFileParam) {
  const res = await request.get<ApiResult<PageResult<ContractFile>>>(
    "/tower/tower-contract/page",
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询合同文件列表
 */
export async function listContractFile(params?: ContractFileParam) {
  const res = await request.get<ApiResult<ContractFile[]>>(
    "/tower/tower-contract-file",
    {
      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 getContractFile(id: number) {
  const res = await request.get<ApiResult<ContractFile>>(
    "/tower/tower-contract-file/" + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加合同文件
 */
export async function addContractFile(data: ContractFile) {
  const res = await request.post<ApiResult<unknown>>(
    "/tower/tower-contract-file",
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改合同文件
 */
export async function updateContractFile(data: ContractFile) {
  const res = await request.put<ApiResult<unknown>>(
    "/tower/tower-contract-file",
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量修改合同文件
 */
export async function updateBatchContractFile(data: ContractFile[]) {
  const res = await request.put<ApiResult<unknown>>(
    "/tower/tower-contract-file/batch",
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量添加合同文件
 */
export async function addBatchContractFile(data: ContractFile[]) {
  const res = await request.post<ApiResult<unknown>>(
    "/tower/tower-contract-file/batch",
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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