import request from '@/utils/request';
import type { ShopDealerPoster, ShopDealerPosterParam } from './model';

/**
 * 分页查询分销商海报设置
 */
export async function pageShopDealerPoster(params: ShopDealerPosterParam) {
  const res = await request.get('/shop/dealer/poster/page', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询分销商海报设置列表
 */
export async function listShopDealerPoster(params?: ShopDealerPosterParam) {
  const res = await request.get('/shop/dealer/poster/list', { params });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询分销商海报设置
 */
export async function getShopDealerPoster(id: number) {
  const res = await request.get('/shop/dealer/poster/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 获取当前海报配置
 */
export async function getCurrentPosterConfig() {
  const res = await request.get('/shop/dealer/poster/config');
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加分销商海报设置
 */
export async function addShopDealerPoster(data: ShopDealerPoster) {
  const res = await request.post('/shop/dealer/poster', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改分销商海报设置
 */
export async function updateShopDealerPoster(data: ShopDealerPoster) {
  const res = await request.put('/shop/dealer/poster', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 保存海报配置
 */
export async function savePosterConfig(data: any) {
  const res = await request.post('/shop/dealer/poster/config', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除分销商海报设置
 */
export async function removeShopDealerPoster(id: number) {
  const res = await request.delete('/shop/dealer/poster/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除分销商海报设置
 */
export async function removeBatchShopDealerPoster(ids: (number | undefined)[]) {
  const res = await request.delete('/shop/dealer/poster/batch', { data: ids });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 生成海报
 */
export async function generatePoster(userId: number, config?: any) {
  const res = await request.post('/shop/dealer/poster/generate', { userId, config });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 上传海报背景图片
 */
export async function uploadPosterBackground(file: File) {
  const formData = new FormData();
  formData.append('file', file);

  const res = await request.post('/shop/dealer/poster/upload/background', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });

  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}
