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

/**
 * 分页查询结算
 */
export async function pageWorksheet(params: WorksheetParam) {
  const res = await request.get<ApiResult<PageResult<Worksheet>>>(
    "/tower/tower-worksheet/page",
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

/**
 * 根据id查询合同
 */
export async function  getWorksheetByWordType(projectId: number, workType: number) {
  const res = await request.post<ApiResult<Worksheet[]>>(
    "/tower/tower-worksheet/by-work-type", { projectId, workType }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

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

export async function addBatchWorksheet(data: Worksheet[]) {
  const res = await request.post<ApiResult<unknown>>(
    "/tower/tower-worksheet/batch",
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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