// 该文件用来存储localStorage 本地缓存的方法
/**
 * 操作用户token
 */
import CONFIG from '@/config'
import CryptoJS from './crypto-js.min.js'
const tName = 'ht-h5_1_test'
const isScrect = CONFIG.isPro
const Screct = {
	getData(key) {
		const text = uni.getStorageSync(key)
		if (text) {
			if (isScrect) {
				const localJosn = CryptoJS.AES.decrypt(text, key).toString(CryptoJS.enc.Utf8);
				return JSON.parse(localJosn).val
			}
			return text
			
		}
		return null
		 
	},
	setData(key, val) {
		let v = val
		if (isScrect) {
			const info = {
				val,
				ts: tName
			}
			v = CryptoJS.AES.encrypt(JSON.stringify(info), key).toString()
		}
		uni.setStorageSync(key, v)
		return v
	}
}

export function setToken(value) {
	return Screct.setData('token', value)
}
export function getToken() {
	return Screct.getData('token')
}
export function removeToken() {
	uni.removeStorageSync('token');
}
/**
 * 操作用户信息
 */

export function setUserInfo(value) {
  console.log('存储用户信息成功');
  return Screct.setData('userinfo', value)
}
export function getUserInfo() {
	return Screct.getData('userinfo')
}
export function removeUserInfo(){
	uni.removeStorageSync('userinfo');
}
/**
 * 项目主题颜色
 */

export function setConfig(value) {
  try {
    let config = JSON.stringify(value);
    uni.setStorageSync('config', config);
    console.log('存储主题成功');
  } catch (e) {
    return;
  }
}
export function getConfig() {
  let config = uni.getStorageSync('config');

  if (config) {
    return JSON.parse(config);
  }
  return null
}
/**
 * 获取推荐人id
 */

export function setRecommend(value) {
  uni.setStorageSync('recommend', value);
  console.log('recommend', value);
}
export function getRecommend() {
  let recommend = uni.getStorageSync('recommend');
  return recommend;
}
/**
 * 浏览历史

*/
export function setGoodsHistory(item) {
  try {
	   let goodsdata = getGoodsHistory()
	   const goodsLength = goodsdata.length
	   if (goodsLength) {
		   const hasGoods = goodsdata.some(v => v.id === item.id)
		   if (!hasGoods) {
			   goodsdata = goodsLength >= 20 ? goodsdata.slice(0, goodsLength - 1) : goodsdata
			   goodsdata.unshift(item)
			   console.log('新增浏览历史');
			   return Screct.setData('goodsHistory', goodsdata)
		   }
	   } else {
			console.log('新增浏览历史');
			const newValue = [item];
			return Screct.setData('goodsHistory', newValue)
	   }
  } catch (e) {
    return;
  }
}
export function getGoodsHistory(value) {
	return Screct.getData('goodsHistory') || []

}
/**
 * 存储商品信息
 */

export function setGoodsData(value) {
  return Screct.setData('goodsdata', value)
}

export function getGoodsData() {
	return Screct.getData('goodsdata')
} 
// 存储用户地址
export function setAddress(value) {
	return Screct.setData('address', value)
}
export function getAddress() {
	return Screct.getData('address')
}
export function removeAddress() {
  uni.removeStorageSync('address');
} // 设置分类跳转

export function setTbIndex(value) { //设置菜单栏选项
  uni.setStorageSync('tabIndex', value);
  console.log('tabIndex', value);
}
export function getTbIndex() { //获取菜单栏选项
  let tabIndex = uni.getStorageSync('tabIndex');
  return tabIndex;
}
export function removeTbIndex() { //移除菜单栏选项
  uni.removeStorageSync('tabIndex');
}
export function setlocation(value){ //存储位置信息
	try {
	  let newValue = JSON.stringify(value);
	  uni.setStorageSync('location', newValue);
	  console.log('存储地址信息成功', newValue);
	} catch (e) {
	  return;
	}
}
export function getlocation() { //获取位置信息
  let location = uni.getStorageSync('location');
  if (location) {
    return JSON.parse(location);
  }
}
export function setCart(value){ //模拟存储购物车数据
	try {
	  const data = getCart() || []
	  data.push(value)
	  return Screct.setData('cart', data)
	} catch (e) {
	  return;
	}
}
// 有问题 只是一个获取cart而已  当一个商品多个数量时 有问题进行修改 yang
export function getCart() { //模拟获取购物车数据   
  return Screct.getData('cart') || []
}
// 获取购物车数量 yang
export function getCartNumber() { 
	const cart = getCart()
	if(cart.length > 0){
		var num = 0;
		for(var i=0;i<cart.length;i++){
			
			// console.log(cart[i])
			num = cart[i].number +num
	
		}
		return num;
	}else{
		return 0;
	}
  
}
export function removeCart() { //模拟删除购物车数据
  uni.removeStorageSync('cart')
}
// 重置购物车数据 yang
export function resetCart(cart) { //
	return Screct.setData('cart', cart)
}

export function setPickaddress(value){ //存储收件地址
	try {
	  let newValue = JSON.stringify(value);
	  uni.setStorageSync('pickaddress', newValue);
	  console.log('存储收件地址信息成功', newValue);
	} catch (e) {
	  return;
	}
}
export function getPickaddress() { //获取收件地址
  let location = uni.getStorageSync('pickaddress');
  if (location) {
    return JSON.parse(location);
  }
}
export function removePickaddress() { //删除收件地址
  uni.removeStorageSync('pickaddress')
}
export function setToaddress(value){ //存储取件地址
	try {
	  let newValue = JSON.stringify(value);
	  uni.setStorageSync('toaddress', newValue);
	  console.log('存储取件地址信息成功', newValue);
	} catch (e) {
	  return;
	}
}
export function getToaddress() { //获取取件地址
  let location = uni.getStorageSync('toaddress');
  if (location) {
    return JSON.parse(location);
  }
}
export function removeToaddress() { //删除取件地址
  uni.removeStorageSync('toaddress')
}


export function setSalesGoods(value) {
  try {
	 return Screct.setData('SalesGoods', value)
  
  } catch (e) {
    return;
  }
}
export function getSalesGoods() {
  return Screct.getData('SalesGoods')
} 

export function setSalesGoodsItem(value) {
  try {
	  
	  return Screct.setData('SalesGoodsItem', value)
  
  } catch (e) {
    return;
  }
}
export function getSalesGoodsItem() {
	 return Screct.getData('SalesGoodsItem')
} 
export function rmSalesGoodsItem() {
  uni.removeStorageSync('SalesGoodsItem')
} 



// 由于无法调取接口 点击商品进入详情只能把数据暂存在缓存内 然后详情页获取该数据渲染
export function setGoodsDetails(value) {
  try {
	   console.log('存储商品信息成功');
	  return Screct.setData('GoodsDetails', value)
   
   
  } catch (e) {
    return;
  }
}
export function getGoodsDetails() {
  return Screct.getData('GoodsDetails')
} 


export function setCartDetails(value){ //模拟存储购物车数据(为了跳转详情渲染 只能再存一个cart数据)
	try {
	  let data = getCartDetails() || []
	  data.push(value)
	  let newValue = JSON.stringify(data);
	  uni.setStorageSync('CartDetails', newValue);
	} catch (e) {
	  return;
	}
}

export function getCartDetails() {
  let CartDetails = uni.getStorageSync('CartDetails');

  if (CartDetails) {
    return JSON.parse(CartDetails);
  }
} 

// 获取对应的商品详情
export function getCartDetails_goods_id(goods_id) {
  let CartDetails = uni.getStorageSync('CartDetails');
	

  if (CartDetails) {
	CartDetails = JSON.parse(CartDetails)
	for(let i=0;i<CartDetails.length;i++){
		
		if(goods_id == CartDetails[i].skuId){
			
			return CartDetails[i];
		}
	}
  }
} 