<template>
	<view class="PanKuContainer">
		<!-- <view class="all">
			<view class="num">库存：总共<text class="spec">3532</text>套</view>
		</view> -->
		<view class="goodList">
			<view
				v-for="(item, i) in infoList"
				:key="i"
				class="goodItem"
			>
				<view class="top">
					<view
						class="avatar"
						:style="{ backgroundImage: `url(${item.imgurl})` }"
					></view>
					<view class="info">
						<view class="name">{{ item.name }}</view>
						<view class="fixed">一年零一天不满意退钱</view>
					</view>
				</view>
				<view class="skuList">
					<view
						class="skuHeader"
						:class="item.toggle ? 'open' : ''"
						@click="item.toggle = !item.toggle"
					>
						<view class="type">规格</view>
						<view class="size">尺码</view>
						<view class="reviseNum">
							<view class="reviseNum-title">数量</view>
						</view>
					</view>
					<template v-if="item.toggle">
						<view
							v-for="(btem, bi) in item.skuList"
							:key="bi"
						>
							<template v-if="btem.type.includes(item.k)">
								<view class="skuItem">
									<view class="type">{{ +btem.sex ? '男款' : '女款' }}</view>
									<view class="size">{{ btem.size }}</view>
									<view class="reviseNum">
										<!-- <input
											v-model="btem.num"
											type="number"
											placeholder="输入数量"
											class="reviseInput"
											@change="inputNum(btem)"
										/> -->
										<count2 :count-num.sync="btem.num" />
									</view>
								</view>
							</template>
						</view>
					</template>
				</view>
				<view class="dataAll">
					<view class="sex">
						男款:{{ item.skuList.filter(item => item.sex).reduce((a, b) => a + Number(b.num), 0) }}套
						/
						女款:{{ item.skuList.filter(item => !item.sex).reduce((a, b) => a + Number(b.num), 0) }}套
					</view>
					<view class="num">
						<text>合计：</text>
						<text class="spec">
							{{ item.skuList.reduce((a, b) => a + Number(b.num), 0) }}套
						</text>
					</view>
				</view>
			</view>
		</view>
		<view class="reviseBtn" @click="publishFirstPanKu">确认提交</view>
	</view>
</template>

<script>
	import Count2 from "./components/count2.vue"
	import { stockGoods, publishFirstPanKu } from "@/apis/stock.js"
	export default {
		components: {
			Count2
		},
		data() {
			return {
				count: 0,
				infoList: []
			}
		},
		onShow() {
			this.getStockGoods()
		},
		methods: {
			// 检查输入
			inputNum(data) {
				if(!(/^[+]{0,1}(\d+)$/.test(data.num))) {
					uni.showToast({
						icon: "none",
						title: '请填写正确的库存数量'
					})
					data.num = 0
					return false
				} else {
					data.num = Number(data.num)
				}
			},
			publishFirstPanKu() {
				let arr = {}
				let num = 0
				this.infoList.forEach((item) => {
					arr[item.k] = item.skuList.filter(btem => btem.type.includes(item.k))
					num += item.skuList.reduce((a, b) => a + Number(b.num), 0)
				})
				if(num === 0) {
					uni.showModal({
						content: "数量最少为1件",
						showCancel: false
					})
					return false
				}
				uni.showLoading()
				publishFirstPanKu(arr).then(res => {
					uni.hideLoading()
					if(res.code === 200) {
						uni.showModal({
							content: "一次性盘库成功,请返回上一个页面",
							showCancel: false,
							success(res) {
								if(res.confirm) {
									uni.reLaunch({
										url: '../stock/stock'
									})
								}
							}
						})
					} else {
						uni.showModal({
							content: res.message || "盘库失败",
							showCancel: false
						})
					}
				}).catch(()=> {
					uni.hideLoading()
					uni.showModal({
						content: "盘库失败",
						showCancel: false
					})
				})
			},
			getStockGoods() {
				uni.showLoading()
				stockGoods().then(res => {
					uni.hideLoading()
					if(res.code === 200) {
						this.infoList = Object.keys(res.data).map(k => ({
							imgurl: res.data[k].imgurl,
							name: res.data[k].name,
							k: k,
							toggle: false,
							skuList: [
								{ size: 'L', sex: 1, num: 0, type: ['hard'] },
								{ size: 'XL', sex: 1, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: '2XL', sex: 1, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: '3XL', sex: 1, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: '4XL', sex: 1, num: 0, type: ['hard'] },
								{ size: '5XL', sex: 1, num: 0, type: ['hard'] },
								{ size: 'M', sex: 0, num: 0, type: ['hard', 'simple', 'youth'] },
								{ size: 'L', sex: 0, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: 'XL', sex: 0, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: '2XL', sex: 0, num: 0, type: ['hard', 'simple', 'old', 'new_old', 'youth'] },
								{ size: '3XL', sex: 0, num: 0, type: ['hard', 'old'] },
								{ size: '4XL', sex: 0, num: 0, type: ['hard'] }
							]
						}))
					} else {
						uni.showModal({
							content: res.message || "获取商品详情失败",
							showCancel: false
						})
					}
				}).catch(() => {
					uni.hideLoading()
					uni.showModal({
						content: "获取商品详情失败",
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		display: flex;
		flex-direction: column;
	}
	.PanKuContainer {
		width: 100%;
		flex: 1;
		background-color: #F9F9FB;
		.btn {
			width: 140rpx;
			height: 56rpx;
			border-radius: 8rpx;
			background-color: #EA4A41;
			color: #FFFFFF;
			font-size: 28rpx;
			line-height: 56rpx;
			text-align: center;
			text-align: center;
		}
		.reviseBtn {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 100rpx;
			text-align: center;
			line-height: 100rpx;
			background: linear-gradient(to right, #F97C55, #F44545);
			color: #FFFFFF;
			font-size: 32rpx;
		}
		.all {
			width: 100%;
			height: 116rpx;
			padding: 0 30rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.num {
				color: #333333;
				font-size: 36rpx;
				line-height: 50rpx;
				font-weight: bold;
				.spec {
					color: #EA4A41 !important;
				}
			}
		}
		.goodList {
			width: 100%;
			padding-bottom: 100rpx;
			.goodItem {
				width: 100%;
				margin-bottom: 30rpx;
				padding: 30rpx;
				box-sizing: border-box;
				background-color: #ffffff;
				.top {
					width: 100%;
					display: flex;
					align-items: stretch;
					justify-content: space-between;
					margin-bottom: 40rpx;
					.avatar {
						width: 180rpx;
						height: 144rpx;
						background-color: #eeeeee;
						background-position: center;
						background-size: cover;
						background-repeat: no-repeat;
						border-radius: 8rpx;
					}
					.info {
						display: flex;
						justify-content: center;
						flex-direction: column;
						flex: 1;
						overflow: hidden;
						margin-left: 20rpx;
						.name {
							color: #333333;
							font-size: 32rpx;
							line-height: 44rpx;
							margin-bottom: 20rpx;
							font-weight: bold;
						}
						.fixed {
							color: #F76454;
							font-size: 28rpx;
							height: 40rpx;
							line-height: 40rpx;
							border: 1px solid #F76454;
							box-sizing: border-box;
							width: 328rpx;
							border-radius: 40rpx;
							color: #F76454;
							text-align: center;
						}
					}
				}
				.skuList{
					width: 100%;
					margin-bottom: 30rpx;
					.skuHeader, .skuItem {
						width: 100%;
						height: 100rpx;
						display: flex;
						align-items: center;
						justify-content: space-between;
						border-bottom: 1px solid #EEEEEE;
						.type, .size, .num, .reviseNum {
							flex: 1;
							overflow: hidden;
							display: flex;
							align-items: center;
							justify-content: center;
						}
						.type {
							justify-content: flex-start;
							flex: none;
							width: 25%;
							font-weight: bold;
						}
						.size {
							font-weight: bold;
						}
						.reviseNum {
							justify-content: flex-end;
							width: 25%;
							// .reviseInput {
							// 	width: 168rpx;
							// 	height: 64rpx;
							// 	text-align: center;
							// 	line-height: 64rpx;
							// 	border-radius: 8rpx;
							// 	background-color: #F8F8F8;
							// 	color: #999999;
							// 	font-size: 28rpx;
							// }
							.reviseNum-title {
								width: 152rpx;
								height: 56rpx;
								border-radius: 56rpx;
								text-align: center;
								line-height: 56rpx;
								background: linear-gradient(141deg, #F97C55 0%, #F44545 100%);
								color: #FFFFFF;
								font-size: 28rpx;
							}
						}
					}
					.skuHeader {
						color: #999999;
						font-size: 28rpx;
						line-height: 40rpx;
						border-top: 1px solid #EEEEEE;
						.reviseNum {
							&::after {
								display: block;
								content: "";
								margin-left: 10rpx;
								width: 30rpx;
								height: 30rpx;
								background-image: url(../../static/new_my/arrow.png);
								background-size: cover;
								background-position: center;
								background-repeat: no-repeat;
							}
						}
						&.open {
							.reviseNum {
								&::after {
									transform: rotate(90deg) !important;
								}
							}
						}
					}
				}
				.dataAll {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.sex {
						color: #333333;
						font-size: 28rpx;
					}
					.num {
						color: #999999;
						font-size: 28rpx;
						.spec {
							color: #EA4A41 !important;
							font-size: 40rpx !important;
							font-weight: bolder;
						}
					}
				}
			}
		}
	}
</style>
