<template>
	<view>
		<StickyNavBar>
			<NavBar title="销售退货上架"></NavBar>
			<FactorySelect v-model="warehouseCode" />
			<view class="btn">
				<uni-easyinput prefixIcon="search" class="topSearch" v-model="nlpla" focus placeholder="请扫描库位" @change="handleInputNlpla"></uni-easyinput>
				<uni-easyinput
					prefixIcon="search"
					:disabled="isDisabled"
					v-model="wxparams"
					:focus="isFocus"
					placeholder="请扫描产品包装条码/外箱码"
					@input="handleInputPlnr"
				></uni-easyinput>
			</view>
			<view class="total">
				<view class="left">入库明细</view>
				<view class="right">
					批次数：
					<text class="blue">{{ goodsList.length }}</text>
					<text class="line">|</text>
					总数量：
					<text class="blue">{{ totalNum }}</text>
				</view>
			</view>
		</StickyNavBar>
		<view class="cardbox">
			<view class="cardContent" v-for="(item, index) in goodsList">
				<view class="listItemTitle">
					<view>
						<view class="item">入库批次：{{ item.CHARG }}</view>
						<view class="item">物料编号：{{ item.MATNR }}</view>
						<view class="item">物料描述：{{ item.MAKTX }}</view>
						<view class="item">入库数量：{{ item.ERFMG }}</view>
						<view class="item">入库库位：{{ item.MATNR }}</view>
					</view>
					<u-button size="mini" type="primary" plain @click="deleGonds(index)">
						<u-icon name="trash" size="28"></u-icon>
						删除
					</u-button>
				</view>
				<view class="listBottom">
					<view class="bottomTitle" @tap="show = !show">
						<view class="desc">卷条码明细</view>
						<view class="topIcons">
							<view v-if="show">
								<u-icon name="arrow-down" color="#aaa" size="32"></u-icon>
								展开
							</view>
							<view v-else>
								<u-icon name="arrow-up" color="#aaa" size="32"></u-icon>
								收起
							</view>
						</view>
					</view>
					<view v-if="!show" class="bottomBox">
						<view class="bottomItem">
							<view class="items">{{ item.CHARG }}</view>
							<view class="items">{{ item.MATNR }}</view>
							<view class="items">数量：{{ item.ERFMG }}</view>
							<view class="items">生产日期：{{ item.BLDAT }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
	</view>
</template>

<script>
import StickyNavBar from '@/components/StickyNavBar/index.vue';
import ContentLoadingMore from '@/components/ContentLoadingMore/index.vue';
import BottomBtn from '@/components/BottomBtn/index.vue';

export default {
	components: {
		StickyNavBar,
		ContentLoadingMore,
		BottomBtn
	},
	data() {
		return {
			number: 0,
			totalNum: 0,
			show: true,
			nlpla: '',
			wxparams: '',
			warehouseCode: '',
			btnArr: [
				{
					style: '',
					type: 'warning',
					text: '返回',
					way: 'goBack'
				},
				{
					style: '',
					type: 'primary',
					text: '提交',
					way: 'goSubmit'
				}
			],
			goodsList: [],
			isFocus: false,
			isDisabled: false
		};
	},
	created() {
		this.getPlnr('10000820240325000001|10000320100');
	},
	methods: {
		deleGonds(idx) {
			this.goodsList.splice(idx, 1);
			this.totalNum = 0;
			this.goodsList.forEach((ele) => {
				this.totalNum += ele.ERFMG;
			});
		},
		getBtnHandle(row) {
			this[row.way]();
		},
		goBack() {
			console.log('进来')
			uni.navigateBack();
		},
		goSubmit() {
			if (this.goodsList.length === 0) {
				return;
			}
			console.log([
				{
					BLDAT: '20240325',
					BUDAT: '20240325',
					ZENCODE: 'A014',
					ITEM: this.goodsList
				}
			]);
			this.$u.api.sapApi
				.sapRrm([
					{
						BLDAT: '20240325',
						BUDAT: '20240325',
						ZENCODE: 'A014',
						// TYPE: 'CG',
						ITEM: this.goodsList
					}
				])
				.then((res) => {
					this.loading = false;
					if(!Array.isArray(res)){
						return
					}
					if (res.every((item) => item.TYPE === 'S')) {
						this.goodsList = [];
						this.totalNum = 0;
						this.nlpla = '';
						this.wxparams = '';
						this.$u.toast('提交成功');
					}
					const foundObject = res.find(item => item.TYPE === 'E');
					if(foundObject && foundObject.MESSAGE){
						this.$u.toast(foundObject.MESSAGE);
					}
				});
		},
		handleInputPlnr(e) {
			this.isFocus = false;
			e && this.getPlnr(e);
		},
		handleInputNlpla(e) {
			if (e) {
				this.nlpla = e;
				this.goodsList.forEach((ele) => {
					this.totalNum += ele.ERFMG;
					ele.NLPLA = ele.NLPLA || this.nlpla;
				});
			}
		},
		getPlnr(plnr) {
			this.totalNum = 0;
			this.isDisabled = true;
			this.$u.api.sapApi
				.sapPlnr({
					plnr
				})
				.then((res) => {
					if (res && res.RESULT.TYPE === 'S') {
						this.goodsList = this.goodsList.concat(res.DATA);
						this.goodsList.forEach((ele) => {
							this.totalNum += ele.ERFMG;
							ele.NLPLA = ele.NLPLA || this.nlpla;
						});
						setTimeout(() => {
							this.wxparams = '';
							this.isFocus = true;
							this.isDisabled = false;
						}, 1000);
					} else {
						setTimeout(() => {
							this.isFocus = true;
							this.isDisabled = false;
						}, 1000);
						this.$u.toast(res.RESULT.MESSAGE);
					}
					this.loading = false;
				});
		}
	}
};
</script>

<style lang="scss" scoped>
page {
	background: #f8f8f8;
}
.btn {
	padding: 20rpx;
	background: #fff;
	.topSearch {
		margin-bottom: 20rpx;
	}
}
.bottomBtn {
	position: sticky;
	padding: 20rpx;
	display: flex;
	justify-content: space-between;
	background: #fff;
	bottom: 0rpx;
	margin-top: 20rpx;
	.u-btn {
		width: 300rpx;
	}
}
.blue {
	color: #3f9dcc;
}
.total {
	background: #fff;
	color: #000;
	padding: 0rpx 40rpx;
	display: flex;
	justify-content: space-between;
	font-size: 32rpx;
	.line {
		margin: 0px 10rpx;
	}
}
.cardbox {
	min-height: calc(100vh - 480rpx);
	padding: 0rpx 20rpx;
	margin-top: 20rpx;
}
.cardContent {
	border-radius: 8px;
	background: #fff;
}
.listItemTitle {
	display: flex;
	justify-content: space-between;
	padding: 20rpx;
	border-bottom: 2px solid #f8f8f8;
	.u-btn {
		width: 150rpx;
		margin: 0px;
		margin-top: 10rpx;
	}
	.item {
		line-height: 50rpx;
		color: #000;
		font-size: 28rpx;
		font-weight: 500;
		word-break: break-all;
	}
}
.listBottom {
	.bottomTitle {
		height: 80rpx;
		position: relative;
		.desc {
			color: #bbb;
			font-size: 24rpx;
			line-height: 80rpx;
			padding-left: 20rpx;
		}
		.topIcons {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
			color: #aaa;
		}
	}
	.bottomBox {
		padding: 20rpx;
		.bottomItem {
			background: #f8f8f8;
			padding: 20rpx;
			margin-bottom: 20rpx;
			.items {
				color: #333;
				font-size: 24rpx;
				height: 40rpx;
				line-height: 40rpx;
			}
		}
	}
}
/deep/ .u-sticky {
	top: 0 !important;
	background: #fff;
}
</style>