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

/**
 * 分页查询送水订单
 */
export async function pageGltTicketOrder(params: GltTicketOrderParam) {
  const res = await request.get<ApiResult<PageResult<GltTicketOrder>>>(
    '/glt/glt-ticket-order/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询送水订单列表
 */
export async function listGltTicketOrder(params?: GltTicketOrderParam) {
  const res = await request.get<ApiResult<GltTicketOrder[]>>(
    '/glt/glt-ticket-order',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加送水订单
 */
export async function addGltTicketOrder(data: GltTicketOrder) {
  const res = await request.post<ApiResult<unknown>>(
    '/glt/glt-ticket-order',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改送水订单
 */
export async function updateGltTicketOrder(data: GltTicketOrder) {
  const res = await request.put<ApiResult<unknown>>(
    '/glt/glt-ticket-order',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除送水订单
 */
export async function removeGltTicketOrder(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/glt/glt-ticket-order/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除送水订单
 */
export async function removeBatchGltTicketOrder(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/glt/glt-ticket-order/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询送水订单
 */
export async function getGltTicketOrder(id: number) {
  const res = await request.get<ApiResult<GltTicketOrder>>(
    '/glt/glt-ticket-order/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
