import { defHttp } from '/@/utils/http/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/oroqen/product/list',
  save = '/oroqen/product/add',
  edit = '/oroqen/product/edit',
  deleteOne = '/oroqen/product/delete',
  deleteBatch = '/oroqen/product/deleteBatch',
  importExcel = '/oroqen/product/importExcel',
  exportXls = '/oroqen/product/exportXls',
  queryById = '/oroqen/product/queryById',
  listByCategory = '/oroqen/product/listByCategory',
  featured = '/oroqen/product/featured',
  hot = '/oroqen/product/hot',
  checkStock = '/oroqen/product/checkStock',
}

/**
 * 导出api
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

/**
 * 列表接口
 */
export const list = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    }
  });
};

/**
 * 保存或者更新
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params }, { isTransformResponse: false });
};

/**
 * 根据ID查询
 */
export const queryById = (params) => {
  return defHttp.get({ url: Api.queryById, params });
};

/**
 * 根据分类ID查询产品列表
 */
export const listByCategory = (params) => {
  return defHttp.get({ url: Api.listByCategory, params });
};

/**
 * 查询推荐产品
 */
export const getFeaturedProducts = (params) => {
  return defHttp.get({ url: Api.featured, params });
};

/**
 * 查询热销产品
 */
export const getHotProducts = (params) => {
  return defHttp.get({ url: Api.hot, params });
};

/**
 * 检查库存
 */
export const checkStock = (params) => {
  return defHttp.get({ url: Api.checkStock, params });
};