<template>
	<view>
		<StickyNavBar>
			<NavBar title="委外发料"></NavBar>
			<view class="headerTitle">
				<view class="items">退料申请:MO2024032100001</view>
				<view class="items">编排单号:MO2024032100001</view>
				<view class="items">退料日期:2024/03/22</view>
				<view class="items">WC01:数码印刷车间</view>
			</view>
			<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="请扫描产品包装条码/外箱码"
					@confirm="handleConfirm"
				></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="">{{ goodsList.length }}</text>
				</view>
			</view>
		</StickyNavBar>
		<view class="cardbox">
			<view class="cardContent" v-for="(item, index) in goodsList">
				<view class="listItemTitle">
					<view>
						<view class="item">生产单号：{{ item.MATNR }}</view>
						<view class="item">物料批次：{{ item.MATNR }}</view>
						<view class="item">{{ item.MATNR }} | {{ item.MAKTX }}</view>
						<view class="item">
							<u-row gutter="16">
								库位：
								<uni-easyinput v-model="wxparams"></uni-easyinput>
							</u-row>
						</view>
						<view class="item">扫描人员：{{ item.MAKTX }}</view>
						<view class="item">退料数量：{{ item.MAKTX }}</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,
			show: true,
			nlpla: '',
			wxparams: '',
			btnArr: [
				{
					style: '',
					type: 'warning',
					text: '返回',
					way: 'goBack'
				},
				{
					style: '',
					type: 'primary',
					text: '提交',
					way: 'goSubmit'
				}
			],
			goodsList: [],
			isFocus: false,
			isDisabled: false
		};
	},
	created() {
		this.getPlnr('10000820240325000001|10000320100');
	},
	methods: {
		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.nlpla = '';
						this.wxparams = '';
						this.$u.toast('提交成功');
					}
					const foundObject = res.find(item => item.TYPE === 'E');
					if(foundObject && foundObject.MESSAGE){
						this.$u.toast(foundObject.MESSAGE);
					}
				});
		},
		handleConfirm(e) {
			this.isFocus = false;
			e && this.getPlnr(e);
		},
		handleInputNlpla(e) {
			if (e) {
				this.nlpla = e;
				this.goodsList.forEach((ele) => {
					ele.NLPLA = ele.NLPLA || this.nlpla;
				});
			}
		},
		getPlnr(plnr) {
			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) => {
							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;
}
.headerTitle{
	padding: 20rpx;
	font-weight: 600;
	.items{
		line-height: 40rpx;
	}
}
.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: #333;
		font-size: 28rpx;
		font-weight: 500;
		word-break: break-all;
		font-weight: 600;
	}
}
.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>