<template>
	<view class="lists">
		<StickyNavBar>
			<NavBar title="发货下架"></NavBar>
			<view class="btn">
				<uni-easyinput class="boxSelect" prefixIcon="search" v-model.trim="lgort" placeholder="请扫描目标仓位"></uni-easyinput>
				<uni-easyinput class="boxSelect" prefixIcon="search" v-model.trim="vbeln" placeholder="请扫描交货单"></uni-easyinput>
				<uni-easyinput prefixIcon="search" :disabled="isDisabled" v-model.trim="wxparams" :focus="isFocus"
					placeholder="请扫描产品包装条码/外箱码" @input="handleInputPlnr"></uni-easyinput>
			</view>
		</StickyNavBar>
		<view class="cardbox">
			<view class="cardContent" v-for="(item, index) in goodsList" :key="item.PLNR">
				<view class="listItemTitle">
					<view>
						<view class="item">{{ item.ZCODE ? 'BL':'PL' }}号：{{ item.ZCODE || item.PLNR }}</view>
					</view>
					<u-button size="mini" type="primary" plain @click="deleGonds(index)">
						<u-icon name="trash" size="28"></u-icon>
						删除
					</u-button>
				</view>
			</view>
		</view>

		<BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
	</view>
</template>

<script>
	import BottomBtn from '@/components/BottomBtn/index.vue';
	import StickyNavBar from '@/components/StickyNavBar/index.vue';
	import {
		parseTime
	} from "@/utils/ruoyi";

	export default {
		components: {
			StickyNavBar,
			BottomBtn
		},
		data() {
			return {
				wxparams: "",
				lgort: '',
				vbeln: '',
				isDisabled: false,
				isFocus: false,
				goodsList: [],
				btnArr: [{
						style: '',
						type: 'warning',
						text: '返回',
						way: 'goBack'
					},
					{
						style: '',
						type: 'primary',
						text: '提交',
						way: 'goSubmit'
					}
				],
			}
		},
		methods: {
			handleInputPlnr(e) {
				if (this.goodsList.some((item) => item.ZCODE === e || item.PLNR === e)) {
					this.$u.toast('已经存在当前条码');
					this.reset()
					return
				}
				// bl 长度 15 pl 长度 80
				const bl = e.length <= 15 && e.length >= 13;
				const pl = e.length > 15
				if (bl) {
					this.isFocus = false;
					this.isDisabled = true;
					this.goodsList.push({
						ZCODE: e,
						PLNR: ''
					})
					this.reset()
				} else if (pl) {
					this.isFocus = false;
					this.isDisabled = true;
					this.goodsList.push({
						PLNR: e,
						ZCODE: ''
					})
					this.reset()
				}
			},
			deleGonds(index) {
				this.goodsList.splice(index, 1)
			},
			reset() {
				setTimeout(() => {
					this.isFocus = true;
					this.isDisabled = false;
					this.wxparams = ''
				}, 1000);
			},
			goBack() {
				uni.navigateBack()
			},
			getBtnHandle(row) {
				console.log('getBtnHandle', row);
				this[row.way]();
			},
			// 提交
			goSubmit() {
				if (!this.goodsList.length) {
					return this.$u.toast('请扫描需要下架的箱条码/外箱码')
				}
				if (!this.vbeln) {
					return this.$u.toast('请扫描交货单')
				}
				if (!this.lgort) {
					return this.$u.toast('请选择目标仓位')
				}
				const arr = this.goodsList.map((item) => {
					item.LGORT = this.lgort
					item.VBELN = this.vbeln
					return item
				})
				this.$u.api.sapApi
					.sapCasePlnr2(arr)
					.then((res) => {
						const flag = res.some((item) => {
							const b = item.TYPE == 'E'
							if (b) {
								this.$u.toast(item.MESSAGE)
							}
							return b
						})
						if (!flag) {
							this.$u.toast(res[0].MESSAGE);
							this.goodsList = []
						}
					})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.lists {
		flex: 1;
		flex-direction: column;
	}

	page {
		background: #f8f8f8;
	}

	/deep/ .u-sticky {
		top: 0 !important;
		background: #fff;
	}

	.btn {
		padding: 20rpx;
		background: #fff;

		.boxSelect {
			margin-bottom: 20rpx;
		}
	}

	.cardContent {
		border-radius: 8px;
		background: #fff;
		margin-bottom: 20rpx;

	}

	.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: #333;
			font-size: 28rpx;
			font-weight: 500;
			word-break: break-all;
			font-weight: 600;
		}
	}
</style>
