<template>
	<view class="zd_container">
		<!-- 出库分拣 -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="is-required">
					<view class="filter_label">{{$t('label.ReturnwarehousePalletCode')}}</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-18">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
					<button class="mini-btn" type="primary" size="mini" style="margin-right: 0;margin-left: 10rpx;" @tap="_getOutAndReturnNumByReturnVehicleCode(val1)">{{$t('button.search')}}</button>
				</view>
				<view>
					<view class="filter_label">{{$t('label.OutboundPalletCode')}}</view>
					<search-box
					v-model="val2"
					/>
				</view>
				<view>
					<view class="filter_label">{{$t('label.OutboundPalletStorageLocation')}}</view>
					<search-box
					v-model="val3"
					/>
				</view>
				<view>
					<view class="filter_label">{{$t('label.QtyReturnedWarehouse')}}</view>
					<input type="text" class="filter_input filter_input_disabled" v-model="currentData.return_num" disabled>
				</view>
				<view>
					<view class="filter_label">{{$t('label.OutboundQty')}}</view>
					<input type="text" class="filter_input filter_input_disabled" v-model="currentData.out_num" disabled>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">{{$t('button.clear')}}</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': !val1 || !val2 || !val3 || JSON.stringify(currentData) === '{}'}" :disabled="disabled" @tap="toSure">{{$t('button.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getOutAndReturnNumByReturnVehicleCode, outSortingSubmit} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				val3: '',
				currentData: {},
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleChange (e) {
				if (e) {
					this._getOutAndReturnNumByReturnVehicleCode(e)
				}
			},
			async _getOutAndReturnNumByReturnVehicleCode (e) {
				try {
					let res = await getOutAndReturnNumByReturnVehicleCode(e)
					if (res && res.status === '200') {
						this.currentData = res.data
					} else {
						this.currentData = {}
					}
				} catch (e) {
					this.currentData = {}
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.val3 = ''
				this.currentData = {}
				this.disabled = false
			},
			toSure () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.val3 || JSON.stringify(this.currentData) === '{}') {
					this.disabled = false
					return
				}
				uni.showModal({
					title: this.$t('common.tip'),
					content: this.$t('modal.checkSorting', {
						location: this.val3,
						returnNum: this.currentData.return_num,
						outNum: this.currentData.out_num
					}),
					success: (res) => {
						if (res.confirm) {
							this._outSortingSubmit()
						} else if (res.cancel) {
							this.disabled = false
						}
					}
				});
			},
			async _outSortingSubmit () {
				try {
					let res = await outSortingSubmit(this.val1, this.val2, this.val3)
					if (res.status === '200') {
						this.clearUp()
					}
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>