<template>
	<view class="ChuKuContainer">
		<!-- <view class="nav">
			<view
				v-for="(nav, ni) in navList"
				:key="ni"
				class="nav-item"
				:class="navIndex === ni ? 'active' : ''"
				@click="navIndex = ni"
			>
				{{ nav }}
			</view>
		</view> -->
		<view class="all">
			<view class="num">
				<text>待出库：总共</text>
				<text
					class="spec">{{ list.reduce((a, b) => a + b.children.reduce((x, y) => x + Number(y.num), 0), 0) }}</text>
				<text>套</text>
			</view>
			<view class="flexS">
				<!-- <view class="btn1" @click="toUrlLink('stock/code_record')">扫码记录</view> -->
				<view class="btn" @click="toUrlLink('stock/chuku_record')">出库记录</view>
			</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">{{ item.name }}</view>
						<view class="fixed">一年零一天不满意退钱</view>
					</view>
				</view>
				<view class="goodsAll">
					<view class="sex">
						男款:{{ item.children.filter(v => Number(v.sex)).reduce((a, b) => a + Number(b.num), 0) }}套 /
						女款:{{
							item.children.filter(v => !Number(v.sex)).reduce((a, b) => a + Number(b.num), 0)
						}}套
					</view>
					<view class="num">
						<text>合计：</text>
						<text class="spec">{{ item.children.reduce((a, b) => a + Number(b.num), 0) }}套</text>
					</view>
				</view>
				<view class="skuList">
					<view v-for="(btem, bi) in item.skuList" :key="bi" class="skuItem">
						<view class="type">{{ +btem.sex ? '男款' : '女款' }}</view>
						<view class="size">{{ btem.size }}</view>
						<view class="num">
							<count2 :countNum.sync="btem.num" />
						</view>
					</view>
				</view>
				<view class="dataAll">
					<view class="sex">
						男款:{{ item.skuList.filter(v => Number(v.sex)).reduce((a, b) => a + Number(b.num), 0) }}套 / 女款:{{
							item.skuList.filter(v => !Number(v.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="reviseFooter">
			<view class="footer-right">
				<text
					class="text1">{{ list.filter(item => item.skuList.reduce((x, y) => x + Number(y.num), 0) > 0).length }}类</text>
				<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-text">
				剩余：
				{{ list.reduce((a, b) => a + (b.children.reduce((x,y) => x + Number(y.num), 0)), 0) - list.reduce((a, b) => a + (b.skuList.reduce((x,y) => x + Number(y.num), 0)), 0) }}
				套
			</view> -->
			<view class="footer-btn" @click="toSubmitChuku">确认出库</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 '../stock/components/count2.vue';
	import {
		stockGoods,
		getPanKuStock,
		publishChuKu
	} from '@/apis/stock.js';
	export default {
		components: {
			Count2
		},
		data() {
			return {
				sexList: [{
					name: '男款',
					value: 1
				}, {
					name: '女款',
					value: 0
				}],
				list: [],
				navList: ['零售', '出货', '其他'],
				navIndex: 0
			};
		},
		onShow() {
			this.init();
		},
		methods: {
			selectSizeTip() {
				// 尺码选择提醒
				uni.showModal({
					content: '请先选择款式',
					showCancel: false
				});
			},
			// 出库
			toSubmitChuku() {
				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();
				publishChuKu({
						type: this.navIndex,
						arr: 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 = '';
			},
			deepClone2(obj) {
				var _obj = JSON.stringify(obj),
					objClone = JSON.parse(_obj);
				return objClone;
			},
			// 初始化
			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.list = Object.keys(stockList).map(k => ({
							k: k,
							children: stockList[k],
							imgurl: goodsList[k].imgurl,
							name: goodsList[k].name,
							skuList: this.deepClone2(skuAllList.slice(0).filter(xtem => xtem.type.includes(
								k)))
						}));
					})
					.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>
	page {
		display: flex;
		flex-direction: column;
	}

	.ChuKuContainer {
		width: 100%;
		flex: 1;
		background-color: #f9f9fb;

		.nav {
			width: 100%;
			height: 104rpx;
			padding: 0 60rpx;
			box-sizing: border-box;

			background-color: #ffffff;

			display: flex;
			align-items: center;
			justify-content: space-between;

			&-item {
				position: relative;

				color: #333333;
				font-size: 32rpx;
				line-height: 44rpx;

				&.active {
					color: #ea4a41 !important;

					&::after {
						content: '';
						display: block;

						position: absolute;
						bottom: 0;
						left: 50%;

						transform: translateX(-50%);

						width: 30rpx;
						height: 4rpx;
						border-radius: 4rpx;

						background: linear-gradient(97deg, #f97c55 0%, #f44545 100%);
					}
				}
			}
		}

		.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: space-between;

			.num {
				color: #333333;
				font-size: 36rpx;
				line-height: 50rpx;
				font-weight: bold;

				.spec {
					color: #ea4a41 !important;
				}
			}

			.btn,
			.btn1 {
				width: 140rpx;
				height: 56rpx;
				border-radius: 8rpx;
				background-color: #ea4a41;
				color: #ffffff;
				font-size: 28rpx;
				line-height: 56rpx;
				text-align: center;
				text-align: center;
			}

			.btn1 {
				background: linear-gradient(180deg, #FFBB00 0%, #FF9100 100%);
				margin-right: 24rpx;
			}
		}

		.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;
						}
					}
				}

				.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%;
					margin-bottom: 30rpx;

					.skuItem {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
						margin-bottom: 30rpx;

						&:nth-last-of-type(1) {
							margin-bottom: 0;
						}

						.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;
						}

						.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;
					}

					.num {
						color: #999999;
						font-size: 28rpx;

						.spec {
							color: #ea4a41 !important;
							font-size: 40rpx !important;
							font-weight: bolder;
						}
					}
				}
			}
		}
	}
</style>
