<template>
	<view class="lists">
		<StickyNavBar class="stickyBox">
			<NavBar title="库存盘点"></NavBar>
			<view class="planBox">
				<view class="planTitle">盘点计划：{{ inventoryInfo.IVNUM || inventoryInfo.IBLNR }}</view>
				<!-- <view class="planTip">仓库：{{ inventoryInfo.LGOBE }} | 存货单元：{{ inventoryInfo.LGPLA }}</view> -->
			</view>
		</StickyNavBar>
		<view class="scanningBtn">
			<uni-easyinput prefixIcon="search" :focus="isFocus" :disabled="isDisabled" v-model="wxparams" focus placeholder="请扫描盘点条码" @input="handleInput">
			</uni-easyinput>
		</view>
		<view class="inventoryDetail">
			<view class="detailTitle">盘点明细</view>
			<!-- <view class="">
				<u-button size="mini" class="" type="success" @click="addShowModel = true">
					<u-icon name="plus"></u-icon>
					新增
				</u-button>
			</view> -->
		</view>
		<view class="cardContent" v-for="(item, index) in goodsList" :key="item.MATNR">
			<view class="cardItem">
				<view class="bottomItem">
					<view class="items">物料编码：{{item.MATNR}}({{item.list.length}})</view>
					<view class="items">物料名称：{{item.MAKTX}}</view>
				</view>
				<view class="boxRight">
					<view class="items" style="display: flex;">
						<view style="margin-right:10rpx">{{sumMENGA(item)}}</view>
						<view v-show="urlPs.FLAG && urlPs.FLAG === 'X'"> / {{item.GESME}} {{ item.MEINS }}</view>
					</view>
				</view>
			</view>
			<view class="listBottom">
				<view class="bottomTitle" @tap="toggleShow(item,index)">
					<view class="topIcons">
						<view v-if="item.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="!item.show" class="bottomBox">
					<view class="boxAll" v-for="(itm, inde) in item.list" :key="inde">
						<view class="bottomItem">
							<view class="items">仓位：{{ itm.LGPLA }}</view>
							<view class="items">存储地点：{{ itm.LGORT }} - {{ itm.LGOBE }}</view>
						</view>
						<view class="boxRight">
							<view class="items">
								<uni-easyinput v-if="isIM" v-model.number="itm.ERFMG" style="width: 200rpx;margin-right:10rpx"/>
								<uni-easyinput v-else="isIM" v-model.number="itm.MENGA" style="width: 200rpx;margin-right:10rpx"/>
								<view v-show="urlPs.FLAG && urlPs.FLAG === 'X'">/ {{itm.PACMG}} {{itm.MEINS}}</view>
							</view>
							<!-- <view class="itemsBtn">
								<u-button size="mini" class="" type="primary" @click="checkShowModel = true">盘点
								</u-button>
							</view> -->
						</view>
					</view>
				</view>
			</view>
		</view>
		<BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
		
		<u-modal v-model="checkShowModel" @confirm="addConfirm" ref="checkShowModel" :show-title="false"
			:show-cancel-button="true">
			<view class="slot-content">
				<u-form :model="checkForm" ref="checkForm">
					<uni-easyinput prefixIcon="search" class="topSearch" v-model="checkForm.name" focus
						placeholder="请扫描盘点库位条码" @input="handleInput"></uni-easyinput>
					<u-form-item label="数量">
						<u-input v-model="checkForm.name" />
					</u-form-item>
				</u-form>
			</view>
		</u-modal>
	</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 {
				urlPs: {},
				wxparams: '',
				isFocus: true,
				isDisabled: false,
				btnArr: [{
					style: '',
					type: 'warning',
					text: '返回',
					way: 'goBack'
				},{
					style: '',
					type: 'primary',
					text: '盘点确认',
					way: 'submitForm'
				}],
				inventoryList: [],
				inventoryInfo: {},
				addShowModel: false,
				checkShowModel: false,
				addForm: {
					name: ''
				},
				checkForm: {
					name: ''
				},
				goodsList: []
			};
		},
		onLoad(option) {
			this.urlPs = option
			this.$u.api.sapApi
				.sapInventory(this.urlPs).then(res => {
					if (res.RDATA_IM.length || res.RDATA_WM.length) {
						// 是否是 IM 数据结构
						this.isIM = Reflect.has(this.urlPs, 'IBLNR');
						this.inventoryInfo = this.isIM ? res.RDATA_IM[0] : res.RDATA_WM[0];
						this.inventoryList = this.isIM ? res.RDATA_IM : res.RDATA_WM

						this.setGoodsList()
					} else {
						// 不能盘点
						this.$u.toast(res.MSG[0].MESSAGE || "该盘点计划暂无库存数据");
					}
				})
		},
		computed: {
			sumMENGA(){
				return function(item){
					return item.list.reduce((pre, item) => {
						return pre + Number(this.isIM ? item['ERFMG'] : item['MENGA'])
					}, 0)
				}
			}
		},
		methods: {
			setGoodsList() {
				this.goodsList = this.inventoryList.reduce((acc, cur) => {
					// 没有分类的产品编码
					const item = acc.find(v => v.MATNR == cur.MATNR)
					if (!item) {
						acc.push({
							MATNR: cur.MATNR, // 物料编码
							MAKTX: cur.MAKTX, // 物料名称
							GESME: cur.GESME || cur.PACMG, // 该物料盘点总数
							MEINS: cur.MEINS, // 单位
							show: true,
							list: [cur]
						})
					} else {
						// 有产品分类
						item.list.push(cur)
						if (this.isIM) {
							item.GESME += cur.PACMG
						}
					}
					return acc
				}, [])
			},
			resetInput() {
				setTimeout(() => {
					this.wxparams = '';
					this.isFocus = true;
					this.isDisabled = false;
				}, 1000);
			},
			handleInput(e) {
				this.isDisabled = true
				this.goodsList.forEach(element => {
					element.list.forEach(item => {
						if (item['PLNR'] === e) {
							if (this.isIM) {
								item.ERFMG = item.PACMG
							} else {
								item.MENGA = item.PACMG
							}
						}
					})
				})
				this.resetInput()
			},
			toggleShow(item, index) {
				console.log('触发了当前选项！', item)
				item.show = !item.show
				this.$set(this.goodsList, index, item)
			},
			getBtnHandle(row) {
				this[row.way]();
			},
			submitForm() {
				let params
				this.inventoryList.forEach((item) => {
					item.ERFME = item.MEINS
				})
				if (this.isIM) {
					params = {
						RDATA_IM: this.inventoryList,
						RDATA_WM: []
					}
				} else {
					params = {
						RDATA_IM: [],
						RDATA_WM: this.inventoryList
					}
				}
				this.$u.api.sapApi
				.postInventory(params)
				.then((res) => {
					if(res && res[0]) {
						const errorMsg = res.find(item => item.TYPE != 'S')
						if (errorMsg) {
							return this.$u.toast(errorMsg.MESSAGE)
						}
						this.$u.toast(res[0].MESSAGE)
						this.goBack()
					}
				})
			},
			goBack() {
				setTimeout(() => {
					uni.navigateBack()
				}, 2000);
			},
			ontabtap(e, type) {
				console.log('切换tab', e, type);
			},
			addConfirm() {
				this.addShowModel = false;
			}
		}
	};
</script>

<style lang="scss">
	/deep/ .u-sticky {
		top: 0 !important;
		background: #fff;
	}

	.planBox {
		padding: 20rpx;
		border-bottom: 1px solid #333;
	}

	.planTitle {
		font-size: 32rpx;
		font-weight: 600;
		text-align: center;
		line-height: 60rpx;
	}

	.planTip {
		font-size: 28rpx;
		text-align: center;
		line-height: 40rpx;
	}

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

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

	.workTitle {
		font-size: 32rpx;
		font-weight: 600;
		line-height: 40rpx;
		margin: 20rpx 0 0 40rpx;
	}

	.inventoryDetail {
		display: flex;
		justify-content: space-between;
		padding: 0 20rpx;
	}

	.detailTitle {
		font-size: 32rpx;
		font-weight: 600;
		line-height: 46rpx;
	}

	.listBottom {
		padding: 0 10rpx 20rpx 10rpx;

		.bottomTitle {
			height: 60rpx;
			position: relative;
			display: flex;
			justify-content: space-between;

			.topIcons {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				color: #aaa;
			}
		}

		.bottomBox {
			.boxAll {
				display: flex;
				justify-content: space-between;
				background: #fff;
				border-radius: 10rpx;
				margin-top: 10rpx;
			}

			.bottomItem {
				padding: 20rpx;

				.items {
					color: #333;
					font-size: 24rpx;
					height: 40rpx;
					line-height: 40rpx;
				}
			}

			.boxRight {
				padding: 20rpx 20rpx 0 20rpx;

				.items {
					color: #333;
					font-size: 24rpx;
					height: 40rpx;
					line-height: 40rpx;
					align-items: center;
					display: flex;
				}

				.itemsBtn {
					float: right;
					padding: 20rpx 0 10rpx;
				}
			}
		}
	}

	.cardContent {
		margin: 20rpx;
		background: #f8f8f8;
	}

	.cardItem {
		padding: 20rpx 10rpx;
		display: flex;
		justify-content: space-between;
		border-bottom: 1px solid #333;
	}

	.slot-content {
		padding: 20rpx;

		.u-form-item {
			padding: 0;
		}
	}
</style>
<style lang="scss" scoped>
/deep/ .boxRight {
	.uni-easyinput__content {
		.uni-easyinput__content-input {
			height: 52rpx;
			font-size: 24rpx;
		}
		.uni-icons.content-clear-icon.uniui-clear {
			font-size: 24rpx;
		}
	}
}
</style>