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

/**
 * 分页查询我的水票
 */
export async function pageGltUserTicket(params: GltUserTicketParam) {
  const res = await request.get<ApiResult<PageResult<GltUserTicket>>>(
    '/glt/glt-user-ticket/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询我的水票列表
 */
export async function listGltUserTicket(params?: GltUserTicketParam) {
  const res = await request.get<ApiResult<GltUserTicket[]>>(
    '/glt/glt-user-ticket',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加我的水票
 */
export async function addGltUserTicket(data: GltUserTicket) {
  const res = await request.post<ApiResult<unknown>>(
    '/glt/glt-user-ticket',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改我的水票
 */
export async function updateGltUserTicket(data: GltUserTicket) {
  const res = await request.put<ApiResult<unknown>>(
    '/glt/glt-user-ticket',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

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

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

/**
 * 根据id查询我的水票
 */
export async function getGltUserTicket(id: number) {
  const res = await request.get<ApiResult<GltUserTicket>>(
    '/glt/glt-user-ticket/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
