const _Http = getApp().globalData.http;
let sa_brandid = null,
	sys_enterpriseid = null;
import {
	fileList
} from "../../utils/FormatTheAttachment";
import currency from "../../utils/currency";

Page({
	data: {
		hidePrice: wx.getStorageSync('hidePrice'),
		badge: getApp().globalData.collectCount,
		loading: true,
		content: {
			sa_itemgroupid: null,
			spec: "", //规格
			color: "", //颜色
			material: "", //材质
			cheek: "", //边框
			dwidth: 0,
			dlength: 0,
		},
		detail: {
			spec: ""
		},
		cWidth: null, //宽定制方案
		cLength: null, //长定制方案
	},
	onLoad(options) {
		let data = JSON.parse(options.params);
		sa_brandid = data.sa_brandid;
		if (wx.getStorageSync('userrole') == '业务员') sys_enterpriseid = data.sys_enterpriseid
		this.data.content.sa_itemgroupid = data.sa_itemgroupid;
		this.setData({
			userrole: wx.getStorageSync('userrole')
		})
		this.getDetail(true);
	},
	/* 获取详情 */
	getDetail(init = false) {
		let content = this.data.content;
		if (sys_enterpriseid) content.sys_enterpriseid = sys_enterpriseid;
		_Http.basic({
			"id": "20221223165503",
			content
		}).then(res => {
			wx.hideLoading();
			console.log("商品详情", res)
			if (res.msg != '成功') return wx.showToast({
				title: res.msg,
				icon: "none"
			})
			if (res.data.item.length == 0) return wx.showToast({
				title: '未查询到商品',
				icon: "none",
				mask: true
			})
			const item = res.data.item[0];
			this.handleFiles(item.attinfos)
			const CNY = sum => currency(sum, {
				symbol: "￥",
				precision: 2
			}).format();
			item.gradeprice = CNY(item.gradeprice);
			item.marketprice = CNY(item.marketprice);
			this.setData({
				content,
				detail: item,
				specRows: res.data.specRows.reverse(),
				cheekRows: res.data.cheekRows,
				materialRows: res.data.materialRows,
				colorRows: res.data.colorRows,
				loading: false
			});
			//是否定制
			if (init) {
				if (item.iscustomsize == 1) {
					let cWidth = null,
						cLength = null;
					Promise.all([{
						"id": "20230707091603",
						"version": 1,
						"content": {
							"sa_sizecustomizedschemeid": item.lengthschemeid,
							date: Date.now() + 1
						}
					}, {
						"id": "20230707091603",
						"version": 1,
						"content": {
							"sa_sizecustomizedschemeid": item.widthschemeid,
							date: Date.now() + 2
						}
					}].map(v => _Http.basic(v))).then(s => {
						if (s.some(c => c.msg != '成功')) return wx.showToast({
							title: '定制方案获取失败',
							icon: "none"
						})
						if (item.lengthschemeid) cLength = s[0].data;
						if (item.widthschemeid) cWidth = s[1].data;
						this.setData({
							cWidth,
							cLength,
							"content.dwidth": cWidth.min || 0,
							"content.dlength": cLength.min || 0,
						});
					})
				} else {
					this.setData({
						cWidth: null,
						cLength: null,
						"content.dwidth": 0,
						"content.dlength": 0,
					});
				}
			}
		})
	},
	/* 预览媒体 */
	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
		})
	},
	/* 切换产品 */
	changeItemno(e) {
		const {
			value,
			valuename
		} = e.currentTarget.dataset,
			content = this.data.content;
		if (!value.flag) return;
		wx.showLoading({
			title: '加载中...',
			mask: true
		})
		content[valuename] = (content[valuename] == value.parm) ? "" : value.parm;
		this.getDetail(valuename == "spec")
	},
	/* 定制选项 */
	customParameter(e) {
		let {
			value,
			valuename
		} = e.currentTarget.dataset;
		this.data.content[valuename] = value;
		this.setData({
			content: this.data.content
		})
	},
	/* 定制步进器 */
	cahngeStepper(e) {
		const name = e.currentTarget.dataset.name,
			name2 = name == 'cWidth' ? "dwidth" : "dlength",
			content = this.data.content;
		if (e.type == 'plus') {
			content[name2] += 1
		} else if (e.type == 'minus') {
			content[name2] -= 1
		} else {
			const data = this.data[name];
			content[name2] = (e.detail.value - 0).toFixed(data.decimalplaces);
		}
		let item = this.data[name];
		if (content[name2] > item.max) content[name2] = item.max;
		if (content[name2] < item.min) content[name2] = item.min;
		this.setData({
			content
		})
	},
	clickBut() {
		this.data.detail.tradefield.length >= 2 ? wx.showToast({
			title: '请选择加入产品领域',
			icon: "none",
			duration: 3000
		}) : this.handleStorage(0);
	},
	/* 打开文档 */
	openDocument(e) {
		const {
			item
		} = e.currentTarget.dataset;
		console.log(item)
		wx.showLoading({
			title: '加载中...',
			mask: true,
		})
		wx.downloadFile({
			url: item.url,
			success: function (res) {
				const filePath = res.tempFilePath
				wx.openDocument({
					filePath: filePath,
					showMenu: true,
					fileType: item.postfix,
					success: function (res) {
						wx.hideLoading();
						console.log('打开文档成功')
					},
					fail(e) {
						console.log(e)
						wx.showToast({
							title: '打开失败',
							icon: "error",
							mask: true
						})
					}
				})
			},
			fail(e) {
				console.log(e)
				wx.showToast({
					title: '打开失败',
					icon: "error",
					mask: true
				})
			}
		})
	},
	/* 选择领域 */
	storage(e) {
		this.handleStorage(e.detail.value)
	},
	handleStorage(index) {
		let detail = this.data.detail,
			content = this.data.content;
		if (detail.iscustomsize == 1) {
			if (detail.widthschemeid != 0 && content.dwidth == 0) return wx.showToast({
				title: '请完成定制宽选项',
				icon: "none"
			})
			if (detail.lengthschemeid != 0 && content.dlength == 0) return wx.showToast({
				title: '请完成定制长选项',
				icon: "none"
			})
		}
		_Http.basic({
			"id": 20220924095102,
			"content": {
				sa_brandid,
				"itemid": detail.itemid, //货品id
				"qty": detail.orderminqty, //数量
				itemno: detail.itemno, //货品编号
				tradefield: detail.tradefield[index].tradefield,
				width: content.dwidth,
				length: content.dlength
			},
		}).then(res => {
			console.log("加入购物车", res)
			wx.showToast({
				title: res.msg == '成功' ? '加入成功' : res.msg,
				icon: "none"
			});
			if (res.msg == '成功') getApp().globalData.getCollectCount().then(badge => this.setData({
				badge
			}))
		})
	},
	/* 前往购物车 */
	toCollect(e) {
		getApp().globalData.changeBar({
			detail: "Collect"
		})
		wx.navigateBack();
	}
})