import http from './http.js'
import $API from '@/http/classApi.js'
import appConfig from '@/config/appConfig.js'
import {
	Base64
} from 'js-base64'
import infoBox from '@/commons/utils/infoBox.js'
function objectToUrlParams(obj) {
  let params = [];
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      let value = obj[key];
      let param = encodeURIComponent(key) + '=' + encodeURIComponent(value);
      params.push(param);
    }
  }
  return params.join('&');
}
/* 商品列表 */
export function $tbProduct(data) {
	return http.req('/api/tbProduct', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}

/* 添加商品 */
export function $addProduct(data) {
	return http.req('/api/tbProduct', {...data,shopId:uni.getStorageSync('shopId')}, 'POST')
}

/* 删除商品 */
export function $delProduct(ids) {
	return http.req('/api/tbProduct', ids, 'DELETE')
}


/* 更新商品相关 */
export function $updateProduct(data) {
	return http.req('/api/tbProduct', {...data,shopId:uni.getStorageSync('shopId')}, 'PUT')
}
/* 修改商品排序 */
export function $upProSort(data) {
	return http.req('/api/tbProduct/upProSort', {...data,shopId:uni.getStorageSync('shopId')}, 'POST')
}
/* 商品详情（单个商品） */
export function $getProductDetail(product,showLoading=true) {
	return http.req('/api/tbProduct/'+product, {shopId:uni.getStorageSync('shopId')}, 'GET',showLoading)
}

/* 设置热销商品 */
export function $goodsIsHot(data) {
	return http.req('/api/tbProduct/isHot', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}



/**
 * 商品分类列表
 */
export function $tbShopCategory(data) {
	return http.req('/api/tbShopCategory', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}
/**
 * 商品分类
 */
export const $productCategory=new $API('/api/tbShopCategory',http.req)


/**
 * 更新商品库存状态
 */
export function $updateProductStatus(data){
	return http.req('/api/stock/productStatus', {...data,shopId:uni.getStorageSync('shopId')}, 'PUT')
}

/**
 * 库存记录列表
 */
export function $getProductStockDetail(data){
	return http.req('/api/tbProductStockDetail/stock', {...data,shopId:uni.getStorageSync('shopId')}, 'POST')
}

/**
 * 库存记录变动数量
 */
export function $getProductStockDetailSum(data){
	return http.req('/api/tbProductStockDetail/sum', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}
/**
 * 新增盘点
 */
export function $addStocktakin(data){
	return http.req('/api/tbProductStocktakin', {...data,shopId:uni.getStorageSync('shopId')}, 'POST')
}
/**
 * 盘点记录查询
 */
export function $getStocktakin(data){
	return http.req('/api/tbProductStocktakin', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}


/**
 * 上下架商品
 */
export function $updateGrounding(data){
	const ajaxData={...data,shopId:uni.getStorageSync('shopId')}
	return http.req('/api/stock/grounding'+`?${objectToUrlParams(ajaxData)}`, ajaxData, 'PUT')
}



/* 商品单位列表 */
export function $tbShopUnit(data) {
	return http.req('/api/tbShopUnit', {...data,shopId:uni.getStorageSync('shopId')}, 'GET')
}

/* 商品规格 */
export const $productSpec=new $API('/api/tbProductSpec',http.req)



// v2 api start

/* 商品列表 V2 */
export function $tbProductV2(data) {
	return http.req('/api/tbProduct/list/v2', {...data,shopId:uni.getStorageSync('shopId')}, 'post')
}
/* 耗材与商品绑定关系 */
export function $tbProskuConV2(data) {
	return http.req('/api/tbProskuCon/V2', data, 'POST')
}

/* 修改商品相关(快捷接口） */
export function $updateProductData(data) {
	return http.req('/api/stock/updateProductData', data, 'POST')
}
/* 商品报损 */
export function $frmLoss(data) {
	return http.req('/api/tbProductStockDetail/frmLoss', {...data,shopId:uni.getStorageSync('shopId')}, 'POST')
}
// v2 api end