<template>
	<view>
		<view class="demo" style="height: 180rpx;">
			<view class="detailMiddle">
				<text>精</text>
				<text>简</text>
				<text>老</text>
				<text>纯棉</text>
				<text>赠</text>
				<text>青春</text>
				<text>总计</text>
			</view>
			<view class="detailMiddleRed">
				<text v-for="(item,index) in numList" :key="index">{{ item }}</text>
				<text>{{ allNum }}</text>
			</view>
		</view>
		<!-- <view @click="getScan">
			点击添加
		</view>
		<view @click="setC">
			点击校验
		</view> -->
		<scroll-view :style="{height: scroll_height}" class="listBox" scroll-y="true">
			<view class="listBox_item" :class="(index + 1)%2 == 0 ? 'back' : ''" v-for="(item,index) in list"
				:key="index">
				<view style="display: flex;align-items: center;">
					<view class="gray">
						{{ list.length - index }}
					</view>
					<view class="left">
						{{ item.style }}
					</view>
				</view>
				<view class="right" @click="del(item.style,index)">
					删除
				</view>
			</view>
		</scroll-view>
		<view class="fiexedEnd">
			<view class="fiexedEnd_title" @click="allDel">
				全部清除
			</view>
			<view class="fiexedEnd_red" @click="fillOut = true">
				扫描
			</view>
			<view class="fiexedEnd_title" @click="sendSure">
				确认发货
			</view>
		</view>
		<u-mask :show="fillOut">
			<view class="showTip">
				<image src="../../static/fangwei/show.png"></image>
				<view class="title">
					温馨提示
				</view>
				<view class="remark">
					是否确保这一件里的所有货没有调换过
				</view>
				<view class="btn">
					<view class="btn_left" @click="fillOut = false">
						不确保
					</view>
					<view class="btn_right" @click="getCode">
						确保
					</view>
				</view>
			</view>
		</u-mask>
		<u-mask :show="ifShow">
			<view class="showTip">
				<image src="../../static/fangwei/show.png"></image>
				<view class="title">
					提示
				</view>
				<view class="remark">
					添加成功!
				</view>
				<view class="btn">
					<view class="btn_left" @click="ifShow = false,clearTime()">
						取消
					</view>
					<view class="btn_right" @click="getCode">
						扫下一个({{ count }}s)
					</view>
				</view>
			</view>
		</u-mask>
		<u-modal v-model="show" :show-cancel-button="true" :content="content" @confirm="confirm"></u-modal>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		scanCode,
		UserSend,
		AllSendGood
	} from '../../apis/fangwei.js'
	export default {
		data() {
			return {
				show: false,
				fillOut: false,
				delShow: false,
				ifShow: false,
				content: '',
				count: 3,
				list: [],
				id: '',
				scroll_height: 0,
				numList: [0, 0, 0, 0, 0, 0],
				info: {},
				code: '',
				allNum: 0,
				param: {}
			}
		},
		// 获取可滑动区域的高度
		onReady: function(res) {
			var _this = this;
			uni.getSystemInfo({
				success: (resu) => {
					const query = uni.createSelectorQuery()
					query.select('.listBox').boundingClientRect()
					query.exec(function(res) {
						_this.scroll_height = resu.windowHeight - res[0].top + 'px';
					})
				},
				fail: (res) => {}
			})
		},
		onLoad: function(option) {
			this.id = option.id
			
		},
		methods: {
			setC() {
				this.count = 3
				this.ifShow = true
				this.setTime()
				let index = this.list.findIndex(item => item.code == '44444')
				if (index != -1) {
					console.log('存在')
				} else {
					console.log('不存在')
				}
			},
			// 测试扫描
			getScan() {
				var good = 6
				var style = this.getStyle(good)
				const data = {
					style: this.getStyle(good),
					styleId: 6,
					code: 6666
				}
				this.list.unshift(data)
				console.log(this.list)
				this.addStyle(good)
			},
			setTime() {
				//设置定时器
				this.clearTimeSet = setInterval(() => {
					this.count--;
					if (this.count == 0) {
						this.ifShow = false
						this.clearTime()
						this.getCode()
					}
				}, 1000);
			},
			clearTime() {
				//清除定时器
				clearInterval(this.clearTimeSet);
			},
			// 确认发货
			sendGood() {
				const data = {
					1: [],
					2: [],
					3: [],
					4: []
				}
				this.list.forEach((item, index, arr) => {
					data[item.styleId].push(item.code)
				})
				AllSendGood({
					id: this.id,
					code: data
				}).then(res => {
					if (res.code == 200) {
						uni.showToast({
							title: '发货成功',
							icon: 'none'
						})
						this.list = []
						this.numList = [0, 0, 0, 0]
						this.allNum = 0
					} else {
						uni.showModal({
							title: '错误提示',
							content: res.message || '发货失败',
							showCancel: false
						})
					}
				})
			},
			// 删除单个 弹窗
			del(item, i) {
				this.item = item
				this.i = i
				this.type = 1
				this.content = '确定要删除吗？'
				this.show = true
			},
			// 删除所有 弹窗
			allDel() {
				if (this.list.length == 0) {
					uni.showToast({
						title: '暂无货物',
						icon: 'none'
					})
					return false
				}
				this.content = '确定要全部清除吗？'
				this.type = 2
				this.show = true
			},
			// 发货调起确认框
			sendSure() {
				if (this.list.length == 0) {
					uni.showToast({
						title: '暂无货物',
						icon: 'none'
					})
					return false
				}
				this.type = 3
				this.show = true
				this.content = '确定要发货吗？'
			},
			confirm() {
				// 删除单个
				if (this.type == 1) {
					this.list.splice(this.i, 1)
					this.reduceStyle(this.item)
					uni.showToast({
						title: '删除成功',
						icon: 'none'
					})
					// 删除所有
				} else if (this.type == 2) {
					this.list = []
					this.numList = [0, 0, 0, 0]
					this.allNum = 0
				} else if (this.type == 3) {
					this.sendGood()
				}
			},
			// 款式
			getStyle(v) {
				if (v == 1) {
					return '精装版'
				} else if (v == 2) {
					return '简约版'
				} else if (v == 3) {
					return '高腰版'
				} else if (v == 4) {
					return '纯棉版'
				} else if (v == 5) {
					return '赠品'
				} else if (v == 6) {
					return '青春版'
				}
			},
			// 增加款式数量
			addStyle(v) {
				if (v == 1) {
					this.numList[0] += 1
				} else if (v === 2) {
					this.numList[1] += 1
				} else if (v === 3) {
					this.numList[2] += 1
				} else if (v === 4) {
					this.numList[3] += 1
				} else if (v === 5) {
					this.numList[4] += 1
				} else if (v === 6) {
					this.numList[5] += 1
				}
				this.allNum += 1
			},
			// 减少款式数量
			reduceStyle(v) {
				if (v === '精装版') {
					this.numList[0] -= 1
				} else if (v === '简约版') {
					this.numList[1] -= 1
				} else if (v === '高腰版') {
					this.numList[2] -= 1
				} else if (v === '纯棉版') {
					this.numList[3] -= 1
				} else if (v === '赠品') {
					this.numList[4] -= 1
				} else if (v === '青春版') {
					this.numList[5] -= 1
				}
				this.allNum -= 1
			},
			//获取信息
			getInfo(params) {
				UserSend({
					id: this.id,
					code: params,
				}).then(res => {
					if (res.code == 200) {
						const data = {
							style: this.getStyle(res.data.good),
							styleId: res.data.good,
							code: params
						}
						this.list.unshift(data)
						this.addStyle(res.data.good)
						this.count = 3
						this.ifShow = true
						this.setTime()
					} else {
						uni.showModal({
							title: '错误提示',
							content: res.message || '获取信息失败',
							showCancel: false
						})
					}
				})
			},
			getCode() {
				this.ifShow = false
				this.clearTime()
				this.fillOut = false
				const script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = 'http://res.wx.qq.com/open/js/jweixin-1.4.0.js';
				document.getElementsByTagName('head')[0].appendChild(script);
				script.onload = () => {
					scanCode().then(res => {
						if (res.code == 200) {
							wx.config({
								debug: false, // 开启调试模式
								appId: res.data.appId, // 必填，公众号的唯一标识
								timestamp: res.data.timestamp, // 必填，生成签名的时间戳
								nonceStr: res.data.nonceStr, // 必填，生成签名的随机串
								signature: res.data.signature, // 必填，签名，见附录1
								jsApiList: res.data.jsApiList // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
							})
							wx.ready(() => {
								wx.scanQRCode({
									needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
									scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
									success: res => {
										var result = res
											.resultStr; // 当needResult 为 1 时，扫码返回的结果
										var site = result.lastIndexOf('\/')
										var params = result.substring(site + 1, result
											.length)
										this.code = window.encodeURIComponent(params)
										let index = this.list.findIndex(item => item
											.code == this.code)
										if (index != -1) {
											uni.showModal({
												title: '错误提示',
												content: '此码已添加，请勿重复扫描',
												showCancel: false
											})
										} else {
											this.getInfo(this.code)
										}
									}
								})
							})
						} else {
							uni.showModal({
								title: '错误提示',
								content: res.message || '获取信息失败',
								showCancel: false
							})
						}
					});
				};
			}
		}
	}
</script>
<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.showTip {
		position: fixed;
		left: 0;
		right: 0;
		top: 30%;
		margin: auto;
		width: 648rpx;
		height: 368rpx;
		background: #FFFFFF;
		border-radius: 24rpx;
		z-index: 10000;
		display: flex;
		justify-content: space-between;
		align-items: center;
		flex-direction: column;
		padding-bottom: 34rpx;

		image {
			width: 180rpx;
			height: 136rpx;
			margin-top: -68rpx;
		}

		.title {
			font-size: 38rpx;
			font-weight: bold;
			color: #333333;
		}

		.remark {
			font-size: 34rpx;
			font-weight: 400;
			color: #333333;
		}

		.btn {
			display: flex;

			view {
				width: 242rpx;
				height: 88rpx;
				border-radius: 44rpx;
				text-align: center;
				line-height: 88rpx;
				font-size: 32rpx;
				font-weight: bold;
			}

			&_left {
				background: #F5F5F5;
				color: #333333;
			}

			&_right {
				margin-left: 24rpx;
				background: linear-gradient(93deg, #F30000 0%, #FE4815 100%);
				color: #fff;
			}
		}
	}

	.detailTop {
		padding: 24rpx 0 0 24rpx;
		display: flex;
		align-items: center;

		&_left {
			width: 136rpx;
			font-size: 34rpx;
			font-weight: bold;
			color: #333333;
		}

		&_right {
			width: 566rpx;
			height: 104rpx;
			background: #F8F8F8;
			border-radius: 8rpx;
			line-height: 104rpx;
			padding-left: 28rpx;
			color: #333;
			font-size: 32rpx;
		}
	}

	.detailMiddle {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 10rpx;

		text {
			display: inline-block;
			width: 14%;
			text-align: center;
			line-height: 90rpx;
			font-size: 32rpx;
			font-weight: bold;
		}
	}

	.detailMiddleRed {
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #FFF4F3;
		padding: 0 10rpx;

		text {
			display: inline-block;
			width: 14%;
			text-align: center;
			line-height: 90rpx;
			font-size: 32rpx;
			font-weight: bold;
			color: #FB231F;
		}
	}

	.listBox {
		padding-bottom: 104rpx;

		// height: 1500rpx;
		.back {
			background-color: #F8F8F8;
		}

		&_item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 24rpx;
			height: 104rpx;

			.left {
				margin-left: 70rpx;
				font-weight: bold;
				font-size: 32rpx;
			}

			.gray {
				width: 80rpx;
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
				color: #999999;
			}

			.right {
				width: 132rpx;
				height: 64rpx;
				background: linear-gradient(141deg, #F30000 0%, #FE4815 100%);
				border-radius: 34rpx;
				line-height: 64rpx;
				text-align: center;
				color: #fff;
				font-size: 28rpx;
			}
		}
	}

	.fiexedEnd {
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
		height: 104rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -4rpx 24rpx rgba(0, 0, 0, 0.1);
		padding: 0 72rpx 18rpx 72rpx;
		font-size: 32rpx;
		font-weight: bold;
		display: flex;
		justify-content: space-between;
		align-items: flex-end;

		&_red {
			width: 172rpx;
			height: 172rpx;
			background: linear-gradient(150deg, #FFAA01 0%, #FE0000 100%);
			box-shadow: 0px 6rpx 20rpx rgba(254, 34, 0, 0.43);
			border-radius: 50%;
			font-size: 40rpx;
			color: #fff;
			line-height: 172rpx;
			text-align: center;
		}

		&_title {
			margin-bottom: 12rpx;
		}
	}
</style>
