// 代课下单
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'

const request = http.request


//就餐形式，默认堂食后付费
const useType = 'dine-in-after'

function getUseType() {
	const type = uni.getStorageSync("useType")
	return type ? type : useType
}
// import {
// 	webscoketUtill
// } from '../websock.js'
// let wxObj = null
// wx初始化购物车
// export function getWXCart(params) {
// 	let wxUrl = 'ws://192.168.1.31:2348/?' + objectToString(params)
// 	wxObj = new webscoketUtill(wxUrl,3000,9,(e)=>{
// 		console.log('收到消息');
// 		console.log(e);
// 	})
// 	return uni.getStorageSync('wxList')
// }
// 新增\删除\修改到购物车
// export function addWXCart(params) {
// 	wxObj.ws.send(params)
// }


function objectToString(obj) {
	let result = '';
	for (const key in obj) {
		if (obj.hasOwnProperty(key)) {
			result += `${key}=${obj[key]}&`;
		}
	}
	// 去掉最后一个多余的 &
	return result.slice(0, -1);
}
/**
 * 获取当前台桌订单信息
 * @returns
 */
export function getCart(params) {
	return request({
		url: `/api/place/cart`,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...params
		}
	});
}
/**
 * 已上架商品列表
 * @returns
 */
export function getGoodsLists(params, showLoading = true) {
	return request({
		url: `/product/admin/product/list`,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...params
		},
		showLoading
	});
}

/**
 * 点单
 * @returns
 */
export function addCart(data) {
	return request({
		url: `/api/place/addCart`,
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}

/**
 * 清空购物车/支付订单
 * @returns
 */
export function $clearCart(data) {
	return request({
		url: `/api/place/clearCart`,
		method: "delete",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}

/**
 * 删除购物车某个商品
 * @returns
 */
export function $removeCart(data) {
	return request({
		url: `/api/place/removeCart`,
		method: "delete",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}
/**
 * 更新规格
 * @returns
 */
export function $updateCart(data) {
	return request({
		url: `/api/place/updateCart`,
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
/**
 * 批量打包
 * @returns
 */
export function $allPack(data) {
	return request({
		url: `/api/place/pack`,
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
/**
 * 获取取餐号
 * @returns
 */
export function $getMasterId(data) {
	return request({
		url: `/api/place/masterId`,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}
/**
 * 支付方式获取
 * @returns
 */
export function $getPayType(data) {
	return request({
		url: `/account/admin/payType`,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
/**
 * 创建订单
 * @returns
 */
export function $createOrder(data) {
	return request({
		url: `/api/place/order`,
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}


/**
 * 挂起订单
 * @returns
 */
export function $cacheOrder(data) {
	return request({
		url: `/api/place/pending`,
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}

/**
 * 获取已挂起订单
 * @returns
 */
export function $getCacheOrder(data) {
	return request({
		url: `/api/place/pending/cart`,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}

// 会员点单/取消会员点单
export function $setUser(data) {
	return request({
		url: `/api/place/updateVip`,
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
// 删除订单
export function $delOrder(data) {
	return request({
		url: `/api/place/order`,
		method: "delete",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
// 支付订单
export function $payOrder(data) {
	return request({
		url: '/api/place/pay',
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
//退单

export function $returnCart(data) {
	return request({
		url: '/api/place/returnCart',
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
// 选择台桌
export function $choseTable(data) {
	return request({
		url: '/api/place/choseTable',
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
// 用餐人数

export function $choseCount(data) {
	return request({
		url: '/api/place/choseCount',
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			useType: getUseType(),
			...data
		}
	});
}

// 批量生成台桌
export function $fastCreateTable(data) {
	return request({
		url: '/api/tbShopTable/generate',
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

//打印当前台桌订单
export function $printOrder(data) {
	return request({
		url: '/api/place/printOrder',
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
//打印当前台桌菜品

export function $printDishes(data) {
	return request({
		url: '/api/place/printDishes',
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

// 就餐模式切换
export function $changeUseType(data) {
	return request({
		url: '/api/place/choseModel',
		method: "put",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

// 退款
export function $returnOrder(data) {
	return request({
		url: '/api/place/returnOrder',
		method: "post",
		data: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

//获取订单可用优惠券
export function $activateByOrderId(data) {
	return request({
		url: '/api/tbShopCoupon/activateByOrderId',
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

//会员积分列表
export function $returnMemberPointsList(data) {
	return request({
		url: '/api/points/member-points/page',
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}

// 会员积分账户信息
export function $returnMemberPoints(memberId) {
	return request({
		url: '/api/points/member-points/' + memberId,
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
//002-获取订单可用积分及抵扣金额（支付页面使用）
export function $calcUsablePoints(data) {
	return request({
		url: '/api/points/member-points/calc-usable-points',
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}
// 003-根据积分计算可抵扣金额
export function $calcDeDuctionPoints(data) {
	return request({
		url: '/api/points/member-points/calc-deduction-amount',
		method: "get",
		params: {
			shopId: uni.getStorageSync("shopId"),
			...data
		}
	});
}