import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { TowerPurchase, TowerPurchaseParam } from './model';
// const merchantCode = String(localStorage.getItem('merchantCode'));

/**
 * 分页查询采购入库
 */
export async function pageTowerPurchase(params: TowerPurchaseParam) {
  const res = await request.get<ApiResult<PageResult<TowerPurchase>>>(
    '/tower/tower-purchase/page',
    {
      params
    }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询采购入库列表
 */
export async function listTowerPurchase(params?: TowerPurchaseParam) {
  const res = await request.get<ApiResult<TowerPurchase[]>>(
    '/tower/tower-purchase',
    {
      params
    }
  );
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加采购入库
 */
export async function addTowerPurchase(data: TowerPurchase) {
  const res = await request.post<ApiResult<unknown>>(
    '/tower/tower-purchase',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改采购入库
 */
export async function updateTowerPurchase(data: TowerPurchase) {
  const res = await request.put<ApiResult<unknown>>(
    '/tower/tower-purchase',
    data
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除采购入库
 */
export async function removeTowerPurchase(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-purchase/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除采购入库
 */
export async function removeBatchTowerPurchase(data: (number | undefined)[]) {
  const res = await request.delete<ApiResult<unknown>>(
    '/tower/tower-purchase/batch',
    {
      data
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 检查IP是否存在
 */
export async function checkExistence(
  field: string,
  value: string,
  id?: number
) {
  const res = await request.get<ApiResult<unknown>>(
    '/tower/tower-purchase/existence',
    {
      params: { field, value, id }
    }
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
