<template>
	<view class="page">
		<view class="headerTitle">
			<view class="">
				<view class="items">退料申请:{{goodsList[0]&&goodsList[0].applyNo}}</view>
				<view class="items">编排单号:{{goodsList[0]&&goodsList[0].arrangeCode }}</view>
				<view class="items">退料日期:{{goodsList[0]&&goodsList[0].createTime }}</view>
				<view class="items">WC001:{{goodsList[0]&&goodsList[0].workshopCode}}</view>
				<view class="items">退料方式:{{goodsList[0]&&(goodsList[0].complete == "N" ? "余料退料" : "整单退料")}}</view>
			</view>
		</view>
		<view class="total">
			<view class="left">物料明细</view>
			<view class="right">
				物料数量:
				<text class="">{{ goodsList.length }}</text>
			</view>
		</view>
		<ContentLoadingMore class="cardbox" :list='goodsList'>
			<scroll-view scroll-y="true" style="height: 680rpx;">
				<view class="cardContent" v-for="(item, index) in goodsList" :key="index">
					<OddmentsItem :item='item' :list='list' />
				</view>
			</scroll-view>
		</ContentLoadingMore>
		<BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
	</view>
</template>

<script>
	import ContentLoadingMore from '@/components/ContentLoadingMore/index.vue';
	import OddmentsItem from './oddmentsItem.vue';
	import BottomBtn from '@/components/BottomBtn/index.vue';
	export default {
		components: {
			ContentLoadingMore,
			OddmentsItem,
			BottomBtn,
		},
		data() {
			return {
				number: 0,
				show: true,
				nlpla: '',
				btnArr: [{
						style: '',
						type: 'warning',
						text: '返回',
						way: 'goBack'
					},
					{
						style: '',
						type: 'primary',
						text: '提交',
						way: 'goSubmit'
					}
				],
				goodsList: [],
				applyNo: '',
				list: [],
			};
		},
		computed: {},
		onLoad(option) {},
		watch: {},
		created() {
			const pages = getCurrentPages(); // 获取页面栈
			const currentPage = pages[pages.length - 1]; // 获取当前页面的对象
			const options = currentPage.options; // 如果是通过路由带参数的话，参数会在这里
			console.log('当前页面参数:', options);
			if (currentPage.options.complete == "N") {
				this.applyNo = currentPage.options.order;
				this.getOrder(currentPage.options.order);
			}
		},
		methods: {
			getOrder(applyNo) {
				this.$u.api.mesApi
					.pdaMaterialReturn({
						pageNum: 1,
						pageSize: 10,
						applyNo
					})
					.then((res) => {
						if (res && res.code - 200 === 0) {
							this.list = res.rows
							const data = res.rows
							const result = data.filter((item, index, self) => {
								return self.findIndex(t => t.sapItemCode === item.sapItemCode) === index;
							});
							this.goodsList = [...result]
						} else {
							this.$u.toast(res.msg);
						}
						this.loading = false;
					});
			},
			getBtnHandle(row) {
				this[row.way]();
			},
			goBack() {
				console.log('进来')
				uni.navigateBack();
			},
			goSubmit() {
				let flag = true;
				if (this.list.length === 0) {
					return;
				}
				let arr = this.list
				arr.forEach((ele, index) => {
					if (!ele.ERFMG) {
						flag = false
					} else {
						ele.MATNR = ele.sapItemCode
						ele.WERKS = this.vuex_user_factory
						ele.CHARG = ele.batchNo
						ele.AUFNR = ele.workorderCode
						ele.MTSNR = ele.applyNo
						ele.ZEILE = index + 1
						ele.ZYL = 'X'
					}
				})
				if (!flag) {
					return this.$u.toast(`存在未填写退料数量的物料单`)
				}
				const params = {
					'zencode': 'A009',
					'item': arr
				}
				this.$u.api.sapApi
					.sapProiss(params)
					.then((res) => {
						this.loading = false;
						if (!Array.isArray(res)) {
							return
						}
						if (res.every((item) => item.TYPE === 'S')) {
							// this.$u.toast('提交成功');
							this.handleMes(arr)
						}
						const foundObject = res.find(item => item.TYPE === 'E');
						if (foundObject && foundObject.MESSAGE) {
							this.$u.toast(foundObject.MESSAGE);
						}
					});
			},
			handleMes(arr) {
				arr.forEach(ele => {
					ele.batchCode = ele.CHARG
					ele.returnNum = ele.ERFMG
				});
				this.$u.api.mesApi
					.pdaMaterialReturnNum(arr)
					.then((res) => {
						this.loading = false;
						console.log(res);
						if (res && res.code == 200) {
							this.$u.toast('提交成功');
							setTimeout(() => {
								this.goBack()
							}, 1000);
						} else {
							this.$u.toast(res.msg);
						}
					});
			},
		}
	};
</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;
		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;
		}
	}

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

	.checkedCase {
		margin-top: 10rpx;
	}
</style>