<template>
	<view class="PanKuContainer">
		<view class="all">
			<view class="num">
				<text>库存：总共</text>
				<text class="spec">
					{{ stockList.reduce((a, b) => a + (b.children.reduce((x, y) => x + Number(y.reviseNum), 0)), 0) }}
				</text>
				<text>套</text>
			</view>
			<view class="btn" @click="toUrlLink('stock/panku_record')">盘库记录</view>
		</view>
		<view class="goodList">
			<view
				v-for="(item, i) in stockList"
				:key="i"
				class="goodItem"
			>
				<view class="top">
					<view
						class="avatar"
						:style="{ backgroundImage: `url(${item.imgurl})` }"
					/>
					<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="num">数量</view>
						<view class="reviseNum">
							<view class="reviseNum-title">修改数量</view>
						</view>
					</view>
					<template v-if="item.toggle">
						<view
							v-for="(btem, bi) in item.children"
							:key="bi"
							class="skuItem"
						>
							<view class="type">{{ +btem.sex ? '男款' : '女款' }}</view>
							<view class="size">{{ btem.size }}</view>
							<view class="num">{{ btem.num }}</view>
							<view class="reviseNum">
								<count
									:countNum.sync="btem.reviseNum"
									@update:inputCountNum="toShowInputDialog(btem)"
								/>
								<!-- :min="-Number(btem.num)" -->
							</view>
						</view>
					</template>
				</view>
				<view class="dataAll">
					<view class="sex">
						男款:{{ item.children.filter(v => Number(v.sex)).reduce((a, b) => a + Number(b.reviseNum), 0) }}套
						/
						女款:{{ item.children.filter(v => !Number(v.sex)).reduce((a, b) => a + Number(b.reviseNum), 0) }}套
					</view>
					<view class="num">
						<text>
							合计：
						</text>
						<text class="spec">
							<!-- {{ item.total + item.children.reduce((a, b) => a + Number(b.reviseNum), 0) }}套 -->
							{{ item.children.reduce((a, b) => a + Number(b.reviseNum), 0) }}套 
						</text>
					</view>
				</view>
			</view>
		</view>
		<view class="reviseBtn" @click="toSubmitRevise">确认修改</view>
		<!-- 输入弹窗 -->
		<toast
			:is-show="dialogInput.show"
			:is-close="false"
		>
			<view class="dialogInput">
				<view class="title">请输入要修改的数量</view>
				<input
					v-model="dialogInput.num"
					type="text"
					class="input"
					:placeholder="`修改数量范围为: ${dialogInput.min}-${dialogInput.max}`"
				/>
				<view class="btns">
					<view class="btn cancel" @click="cancelInput">取消</view>
					<view class="btn" @click="toPublishInput">修改</view>
				</view>
			</view>
		</toast>
	</view>
</template>

<script>
	import Count from "./components/count.vue"
	import Toast from "@/components/toast.vue"
	import { getPanKuStock, stockGoods, morePanKuRevise } from "@/apis/stock.js"
	export default {
		components: {
			Count,
			Toast
		},
		data() {
			return {
				stockList: [],
				inputNum: 0,
				dialogInput: {
					show: false,
					num: '',
					min: 0,
					max: 999,
					info: new Map()
				}
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			// 提交修改
			toSubmitRevise() {
				const _this = this
				uni.showModal({
					content: "确认提交修改吗?",
					cancelText: '取消',
					confirmText: '修改',
					success(res){
						if(res.confirm) {
							let arr = {}
							_this.stockList.forEach(item => {
								arr[item.k] = item.children.map(v => {
									let _d = {}
									// _d.num = Number(v.num) + Number(v.reviseNum)
									_d.num = Number(v.reviseNum)
									_d.sex = v.sex
									_d.size = v.size
									return _d
								})
							})
							uni.showLoading()
							morePanKuRevise(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
								})
							})
						}
					}
				})
			},
			// 确定弹窗输入
			toPublishInput() {
				// /^[-]?[0-9]+$/
				if(!(/^[0-9]+$/.test(this.dialogInput.num))) {
					uni.showModal({
						content: "修改数量不合法,请重新输入",
						showCancel: false
					})
					return false
				}
				if(Number(this.dialogInput.num) < this.dialogInput.min) {
					uni.showModal({
						content: `修改数量最小为:${this.dialogInput.min}`,
						showCancel: false
					})
					return false
				}
				if(Number(this.dialogInput.num) > this.dialogInput.max) {
					uni.showModal({
						content: `修改数量最大为:${this.dialogInput.max}`,
						showCancel: false
					})
					return false
				}
				this.dialogInput.info.get('data').reviseNum = Number(this.dialogInput.num)
				this.cancelInput()
			},
			// 取消弹窗输入
			cancelInput() {
				this.dialogInput.show = false
				this.dialogInput.num = ''
				this.dialogInput.min = 0
				this.dialogInput.max = 999
				this.dialogInput.info.clear()
			},
			// 输入弹窗
			toShowInputDialog(data) {
				// this.dialogInput.min = -Number(data.num)
				this.dialogInput.min = 0
				this.dialogInput.show = true
				this.dialogInput.info.set('data', data)
			},
			// 初始化
			init() {
				uni.showLoading()
				const _this = this
				Promise.all([this.getStockGoods(), this.getPanKuStock()]).then(res => {
					uni.hideLoading()
					let goodsList = res[0]
					let stockList = res[1]
					_this.stockList = Object.keys(stockList).map(k => ({
						k: k,
						children: stockList[k].map(v => {
							v.reviseNum = v.num
							return v
						}),
						imgurl: goodsList[k].imgurl,
						name: goodsList[k].name,
						total: stockList[k].reduce((a, b) => a + Number(b.num), 0),
						toggle: false
					}))
				}).catch(() => {
					uni.hideLoading()
					uni.showModal({
						content: "获取盘库信息失败",
						showCancel: false
					})
				})
			},
			// 获取图片
			getStockGoods() {
				return new Promise(resolve => {
					stockGoods().then(res => {
						if(res.code === 200) {
							resolve(res.data)
						}
					})
				}) 
			},
			// 获取盘库历史
			getPanKuStock() {
				return new Promise(resolve => {
					getPanKuStock().then(res => {
						if(res.code === 200) {
							resolve(res.data)
						}
					})
				})
			},
			toUrlLink(url) {
				if(!url) return false
				uni.navigateTo({
					url: `../${url}`
				})
			}
		}
		
	}
</script>

<style lang="scss" scoped>
	/deep/ .uni-input-placeholder {
		color: #cccaca !important;
	}
	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;
						}
						.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: 18%;
						}
						.reviseNum {
							justify-content: flex-end;
						}
					}
					.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;
							}
							.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;
							}
						}
						&.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;
						}
					}
				}
			}
		}
		.dialogInput {
			width: 80%;
			max-width: 600rpx;
			padding: 50rpx 30rpx;
			background-color: #ffffff;
			box-sizing: border-box;
			margin: 0 auto;
			border-radius: 24rpx;
			.title {
				font-size: 40rpx;
				color: #333333;
				line-height: 60rpx;
				text-align: center;
				margin-bottom: 40rpx;
			}
			.input {
				width: 100%;
				border: 1px solid #c1c1c1;
				height: 80rpx;
				font-size: 40rpx;
				line-height: 80rpx;
				padding: 0 20rpx;
				box-sizing: border-box;
				text-align: center;
				margin-bottom: 40rpx;
				border-radius: 8rpx;
			}
			.btns {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				.btn {
					margin: 0 20rpx;
					height: 70rpx;
					line-height: 70rpx;
					padding: 0 20rpx;
					&.cancel {
						background: #ffffff !important;
						color: #EA4A41 !important;
						border: 1px solid #EA4A41 !important;
						box-sizing: border-box;
					}
				}
			}
		}
	}
</style>
