<template>
	<view class="page">
		<StickyNavBar>
			<NavBar title="完工入库扫描"></NavBar>
			<view class="selectSheet">
				<u-row >
					<u-col span="12">
						<u-picker v-model="depotShow" mode="selector" :default-selector="[0]" range-key="LGOBE" :range="depotLists" @confirm="actionSheetCallback"></u-picker>
						<u-input v-model="LGOBE" placeholder="请选择仓库别" :type="type" :border="border" @click="depotShow = true" />
					</u-col>
				</u-row>
			</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="请扫描产品包装条码/外箱码"
					@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>
		<ContentLoadingMore class="cardbox" :list="goodsList">
			<view class="cardContent" v-for="(item, index) in goodsList">
				<scanningList :item='item' :idx="index" @deleGonds="deleGonds" :list='list'/>
			</view>
		</ContentLoadingMore>
		<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';
import { parseTime} from "@/utils/ruoyi";
import scanningList from "./scanningList.vue"

export default {
	components: {
		StickyNavBar,
		ContentLoadingMore,
		BottomBtn,
		scanningList
	},
	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,
			depotShow: false,
			depotLists: [],
			LGOBE: '',
			LGORT: '',
			type: 'select',
			border: true,
			list:[]

		};
	},
	created() {
		this.getDepotLists();
		// 二维码包含：10位物料号（右对齐不足补充为0）物料号12位生产单号（右对齐不足补充为0），
		// 10位批次号（右对齐不足补充为0），8位数量（100000.0）（右对齐不足补充为0） 
		// this.getPlnr('006000000100100000000300000501001');
	},
	computed:{
		totalNum(){
			let num = 0;
			this.goodsList.forEach((ele) => {
				num += ele.quantity;
			});
			return num
		}
	},
	methods: {
		getDepotLists() {
			this.$u.api.sapApi
				.sapDict({
					FIELDNAME: 'T001L'
				})
				.then((res) => {
					if (res) {
						res.forEach((ele) => {
							ele.text = ele.LGOBE;
						});
						this.depotLists = res;
					} else {
						this.depotLists = [];
					}
				});
		},
		// 点击actionSheet回调
		actionSheetCallback(index) {
			console.log(index, 'index');
			this.LGOBE = this.depotLists[index].LGOBE;
			this.LGORT = this.depotLists[index].LGORT;
		},
		deleGonds(idx) {
			this.goodsList.splice(idx, 1);
			
		},
		getBtnHandle(row) {
			this[row.way]();
		},
		goBack() {
			uni.navigateBack();
		},
		goSubmit() {
			if (this.goodsList.length === 0) {
				return;
			}
			let params = []
			let item = []
			this.goodsList.forEach(ele => {
				item.push({
						AUFNR:ele.workorderNo,
						PLNR: ele.qrcode,
						MATNR: ele.sapItemCode,
						WERKS: ele.factory,
						LGORT: this.LGORT,
						PACKNR: ele.qrcode,
						ZSIZE: ele.standardSize,
						ZINSTNUM: ele.clientCmd,
						BRGEW: ele.grossWeight,
						NTGEW: ele.netWeight,
						GEWEI: ele.unitOfMeasure,
						INSNAM: ele.checkBy,
						ZMNGO: ele.totalQuantity,
						ZMNGL: ele.lossNum,
					})
			});
			params.push({
				lfsnr: this.goodsList[0].workorderNo,
				BLDAT: 20240420,
				BUDAT: 20240420,
				// BLDAT: parseTime(new Date(),'{y}{m}{d}'),
				// BUDAT: parseTime(new Date(),'{y}{m}{d}'),
				item,
			})
			this.$u.api.sapApi
				.sapOrder(params)
				.then((res) => {
					this.loading = false;
					if(!Array.isArray(res)){
						return
					}
					if (res.every((item) => item.TYPE === 'S')) {
						this.goodsList = [];
						this.LGOBE = '';
						this.LGORT = '';
						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) {
			e && this.getPlnr(e);
		},
		handleInputNlpla(e) {
			console.log(`output->e`,e)
		},
		reset(){
			setTimeout(() => {
				this.wxparams = '';
				this.isFocus = true;
				this.isDisabled = false;
			}, 1000);
		},
		getPlnr(qrcode) {
			this.isDisabled = true;
			this.isFocus = false
			if(!this.nlpla){
				this.$u.toast('请先扫描仓位');
				this.reset()
				return
			}
			this.$u.api.mesApi
				.pdaFindPackageLinkInfo({
					qrcode
				})
				.then((res) => {
					if (res && res.code == 200) {
						if (this.goodsList.some((item) => item.workorderNo !== res.data[0].workorderNo)) {
							this.$u.toast('请扫描同一个工单');
							this.reset()
							return
						}
						if (this.goodsList.some((item) => item.qrcode === qrcode)) {
							this.$u.toast('已经存在当前条码');
							this.reset()
							return
						}
						res.data.forEach(ele => {
							ele.NLPLA = this.nlpla
						});
						this.list = this.list.concat(res.data)

						const result = res.data.filter((item, index, self) => {
						    return self.findIndex(t => t.qrcode === item.qrcode) === index;
						});
						this.goodsList = this.goodsList.concat(result)
						this.reset()
					} else {
						this.reset()
						this.$u.toast(res.msg);
					}
					this.loading = false;
				});
		}
	}
};
</script>

<style lang="scss" scoped>
.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;
	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: #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;
}
.selectSheet {
	padding: 20rpx 10rpx;
	padding-bottom: 0;
	background: #fff;
}
</style>