const _Http = getApp().globalData.http;

import {
	fileList
} from "../../utils/FormatTheAttachment";

Page({
	data: {
		loading: true,
		active: "",
		list: [],
		groupList: [],
		pageNumber: 1,
		pageTotal: 1,
	},
	onLoad(options) {
		if (options.id) this.setData({
			sa_promotionid: options.id
		})
		this.getDetail();
	},
	/* 获取详情 */
	getDetail() {
		_Http.basic({
			"id": "20221230144703",
			"version": 1,
			"content": {
				"sa_promotionid": this.data.sa_promotionid
			}
		}).then(res => {
			if (res.msg != '成功') return wx.showToast({
				title: res.msg,
				icon: "none"
			})
			this.handleFiles(res.data.attinfos)
			this.setData({
				detail: res.data,
				loading: false
			});
			this.getProductList();
		})
	},
	/* 切换商品tab */
	tabChange(e) {
		this.setData({
			active: e.detail.name,
			pageNumber: 1,
			pageTotal: 1
		});
		this.getProductList();
	},
	/* 获取产品列表 */
	getProductList() {
		let pageNumber = this.data.pageNumber,
			pageTotal = this.data.pageTotal;
		if (pageNumber > pageTotal) return;
		_Http.basic({
			"id": 20230116094803,
			"version": 1,
			"content": {
				sa_promotionid: this.data.sa_promotionid,
				"sa_promotion_itemgroupid": this.data.active,
				pageNumber,
				pageTotal,
				"where": {
					"condition": ""
				}
			}
		}).then(res => {
			console.log('活动产品列表', res)
			if (res.msg != '成功') return wx.showToast({
				title: res.msg,
				icon: "none"
			})
			res.data = res.data.map(value => {
				if (value.attinfos.length != 0) {
					value.attinfos = fileList(value.attinfos)
					let image = value.attinfos.find(v => v.fileType == "image");
					value.cover = image ? image.cover : "";
				}
				return value;
			})
			this.setData({
				list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
				pageNumber: res.pageNumber + 1,
				pageTotal: res.pageTotal
			})
		})
	},
	/* 预览图片 */
	viewImage(e) {
		const {
			file
		} = e.currentTarget.dataset;
		if (file.length) wx.previewMedia({
			sources: file.filter(value => ['image', 'vadio'].includes(value.fileType)).map(v => {
				return {
					url: v.url,
					type: v.fileType
				}
			}),
			current: 0,
			showmenu: true
		})
	},
	/* 预览媒体 */
	viewMedias(e) {
		const {
			index,
			type
		} = e.currentTarget.dataset;
		wx.previewMedia({
			current: index,
			sources: type == 'image' ? this.data.files.viewImages : this.data.files.viewVideos,
		})
	},
	/* 处理附件 */
	handleFiles(arr) {
		let files = {
				images: [],
				viewImages: [],
				videos: [],
				viewVideos: [],
				files: []
			},
			list = fileList(arr);
		list.forEach(v => {
			switch (v.fileType) {
				case "video":
					files.videos.push(v)
					files.viewVideos.push({
						url: v.url,
						type: "video",
						poster: v.subfiles[0].url
					})
					break;
				case "image":
					files.images.push(v)
					files.viewImages.push({
						url: v.url,
						type: "image"
					})
					break;
				default:
					files.files.push(v)
					break;
			}
		});
		this.setData({
			files
		})
	},
	/* 去下单 */
	clickBut() {
		wx.navigateTo({
			url: '/packageA/activity/selectProduct/index?params=' + JSON.stringify({
				"id": 20230116094803,
				"version": 1,
				"content": {
					nocache: true,
					sa_promotionid: this.data.sa_promotionid,
					sa_promotion_itemgroupid: this.data.active,
					"where": {
						"condition": ""
					}
				}
			}),
		});
		getApp().globalData.handleSelect = this.creadedOrderForm.bind(this);
	},
	creadedOrderForm({
		list
	}) {
		console.log(list)
		let that = this;
		wx.showModal({
			title: '提示',
			content: '是否确定创建促销订单?',
			complete: (res) => {
				if (res.confirm) _Http.basic({
					"id": 20221108111402,
					content: {
						sa_orderid: 0,
						sa_accountclassid: that.data.detail.sa_accountclassid,
						rec_contactsid: 0,
						pay_enterpriseid: 0,
						sa_contractid: 0,
						sa_projectid: 0,
						sa_promotionid: that.data.detail.sa_promotionid,
						"sa_brandid": that.data.detail.sa_brandid, //品牌ID
						"type": "促销订单", //订单类型
						"tradefield": that.data.detail.tradefield, //必选

					}
				}).then(res => {
					console.log("创建促销订单", res);
					wx.showToast({
						title: res.msg != '成功' ? res.msg : '创建成功',
						icon: "none"
					});
					/* 绑定产品 */
					_Http.basic({
						"id": 20221109093602,
						"content": {
							"sa_orderid": res.data.sa_orderid,
							"sa_contractid": res.data.sa_contractid,
							"sys_enterpriseid": res.data.sys_enterpriseid,
							"type": "促销订单",
							"items": list.map(v => {
								return {
									"sa_orderitemsid": 0,
									"itemid": v.itemid,
									"qty": v.qty,
									price: v.price
								}
							})
						}
					}).then(s => {
						if (s.msg == '成功') setTimeout(() => {
							wx.redirectTo({
								url: '/packageA/orderForm/detail?id=' + res.data.sa_orderid,
							});
						}, 500)
					})
				})
			}
		})
	},
	onReachBottom() {
		this.getProductList();
	}
})