import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { ShopActivity, ShopActivityParam, ShopActivityFormField } from './model';

export type { ShopActivityFormField };

/**
 * 分页查询活动表
 */
export async function pageShopActivity(params: ShopActivityParam) {
  const res = await request.get<ApiResult<PageResult<ShopActivity>>>(
    '/shop/shop-activity/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询活动表列表
 */
export async function listShopActivity(params?: ShopActivityParam) {
  const res = await request.get<ApiResult<ShopActivity[]>>(
    '/shop/shop-activity',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加活动表
 */
export async function addShopActivity(data: ShopActivity) {
  const res = await request.post<ApiResult<unknown>>(
    '/shop/shop-activity',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改活动表
 */
export async function updateShopActivity(data: ShopActivity) {
  const res = await request.put<ApiResult<unknown>>(
    '/shop/shop-activity',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除活动表
 */
export async function removeShopActivity(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-activity/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除活动表
 */
export async function removeBatchShopActivity(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/shop/shop-activity/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询活动表
 */
export async function getShopActivity(id: number) {
  const res = await request.get<ApiResult<ShopActivity>>(
    '/shop/shop-activity/' + id
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
