/**
 * 支付宝商家端查询门店/设备交易数据
 */
import {
	Dialog
} from 'vant';
import config from '../../common/config.js'
import {
	requestGet,
	requestPost
} from '../../common/request.js'
export default {
	data() {
		return {
			path: "",
			// 门店初始化数据
			storeInfo: {}
		};
	},
	onLoad: function(options) {
		if (!options['token']) {
			Dialog({
				title: '系统提示',
				message: 'token为空！',
				confirmButtonText: "知道了"
			}).then(res => {
				window.close()
			})
			return;
		}
		this.UserLogin(options['token'])
		if (options['path']) {
			this.path = options['path'] || ""
		}
	},
	onShow: function() {

	},
	onunload: function() {

	},
	methods: {
		//登录获取用户信息
		async UserLogin(token) {
			let data = await requestPost('/cgi-xpay/app/Login', {
				grantType: 'GT_azCloud_business',
				code: token,
			})
			if (data['code'] != 200) {
				await Dialog({
					title: '温馨提示',
					message: data['message'],
					confirmButtonText: "知道了"
				})
				return
			}
			uni.setStorageSync('token', data.data['token'])

			// 门店初始化数据
			this.StoreData()
			// 门店商家列表
			this.StoreList()
		},
		//门店初始化数据
		async StoreData(token) {
			let data = await requestPost('/cgi-xpay/app/busi/StoreData', {
				"businessId": 0,
				"startDate": this.starttime.replaceAll("/", "-"),
				"endDate": this.endtime.replaceAll("/", "-"),
				"storeId": this.StoreInfo_['id'] || 0
			})
			if (data['code'] != 200) {
				await Dialog({
					title: '温馨提示',
					message: "门店初始化数据",
					confirmButtonText: "知道了"
				})
				return
			}
			let jsonData = JSON.parse(JSON.stringify(data.data))
			jsonData['offlineDeviceCnt'] = jsonData['offlineDeviceCnt'] || 0
			jsonData['onlineDeviceCnt'] = jsonData['onlineDeviceCnt'] || 0
			jsonData['totalAmount'] = ((jsonData['totalAmount'] || 0) / 100).toFixed(2)
			jsonData['totalCnt'] = jsonData['totalCnt'] || 0
			jsonData["cateringData"] = jsonData["cateringData"].map((v, index) => {
				if (v['mealType'] == "早餐") {
					v['mealTypePng'] = "xzf/h5/zao.png"
				}
				if (v['mealType'] == "中餐") {
					v['mealTypePng'] = "xzf/h5/wu.png"
				}
				if (v['mealType'] == "晚餐") {
					v['mealTypePng'] = "xzf/h5/wan.png"
				}
				if (v['mealType'] == "夜宵") {
					v['mealTypePng'] = "xzf/h5/ye.png"
				}
				if (v['mealType'] == "夜夜宵") {
					v['mealTypePng'] = "xzf/h5/yeye.png"
				}
				if (parseInt(jsonData['totalAmount']) === 0) {
					v['perc'] = 0
				} else {
					v['perc'] = parseFloat(v.dealAmount / jsonData['totalAmount'])
				}
				v['dealAmount'] = (v['dealAmount'] / 100).toFixed(2)
				return v
			})
			this.storeInfo = jsonData
		},
		//门商家列表
		async StoreList(token) {
			let data = await requestPost('/cgi-xpay/app/busi/StoreList', {})
			if (data['code'] != 200) {
				await Dialog({
					title: '温馨提示',
					message: "门商家列表查询失败",
					confirmButtonText: "知道了"
				})
				return
			}
			let jsonData = JSON.parse(JSON.stringify(data.data || []))
			this.stores = [{
				id: 0,
				storeName: "全部门店"
			}, ...jsonData]
			// 查询门店交易数据
			if (this.active === 0) {
				this.StoreFlows()
			}
		}
	}
}