<template>
	<view class="container">
		<view style="position: relative;">
			<uni-card :title="sales.userName == null ?'暂无':sales.userName" thumbnail="/static/images/dingdan/dd0.png"></uni-card>
		</view>
		<view class="tui-order-item boxbg">
			<view class="content-box">
				<view v-for="(item,index) in newData" :key="index" class="shop-box" v-if="payType1 == '2'"
					style="border-top:5px solid #eee;" :style="{color:item.checked?'#777':'#000'}">
					<view style="display: flex;">
						<view style="height: 100rpx;line-height: 100rpx;width: 80%;" :style="{color:item.checked?'#777':'#FF5809'}">
							<view style="width: 70%;text-align:center;float: left;">
								{{item.productSn == null ? '暂无':item.productSn}}({{item.productName ? item.productName :''}})
							</view>
							<view style="width: 30%;text-align:center;float: left;">
								数量:{{item.productCount == null ? '暂无':item.productCount}}
							</view>
						</view>
					</view>
					<view style="margin:20rpx auto;text-align: center;">
						<view style="display: flex;height: 50rpx;line-height: 50rpx;border-top: 1px solid #eee;border-bottom: 1px solid #eee;">
							<view style="width: 200rpx;border-right: 1px solid #eee;border-left: 1px solid #eee;">颜色</view>
							<view v-for="(item1,index1) in newData[index].sizeList" :key='index1' style="flex: 1;border-right: 1px solid #eee;">
								<text>{{item1}}</text>
							</view>
						</view>
						<view :style="{color:item.checked?'#777':value[0].indexOf('退') != -1?'red':'#000'}"
							v-for="(value,key) in Array.from(newData[index].sizePrintList)" :key='key'
							style="display: flex;height: 50rpx;line-height: 50rpx;border-bottom: 1px solid #eee;">
							<view style="width: 200rpx;border-right: 1px solid #eee;border-left: 1px solid #eee;">
								{{value[0]}}
							</view>
							<view v-for="(item2,index2) in newData[index].sizeList" :key='index2' style="flex: 1;border-right: 1px solid #eee;">
								<view>
									{{(value[1].get(item2) == undefined || (value[1].get(item2) != undefined && value[1].get(item2).split(":")[0] == undefined))?'-' : value[1].get(item2).split(":")[0]}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="tui-tabbar">
			<view class="tui-btn-mr" @click="distribution">确认配货</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				sales: {},
				order: {},
				list: [],
				newData: [],
				lettrList: ['XS', 'S', 'M', 'L', 'XL', '2XL', 'XXL', '3XL', 'XXXL', '4XL', 'XXXXL', '5XL', 'XXXXXL', '6XL',
					'XXXXXXL', '7XL', 'XXXXXXXL'
				],
				isReturn: false,
				inventoryList: [],
				printHeight: 0,
				smailDcl: 1,
				
				printHeight1: 0,
				printWidth1: 0,
				payType1: 2,
				carList:[]
			}
		},
		onLoad(option) {
			var that = this;
			this.saleId = option.id
			if (option.id != null && option.id != undefined && option.id != "") {
				this.tui.request("/app/sale/get/" + option.id, "GET", {
					customerId: ""
				}, false, false).then((res) => {
					if (res.code == 200 && res.result != null && res.result != undefined) {
						
						this.order = res.result.customer

						if(res.result.customer.name == '散客'){
							this.isSelfOrder = true
						}
						this.sales = res.result.sale
						this.list = res.result.saleDetailDTOList
						for (var i = 0; i < that.list.length; i++) {
							var isZiMu = false
							var isNumber = false
							var colorList = new Map()
							var sizeNumList = []
							let notNum = []
							that.list[i].checked = false
							if (that.list[i].stockLogList1.length > 0) {
								for (var m = 0; m < that.list[i].stockLogList1.length; m++) {
									var attr = that.list[i].stockLogList1[m].attributeList
									var newObj = JSON.parse(attr)
									for (var as in newObj) {
										if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g, '')) ==
											'Letter') {
											isZiMu = true
										}
										if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g, '')) ==
											'Digit') {
											isNumber = true
										}
										if (as == '尺码' && sizeNumList.indexOf(newObj[as].replace(/码/g, '')) ==
											-1) {
											if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g,
													'')) == 'Digit') {
												sizeNumList.push(newObj[as].replace(/码/g, ''))
											} else {
												if (notNum.indexOf(newObj[as].replace(/码/g, '')) == -1) {
													notNum.push(newObj[as].replace(/码/g, ''))
												}
											}

										}
										if (as == '颜色') {
											if (colorList.has(newObj[as])) {
												colorList.get(newObj[as]).push(that.list[i].stockLogList1[m])
											} else {
												let listChild = []
												listChild.push(that.list[i].stockLogList1[m])
												colorList.set(newObj[as], listChild)
											}
										}
									}
								}
							}
							if (notNum.length > 0) {
								sizeNumList = sizeNumList.concat(notNum)
							}
							var newStockList = []
							if (isNumber) {
								sizeNumList = sizeNumList.sort((a, b) => a - b);
								colorList.forEach(function(value, key) {
									for (var j = 0; j < sizeNumList.length; j++) {
										for (var m = 0; m < value.length; m++) {
											var newObj = JSON.parse(value[m].attributeList)
											var size = ''
											var color = ''
											for (var as in newObj) {
												if (as == '尺码') {
													size = newObj[as].replace(/码/g, '')
												}
											}
											if (size == sizeNumList[j]) {
												newStockList.push(value[m])
												value.splice(m, 1)
												break
											}
										}
									}
									if (value.length > 0) {
										newStockList = newStockList.concat(value)
									}
								})
								that.list[i].stockLogList1 = newStockList
							}
							if (isZiMu) {
								colorList.forEach(function(value, key) {
									for (var j = 0; j < that.lettrList.length; j++) {
										for (var m = 0; m < value.length; m++) {
											var newObj = JSON.parse(value[m].attributeList)
											var size = ''
											var color = ''
											for (var as in newObj) {
												if (as == '尺码') {
													size = newObj[as].replace(/码/g, '')
												}
											}
											if (size == that.lettrList[j]) {
												newStockList.push(value[m])
												value.splice(m, 1)
												break
											}
										}
									}
									if (value.length > 0) {
										newStockList = newStockList.concat(value)
									}
								})
								that.list[i].stockLogList1 = newStockList
							}

							for (var m = 0; m < this.list[i].stockLogList1.length; m++) {
								if (this.list[i].productCount == null) {
									this.list[i].productCount = this.list[i].stockLogList1[m].productCount
								} else {
									this.list[i].productCount += this.list[i].stockLogList1[m].productCount
								}
								let hasBuy = ''
								if (this.list[i].stockLogList1[m].isPurchasedBefore == 0 || (this.list[i]
										.stockLogList1[m].isPurchasedBefore == 1 && this.sales.userName == '散客'
										)) {
									hasBuy = '【销】'
								}
								if (this.list[i].stockLogList1[m].isPurchasedBefore == 1 && this.sales
									.userName != '散客') {
									hasBuy = '【补】'
								}
							}
						}
						this.newData = JSON.parse(JSON.stringify(this.list))
						this.getReturnOrder(option.id)

					}
				}).catch((res) => {})
			}
		},
		methods: {
			//配货完成按钮
			distribution(){
				uni.showToast({
					title: '配货成功！',
					icon: 'none',
					duration: 1000
				})
				uni.removeStorageSync('noReload')
				setTimeout(res=>{
					uni.navigateBack({
						delta: 1
					});
				},1000)
			},
			isLetterOrDigit(character) {
				if (/[a-zA-Z]/.test(character)) {
					return 'Letter';
				} else if (/[0-9]/.test(character)) {
					return 'Digit';
				} else {
					return 'Neither';
				}
			},
			getPrintHeight() {
				let that = this;
				this.printHeight = 960
				this.printHeight1 = 1900
				this.printWidth1 = 2500
				this.printHeight1 += 200
				for (let i = 0; i < that.newData.length; i++) {
					this.printHeight += 40
					for (let m = 0; m < that.newData[i].stockLogList1.length; m++) {
						this.printHeight1 += 200
						this.printHeight += 40
					}
				}
				let smailBegin = 4000
				this.smailDcl = Number(Number(this.printHeight1 / smailBegin).toFixed(2))
				this.printHeight1 = smailBegin
				if (this.smailDcl >= 1) {
					this.printWidth1 = Number(Number(this.printWidth1 / this.smailDcl).toFixed(0))
				}
			},
			//退款接口
			async getReturnOrder(id) {
				let that = this;
				await this.tui.request("/app/return/list", "post", {
					remark: 3,
					saleId: id
				}, false, false).then((res) => {
					if (res.code == 200 && res.result != null && res.result != undefined) {
						if (res.result != '') {
							this.inventoryList = res.result
							this.inventoryList.forEach(item => {
								this.tui.request("/app/return/get/" + item.id, "GET", null, false,
										false)
									.then((res) => {
										if (res.code == 200 && res.result != null && res.result !=
											undefined) {

											for (let j = 0; j < res.result.saleDetailDTOList
												.length; j++) {

												var isZiMu = false
												var isNumber = false
												var colorList = new Map()
												var sizeNumList = []
												if (res.result.saleDetailDTOList[j].stockLogList1
													.length > 0) {
													for (var m = 0; m < res.result
														.saleDetailDTOList[j].stockLogList1
														.length; m++) {
														var attr = res.result.saleDetailDTOList[j]
															.stockLogList1[m].attributeList
														var newObj = JSON.parse(attr)
														for (var as in newObj) {
															if (as == '尺码' && that.isLetterOrDigit(
																	newObj[as].replace(/码/g, '')
																	) == 'Letter') {
																isZiMu = true
															}
															if (as == '尺码' && that.isLetterOrDigit(
																	newObj[as].replace(/码/g, '')
																	) == 'Digit') {
																isNumber = true
															}
															if (as == '尺码' && sizeNumList.indexOf(
																	newObj[as].replace(/码/g, '')
																	) == -1) {
																sizeNumList.push(newObj[as]
																	.replace(/码/g, ''))
															}
															if (as == '颜色') {
																if (colorList.has(newObj[as])) {
																	colorList.get(newObj[as]).push(
																		res.result
																		.saleDetailDTOList[j]
																		.stockLogList1[m])
																} else {
																	let listChild = []
																	listChild.push(res.result
																		.saleDetailDTOList[j]
																		.stockLogList1[m])
																	colorList.set(newObj[as],
																		listChild)
																}
															}
														}
													}
												}
												var newStockList = []
												if (isNumber) {
													sizeNumList = sizeNumList.sort((a, b) => a -
													b);
													colorList.forEach(function(value, key) {
														for (var j = 0; j < sizeNumList
															.length; j++) {
															for (var m = 0; m < value
																.length; m++) {
																var newObj = JSON.parse(
																	value[m]
																	.attributeList)
																var size = ''
																var color = ''
																for (var as in newObj) {
																	if (as == '尺码') {
																		size = newObj[as]
																			.replace(/码/g,
																				'')
																	}
																}
																if (size == sizeNumList[
																	j]) {
																	newStockList.push(
																		value[m])
																	value.splice(m, 1)
																	break
																}
															}
														}
														if (value.length > 0) {
															newStockList = newStockList
																.concat(value)
														}
													})
													res.result.saleDetailDTOList[j].stockLogList1 =
														newStockList
												}
												if (isZiMu) {
													colorList.forEach(function(value, key) {
														for (var j = 0; j < that.lettrList
															.length; j++) {
															for (var m = 0; m < value
																.length; m++) {
																var newObj = JSON.parse(
																	value[m]
																	.attributeList)
																var size = ''
																var color = ''
																for (var as in newObj) {
																	if (as == '尺码') {
																		size = newObj[as]
																			.replace(/码/g,
																				'')
																	}
																}
																if (size == that.lettrList[
																		j]) {
																	newStockList.push(
																		value[m])
																	value.splice(m, 1)
																	break
																}
															}
														}
														if (value.length > 0) {
															newStockList = newStockList
																.concat(value)
														}
													})
													res.result.saleDetailDTOList[j].stockLogList1 =
														newStockList
												}




												let isTure = true
												res.result.saleDetailDTOList[j].productCount = 0
												for (let m = 0; m < res.result.saleDetailDTOList[j]
													.stockLogList1.length; m++) {
													res.result.saleDetailDTOList[j].productCount +=
														res.result.saleDetailDTOList[j]
														.stockLogList1[m].productCount
													res.result.saleDetailDTOList[j].stockLogList1[
														m].isReturn = true
												}
												for (let i = 0; i < that.newData.length; i++) {
													if (that.newData[i].productId == res.result
														.saleDetailDTOList[j].productId) {
														isTure = false
														that.newData[i].stockLogList1.push(...res
															.result.saleDetailDTOList[j]
															.stockLogList1)
													}
												}
												if (isTure) {
													that.newData.push(res.result.saleDetailDTOList[
														j])
												}
											}
										}
									}).catch((res) => {})
							})

						} else {
							for (let i = 0; i < that.list.length; i++) {
								for (let z = 0; z < that.list[i].stockLogList1.length; z++) {
									that.list[i].stockLogList1[z].maxNum = that.list[i].stockLogList1[z]
										.productCount
								}
							}
						}
					} else {
						this.tui.toast(res.message)
					}
				})
				setTimeout(res => {
					let that = this
					for (let i = 0; i < this.newData.length; i++) {
						that.newData[i].sizeList = []
						var isZiMu = false
						var isNumber = false
						var sizeNumList = []
						var notNum = []
						that.newData[i].sizePrintList = new Map()
						for (let j = 0; j < that.newData[i].stockLogList1.length; j++) {
							let discountAmount = that.newData[i].discountAmount
							let newObj = JSON.parse(that.newData[i].stockLogList1[j].attributeList)
							let printSize = new Map()
							let color = ''
							let statusAttr = '[销] '
							if (that.newData[i].stockLogList1[j].isReturn) {
								statusAttr = '[退] '
								discountAmount = that.newData[i].stockLogList1[j].discountAmount
							} else if (that.newData[i].stockLogList1[j].isPurchasedBefore == 1 && that.order
								.name != '散客') {
								statusAttr = '[补] '
							}
							for (let as in newObj) {
								if (as == '颜色') {
									color = statusAttr + newObj[as]
									if (that.newData[i].sizePrintList.has(color) == true) {
										printSize = that.newData[i].sizePrintList.get(color)
									} else {
										that.newData[i].sizePrintList.set(color, printSize)
									}
								}
								if (as == '尺码') {
									if (newObj[as].indexOf('码') != -1) {
										if (that.newData[i].sizeList.indexOf(newObj[as].substr(0, newObj[as]
												.indexOf('码'))) == -1) {
											that.newData[i].sizeList.push(newObj[as].substr(0, newObj[as]
												.indexOf('码')))
										}
										that.newData[i].sizePrintList.get(color).set(newObj[as].substr(0,
												newObj[as].indexOf('码')), that.newData[i].stockLogList1[j]
											.productCount + ':' + discountAmount)
									} else {
										if (that.newData[i].sizeList.indexOf(newObj[as]) == -1) {
											that.newData[i].sizeList.push(newObj[as])
										}
										that.newData[i].sizePrintList.get(color).set(newObj[as], that.newData[
											i].stockLogList1[j].productCount + ':' + discountAmount)
									}
								}
								if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g, '')) ==
									'Letter') {
									isZiMu = true
								}
								if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g, '')) ==
									'Digit') {
									isNumber = true
								}
								if (as == '尺码' && sizeNumList.indexOf(newObj[as].replace(/码/g, '')) == -1) {
									if (as == '尺码' && that.isLetterOrDigit(newObj[as].replace(/码/g, '')) ==
										'Digit') {
										sizeNumList.push(newObj[as].replace(/码/g, ''))
									} else {
										if (notNum.indexOf(newObj[as].replace(/码/g, '')) == -1) {
											notNum.push(newObj[as].replace(/码/g, ''))
										}
									}

								}
							}
						}
						if (notNum.length > 0) {
							sizeNumList = sizeNumList.concat(notNum)
						}
						if (isZiMu) {
							let newStockList = []
							for (var j = 0; j < that.lettrList.length; j++) {
								for (var m = 0; m < that.newData[i].sizeList.length; m++) {
									if (that.newData[i].sizeList[m] == that.lettrList[j]) {
										newStockList.push(that.newData[i].sizeList[m])
										that.newData[i].sizeList.splice(m, 1)
										break
									}
								}
							}
							if (that.newData[i].sizeList.length > 0) {
								newStockList = newStockList.concat(that.newData[i].sizeList)
							}
							that.newData[i].sizeList = newStockList
						}
						if (isNumber) {
							let newStockList = []
							sizeNumList = sizeNumList.sort((a, b) => a - b);
							for (var j = 0; j < sizeNumList.length; j++) {
								for (var m = 0; m < that.newData[i].sizeList.length; m++) {
									if (that.newData[i].sizeList[m] == sizeNumList[j]) {
										newStockList.push(that.newData[i].sizeList[m])
										that.newData[i].sizeList.splice(m, 1)
										break
									}
								}
							}
							if (that.newData[i].sizeList.length > 0) {
								newStockList = newStockList.concat(that.newData[i].sizeList)
							}
							that.newData[i].sizeList = newStockList
						}
						//this.newData[i].sizeList = [...new Set(this.newData[i].sizeList)]
					}

				}, 500)
				this.$nextTick(() => {
					setTimeout(res => {
						this.getPrintHeight()
					}, 1000)
				})
			}
		},
	}
</script>

<style lang="scss">
	page{
		font-size: 24rpx;
	}

	.container {
		padding-bottom: 118rpx;

		.boxbg {
			background-color: #fff;
			margin: 30upx;
			padding: 20upx;
			border-radius: 10upx;
			box-shadow: 0px 1px 10px 2px #e2e2e2;
		}

		.tui-order-item {
			.u-collapse-content {
				display: flex;
				justify-content: space-between;
				line-height: 30px;
			}
		}
	}

	.tui-tabbar {
		width: 100%;
		height: 98rpx;
		background: #fff;
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		font-size: 26rpx;
		box-shadow: 0 0 1px rgba(0, 0, 0, .3);
		padding-bottom: env(safe-area-inset-bottom);
		z-index: 99;
	}

	.content-box {
		width: 100%;
		height: auto;
		background: #fff;
		margin-bottom: 20rpx;

	}

	.shop-box {
		width: 95%;
		margin: 0 auto;
	}
	.uni-card__header {
		height: 120rpx;
	}
	.tui-btn-mr {
		width: 130rpx;
		height: 60rpx;
		background: #088FEB;
		color: #fff;
		border-radius: 10px;
		line-height: 60rpx;
		text-align: center;
		margin-right: 30rpx;
	}
</style>