const _Http = getApp().globalData.http;

Page({
	data: {
		loading: true,
		"content": {
			"version": 1,
			nocache: true,
			"pageNumber": 1,
			"pageTotal": 1,
			"where": {
				"condition": ""
			},
			sort: []
		}
	},
	onLoad(options) {
		this.getList()
	},
	getList(init = false) {
		if (init.detail != undefined) init = init.detail;
		let content = this.data.content;
		if (init) content.pageNumber = 1;
		if (content.pageNumber > content.pageTotal) return;
		_Http.basic({
			"id": 20220103140003,
			content
		}).then(res => {
			console.log("活动列表", res)
			this.selectComponent('#ListBox').RefreshToComplete();
			let list = res.data.map(v => {
				v.surplus = Date.parse(new Date(v.enddate)) - Date.parse(new Date())
				return v
			});
			this.setData({
				list: res.pageNumber == 1 ? list : this.data.list.concat(list),
				"content.pageNumber": res.pageNumber + 1,
				"content.pageTotal": res.pageTotal,
				"content.sort": res.sort,
				loading: false
			})
		})
	},
	onClick({
		detail
	}) {
		console.log(detail)
		if (detail.type == '套餐活动') {
			wx.navigateTo({
				url: '/packageA/activity/bindingProduct/index?params=' + JSON.stringify({
					"id": 20230116094803,
					"version": 1,
					"content": {
						nocache: true,
						sa_brandid: detail.sa_brandid,
						sa_promotionid: detail.sa_promotionid,
						packagetype: detail.packagetype,
						"where": {
							"condition": "",
							packagetypemx: '',
						}
					}
				}),
			});
			getApp().globalData.handleSelect = function ({
				list
			}) {
				return handleSubmit(list)
			}.bind(this);
		} else {
			wx.navigateTo({
				url: '/packageA/activity/selectProduct/index?params=' + JSON.stringify({
					"id": 20230116094803,
					"version": 1,
					"content": {
						nocache: true,
						sa_brandid: detail.sa_brandid,
						sa_promotionid: detail.sa_promotionid,
						"where": {
							"condition": ""
						}
					}
				}),
			});
			getApp().globalData.handleSelect = function ({
				list
			}) {
				wx.showModal({
					title: '提示',
					content: '是否确定创建促销订单?',
					complete: ({
						confirm
					}) => {
						if (confirm) handleSubmit(list)
					}
				})
			}.bind(this);
		}

		function handleSubmit(list) {
			return new Promise((resolve) => {
				wx.showLoading({
					title: '生成中...',
					mask: true
				})
				_Http.basic({
					"id": 20221128183202,
					"content": {
						istool: 0,
						type: "促销订单",
						sa_orderid: 0,
						sa_accountclassid: detail.sa_accountclassid,
						rec_contactsid: 0,
						pay_enterpriseid: 0,
						sa_contractid: 0,
						sa_projectid: 0,
						sa_promotionid: detail.sa_promotionid,
						sa_brandid: detail.sa_brandid, //品牌ID
						type: "促销订单", //订单类型
						tradefield: detail.tradefield.join(','), //必选
						items: list.map(v => {
							return {
								sa_orderitemsid: 0,
								itemid: v.itemid,
								itemno: v.itemno,
								qty: v.qty,
								price: v.price,
								length: v.length,
								width: v.width,
								sa_promotion_itemsid: v.sa_promotion_itemsid,
								cheek: v.cheek,
								color: v.color,
								material: v.material,
								spec: v.spec,
								custom: v.custom
							}
						})
					}
				}).then(res => {
					wx.hideLoading()
					console.log("转化促销订单", res)
					resolve(res.msg == '成功')
					wx.showToast({
						title: res.msg != '成功' ? res.msg : '创建成功',
						icon: "none",
						mask: res.msg == '成功'
					});
					if (res.msg == '成功') setTimeout(() => {
						wx.redirectTo({
							url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
						});
						delete getApp().globalData.handleSelect
					}, 500)
				})
			})
		}
	},
	onSearch({
		detail
	}) {
		this.setData({
			"content.where.condition": detail
		});
		this.getList(true)
	},
	onReady() {
		this.setListHeight()
	},
	/* 设置页面高度 */
	setListHeight() {
		this.selectComponent("#ListBox").setHeight(".division", this);
	},
})