<template>
	<view class="ChuKuContainer">
		<view class="all">
			<view class="btn" @click="toUrlLink('stock/ruku_record')">入库记录</view>
		</view>
		<view class="goodList">
			<view
				v-for="(item, i) in list"
				:key="i"
				class="goodItem"
			>
				<view class="top">
					<view
						class="avatar"
						:style="{ backgroundImage: `url(${item.imgurl})` }"
					></view>
					<view class="info">
						<view class="name">
							<text class="text">{{ item.name }}</text>
							<text class="text spec">（单位：{{ item.unitName }}）</text>
						</view>
						<view class="fixed">一年零一天不满意退钱</view>
					</view>
				</view>
				<view class="skuList">
					<view
						v-for="(btem, bi) in item.skuList"
						:key="bi"
						class="skuItem"
					>
						<picker mode="selector" :range="sexList" range-key="name" @change="toChangeSex($event, btem, item)">
							<view class="select">
								<view class="value">{{ btem.sex === '' ? '款式' : btem.sex ? '男款' : '女款' }}</view>
							</view>
						</picker>
						<template v-if="btem.sex === ''">
							<view class="select" @click="selectSizeTip">
								<view class="value">{{ btem.size === '' ? '尺码' : btem.size }}</view>
							</view>
						</template>
						<template v-else>
							<picker mode="selector" :range="item.pickerSizeList[String(btem.sex)]" @change="toChangeSize($event, btem, item)">
								<view class="select">
									<view class="value">{{ btem.size === '' ? '尺码' : btem.size }}</view>
								</view>
							</picker>
						</template>
						<view class="countBox">
							<count2
								:countNum.sync="btem.num"
							/>
						</view>
						<view
							class="addIcon"
							:class="bi ? 'delete' : ''"
							@click="toAddSku(item, bi)"
						/>
					</view>
				</view>
				<view class="dataAll">
					<view class="sex">
						男款:{{ item.skuList.filter(v => Number(v.sex)).reduce((a, b) => a + Number(b.num), 0) }}{{ item.unitName }}
						/
						女款:{{ item.skuList.filter(v => !Number(v.sex)).reduce((a, b) => a + Number(b.num), 0) }}{{ item.unitName }}
					</view>
					<view class="num">
						<text>共计：</text>
						<text class="spec">
							{{ item.skuList.reduce((a, b) => a + Number(b.num), 0) }}{{ item.unitName }}
						</text>
					</view>
				</view>
				<view class="dataAll" style="margin-top: 20rpx;">
					<view class="unit" v-if="item.unitNum > 1">
						1{{ item.unitName }} = {{ item.unitNum }}套
					</view>
					<view class="num">
						<text>合计：</text>
						<text class="spec">
							{{ item.skuList.reduce((a, b) => a + Number(b.num), 0) * item.unitNum }}套
						</text>
					</view>
				</view>
			</view>
		</view>
		<view class="reviseFooter">
			<view class="footer-right">
				<text class="text2">共计：</text>
				<text class="text3">
					{{ list.reduce((a, b) => a + (b.skuList.reduce((x,y) => x + Number(y.num), 0)), 0) }}
				</text>
				<text class="text4">套</text>
			</view>
			<!-- <view class="footer-right" style="margin-right: 0;">
				<text class="text2">合计：</text>
				<text class="text3">
					{{ list.reduce((a, b) => a + (b.skuList.reduce((x,y) => x + Number(y.num) * b.unitNum, 0)), 0) }}
				</text>
				<text class="text4">套</text>
			</view> -->
			<view class="footer-btn" @click="toSubmitRuku">确认入库</view>
		</view>
	</view>
</template>

<script>
	
	const skuAllList = [
		{ 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'] }
	]
	
	import Count2 from "./components/count2.vue"
	import { stockGoods, publishRuKuNew } from "@/apis/stock.js"
	export default {
		components: {
			Count2
		},
		data() {
			return {
				sexList: [
					{ name:'男款', value: 1 },
					{ name:'女款', value: 0 },
				],
				list: []
			}
		},
		onShow() {
			this.getStockGoods()
		},
		methods: {
			selectSizeTip() { // 尺码选择提醒
				uni.showModal({
					content: "请先选择款式",
					showCancel: false
				})
			},
			toSubmitRuku() { // 入库
				let arr = {}
				let num = 0, errorNum = 0
				this.list.forEach(v => {
					arr[v.k] = v.skuList
					errorNum += v.skuList.filter(item => (item.sex === '' || item.size === '') && item.num > 0).length
					num += v.skuList.reduce((a, b) => a+Number(b.num), 0)
				})
				if(num === 0) {
					uni.showModal({
						content: '入库最少为1件',
						showCancel: false
					})
					return false
				}
				if(errorNum > 0) {
					uni.showModal({
						content: '请选择款式和尺码',
						showCancel: false
					})
					return false
				}
				let copy_arr = JSON.parse(JSON.stringify(arr))
				Object.keys(copy_arr).forEach(k => {
					copy_arr[k].forEach((item, i) => {
						if(i < copy_arr[k].length - 1) {
							if(item.size === copy_arr[k][i + 1].size && item.sex === copy_arr[k][i + 1].sex) {
								item.num += copy_arr[k][i + 1].num
								copy_arr[k][i + 1].num = 0
							}
						}
					})
					copy_arr[k] = copy_arr[k].filter(item => Number(item.num) > 0)
				})
				uni.showLoading()
				publishRuKuNew({
					put_type: 1,
					data: copy_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: typeof res.message === "string" ? res.message : "入库失败",
							showCancel: false
						})
					}
				}).catch(() => {
					uni.hideLoading()
					uni.showModal({
						content: "入库失败",
						showCancel: false
					})
				})
			},
			toAddSku(parent, i) { // 添加尺码
				if(i) {
					parent.skuList.splice(i, 1)
				} else {
					parent.skuList.push({
						sex: '',
						size: '',
						num: 0
					})
				}
			},
			// 尺码选择
			toChangeSize(e, data, parent) {
				let i = e.detail.value
				data.size = parent.pickerSizeList[String(data.sex)][i]
			},
			// 男女款选择
			toChangeSex(e, data, parent) {
				let i = e.detail.value
				data.sex = this.sexList[i].value
				data.size = ''
			},
			// 获取图片
			getStockGoods() {
				const unitMap = new Map([
					['hard', { name: '套', num: 1 }],
					['simple', { name: '套', num: 1 }],
					['old', { name: '套', num: 1 }],
					['new_old', { name: '套', num: 1 }],
					['youth', { name: '套', num: 1 }],
				])
				const _this = this
				stockGoods().then(res => {
					if(res.code === 200) {
						_this.list = Object.keys(res.data).map(k => ({
							k: k,
							imgurl: res.data[k].imgurl,
							name: res.data[k].name,
							unitName: unitMap.get(k).name,
							unitNum: unitMap.get(k).num,
							pickerSizeList: {
								'0': skuAllList.filter(stem => stem.type.includes(k) && Number(stem.sex) === 0).map(xtem => xtem.size),
								'1': skuAllList.filter(stem => stem.type.includes(k) && Number(stem.sex) === 1).map(xtem => xtem.size)
							},
							skuList: [
								{
									sex: '',
									size: '',
									num: 0,
								}
							]
						}))
					}
				})
			},
			toUrlLink(url) {
				if(!url) return false
				uni.navigateTo({
					url: `../${url}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		display: flex;
		flex-direction: column;
	}
	.ChuKuContainer {
		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;
		}
		.reviseFooter {
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 98rpx;
			background-color: #ffffff;
			border-top: 2rpx solid #EEEEEE;
			padding: 0 30rpx;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			.footer-right {
				margin-right: auto;
				.text1 {
					color: #999999;
					font-size: 28rpx;
					line-height: 40rpx;
				}
				.text2 {
					color: #333333;
					font-size: 28rpx;
					line-height: 40rpx;
					margin-left: 10rpx;
				}
				.text3 {
					color: #EA4A41;
					font-size: 40rpx;
					line-height: 56rpx;
				}
				.text4 {
					color: #EA4A41;
					font-size: 28rpx;
					line-height: 40rpx;
				}
			}
			.footer-text {
				color: #999999;
				font-size: 28rpx;
				line-height: 40rpx;
			}
			.footer-btn {
				width: 192rpx;
				height: 68rpx;
				border-radius: 68rpx;
				text-align: center;
				line-height: 68rpx;
				background: linear-gradient(141deg, #F97C55 0%, #F44545 100%);
				color: #FFFFFF;
				font-size: 28rpx;
				margin-left: 12rpx;
			}
		}
		.all {
			width: 100%;
			height: 116rpx;
			padding: 0 30rpx;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			.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 {
							.text {
								color: #333333;
								font-size: 32rpx;
								line-height: 44rpx;
								font-weight: bold;
								&.spec {
									color: #3394F8 !important;
								}
							}
							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;
						}
					}
				}
				.goodsAll {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin-bottom: 30rpx;
					padding-bottom: 36rpx;
					border-bottom: 1px solid #EEEEEE;
					.sex {
						color: #333333;
						font-size: 28rpx;
						line-height: 40rpx;
					}
					.num {
						color: #999999;
						font-size: 28rpx;
						.spec {
							color: #EA4A41 !important;
							font-size: 40rpx !important;
							font-weight: bold;
						}
					}
				}
				.skuList{
					width: 100%;
					border-top: 1px solid #EEEEEE;
					padding: 30rpx 0;
					.skuItem {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 30rpx;
						&:nth-last-of-type(1) {
							margin-bottom: 0;
						}
						.select {
							height: 72rpx;
							width: 156rpx;
							padding: 20rpx;
							box-sizing: 0 border-box;
							border-radius: 8rpx;
							background-color: #F5F5F5;
							display: flex;
							align-items: center;
							justify-content: flex-end;
							.value {
								color: #333333;
								font-size: 32rpx;
								line-height: 44rpx;
								text-align: center;
								flex: 1;
								overflow: hidden;
							}
							&::after{
								content: "";
								display: block;
								width: 30rpx;
								height: 30rpx;
								background-image: url(../../static/new_my/arrow.png);
								background-position: center;
								background-size: cover;
								background-repeat: no-repeat;
								margin-left: 10rpx;
								transform: rotate(90deg);
							}
						}
						.countBox {
							width: 192rpx;
						}
						.addIcon {
							width: 40rpx;
							height: 40rpx;
							background-image: url(../../static/new_stock/add.png);
							background-repeat: no-repeat;
							background-size: 100%;
							background-position: center;
							&.delete {
								background-image: url(../../static/new_stock/delete.png) !important;
							}
						}
					}
				}
				.dataAll {
					width: 100%;
					display: flex;
					align-items: baseline;
					justify-content: flex-end;
					.sex {
						color: #333333;
						font-size: 28rpx;
						margin-right: 30rpx;
					}
					.unit {
						color: #3394F8;
						font-size: 32rpx;
						margin-right: 30rpx;
						font-weight: bold;
					}
					.num {
						color: #999999;
						font-size: 28rpx;
						.spec {
							color: #EA4A41 !important;
							font-size: 40rpx !important;
							font-weight: bolder;
						}
					}
				}
			}
		}
	}
</style>

