<template>
	<page-meta root-font-size="16px"></page-meta>
	<view class="">
		<win-blank-view @goScan='openScanPopup' v-if="itemList.length==0"></win-blank-view>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scrollView" style="padding-bottom: 150px"
			@scrolltoupper="upper" @scrolltolower="lower" @scroll="scroll">
			<view class="detail-list" v-for="(item,index) in itemList">
				<view class="detail-content">
					<uni-swipe-action>
						<uni-swipe-action-item :right-options="options" :auto-close="false"
							@click="swipeClick($event,index)">
							<com-unissue :dataContent="item" @qtyChanged='qtyChanged'></com-unissue>
						</uni-swipe-action-item>
					</uni-swipe-action>
				</view>
			</view>
		</scroll-view>

		<view class="new_bot_box" v-if="itemList.length>0">
			<uni-collapse>
				<uni-collapse-item :show-animation="true" title="点击添加其他原因">
					<view class="content">
						<view class="tit_bot_box">原因</view>
						<uni-data-picker class='uni-data-picker' placeholder="请选择原因" popup-title="出库原因"
							:localdata="issueReasonArray" v-model="issueReason" style="padding: 10rpx 20rpx;"
							placeholderStyle="padding-left:10rpx;">
						</uni-data-picker>
						<view class="tit_bot_box">备注</view>
						<uni-easyinput type="textarea" v-model="remark" placeholder="请输入备注"
							style="padding: 10rpx 20rpx; ">
						</uni-easyinput>
					</view>
				</uni-collapse-item>
			</uni-collapse>
			<view class="new_btn_bot bot_pos">
				<button :disabled="submitting" class="new_save_btn" @click="submit()">提交</button>
			</view>
		</view>

		<comMessage ref="comMessage"></comMessage>
		<win-scan-button @goScan='openScanPopup' v-if="itemList.length>0"></win-scan-button>
		<win-scan-by-pack ref="scanPopup" @getScanResult='getScanResult'></win-scan-by-pack>
	</view>
</template>

<script>
	import {
		balances,
		unplannedIssue,
	} from '@/api/index.js';
	import {
		getIssueReasonArray
	} from '@/common/array.js';

	import winBlankView from '@/mycomponents/wincom/winBlankView.vue'
	import winScanButton from '@/mycomponents/wincom/winScanButton.vue'
	import comUnissue from '@/mycomponents/coms/inventory/comUnissue.vue'
	import winScanByPack from '@/mycomponents/wincom/winScanByPack.vue'
	import comMessage from '@/mycomponents/common/comMessage.vue'
	import {
		showConfirmMsg,
		goHome,
		getRemoveOption
	} from '@/common/basic.js';
	export default {
		name: 'unissue',
		components: {
			winBlankView,
			winScanButton,
			comUnissue,
			winScanByPack,
			comMessage
		},
		data() {
			return {
				//滑动移除
				options: [],
				itemList: [],
				fromLocationCode: '',
				toLocationCode: '',
				scrollTop: 0,
				old: {
					scrollTop: 0
				},

				issueReason: '0',
				issueReasonArray: [],
				remark: '',
				currentItem: {},
				submitting: false
			};
		},
		created() {
			this.options = getRemoveOption();
			this.issueReasonArray = getIssueReasonArray();
		},
		onLoad() {},
		onNavigationBarButtonTap(e) {
			if (e.index === 0) {
				goHome();
			}
		},
		filters: {},
		methods: {
			openScanPopup() {
				this.$refs.scanPopup.openScanPopup();
			},
			//提示是否移除选择的行?
			swipeClick(e, index) {
				let {
					content
				} = e;
				if (content.text === '移除') {
					uni.showModal({
						title: '提示',
						content: '是否移除选择的行?',
						success: res => {
							if (res.confirm) {
								this.itemList.splice(index, 1);
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			},

			getScanResult(result) {
				let that = this;
				//判断是否重复扫描
				let datas = [];
				let params = {
					pageSize: 1000,
					pageIndex: 1,
				};
				if (result.data.isPack === true) {
					datas = that.itemList.filter(r => {
						return r.packingCode === result.data.packingCode && r.itemCode === result.data
							.itemCode && r.lot === result.data.lot
					})
					params.packingCode = result.data.packingCode;
					params.itemCode = result.data.itemCode;
				} else if (result.data.isPack === false) {
					datas = that.itemList.filter(r => {
						return r.containerCode == result.data.containerCode
					})
					params.containerCode = result.data.containerCode
				} else {
					this.showMessage('标签格式不正确')
					return;
				}
				if (datas.length > 0) {
					showConfirmMsg('零件已经存在,是否要重新扫描?', confirm => {
						if (confirm) {
							that.itemList.forEach((r, i) => {
								if (result.data.isPack === true) {
									if (r.packingCode == result.data.packingCode) {
										that.itemList.splice(i, 1);
										return;
									}
								} else {
									if (r.containerCode == result.data.containerCode) {
										that.itemList.splice(i, 1);
									}
								}
							});
							that.getInventory(params);
						}
					});
				} else {
					that.getInventory(params);
				}
			},

			getInventory(params) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})

				balances(params).then((res) => {
					try {
						if (res.totalCount === 0) {
							this.showMessage('未查找到库存信息');
						} else {
							this.getItemList(res.items);
						}
					} catch (e) {
						this.showMessage(e.message)
					}
					uni.hideLoading();
				}).catch((err) => {
					uni.hideLoading();
					this.showMessage(err.message);
				})
			},

			getItemList(items) {
				console.log('items', items);
				let that = this;
				items.forEach(r => {
					r.handelQty = {
						qty: r.qty.qty,
						uom: r.qty.uom
					}
				});
				that.itemList = [...that.itemList, ...items];
			},

			qtyChanged(qtyValue, item) {
				let balanceQty = item.qty.qty;
				if (qtyValue < 0) {
					this.showMessage('出库数量不能小于0');
				} else {
					if (qtyValue > balanceQty) {
						this.showMessage('出库数量[' + qtyValue + ']不能大于库存数量[' + balanceQty + ']');
						this.$nextTick(() => {
							item.handelQty.qty = balanceQty;
						})
					}
				}
			},

			submit() {
				let that = this;

				if (that.itemList.length === 0) {
					this.showMessage('请扫描箱标签');
					return;
				}

				if (that.issueReason === '') {
					this.showMessage('请先选择库存原因');
					return;
				}

				that.submitting = true;
				uni.showLoading({
					title: "提交中...",
					mask: true
				});
				let item = {
					worker: localStorage.userName,
					warehouseCode: localStorage.warehouseCode,
					jobNumber: "",
					supplierCode: "",
					company: localStorage.company,
					number: "",
					remark: that.remark,
					details: []
				}
				that.itemList.forEach(r => {
					let detail = {
						itemCode: r.itemCode,
						item: r.item,
						lot: r.lot,
						batch: r.batch,
						packingCode: r.packingCode,
						containerCode: r.containerCode,
						warehouseCode: localStorage.warehouseCode,
						qty: r.handelQty,
						status: r.status,
						locationCode: r.locationCode,
						reasonCode: that.issueReason,
						worker: localStorage.userName,
					}
					item.details.push(detail);
				})

				let params = JSON.stringify(item);
				console.log('params', params);
				unplannedIssue(params)
					.then(res => {
						that.showMessage('提交成功');
						that.clearInfo();
						that.hideLoading();
					})
					.catch(err => {
						that.showMessage(err.message);
						that.hideLoading();
					});
			},
			hideLoading() {
				this.submitting = false;
				uni.hideLoading();
			},
			clearInfo() {
				let that = this;
				that.itemList = [];
				that.issueReason = that.issueReason;
				that.remark = '';
			},
			showMessage(message) {
				this.$refs.comMessage.showMessage(message);
			}
		}
	};
</script>

<style scoped lang="scss">
	/deep/ .input-value {
		font-size: 16px;
	}
</style>
