<template>
	<view class="zd_container">
		<!-- 合格证入库 -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-8">
						<span class="filter_label">合格证编码</span>
					</view>
					<view class="zd-col-16">
						<search-box
						v-model="val1"
						@handleChange="handleChange1"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-8">
						<span class="filter_label">载具编码</span>
					</view>
					<view class="zd-col-16">
						<search-box
						v-model="val2"
						 @handleChange="handleChange2"
						 @handleDel="handleDel2"
						/>
					</view>
				</view>
			</view>
			<view class="zd_wrapper">
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">订单号</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.moNumber}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">源单信息</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.srcBillNo}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">物料编码</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.material_code}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">物料名称</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.material_name}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">物料规格</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.material_spec}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-8"><span class="filter_label">单位</span></view>
					<view class="zd-col-16"><span class="filter_input">{{currentData.unit_name}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8"><span class="filter_label">单重</span></view>
					<view class="zd-col-16">
						<input type="number" class="filter_input" v-model="currentData.single_weight">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8"><span class="filter_label">批次</span></view>
					<view class="zd-col-16">
						<input type="text" class="filter_input" v-model="currentData.pcsn">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8"><span class="filter_label">物料数量</span></view>
					<view class="zd-col-16">
						<input type="number" class="filter_input" v-model="val3">
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-8"><span class="filter_label">仓库编码</span></view>
					<view class="zd-col-16 filter_select">
						<uni-data-select v-model="index" :localdata="options"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="filter_label">当前库位可用数：</view>
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-4">
						<span class="filter_label" style="font-size: 24rpx;">高库位</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_input" style="font-size: 28rpx; color: #ff6a00; font-weight: 700;">{{kwData.g}}个</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_label" style="font-size: 24rpx;">中库位</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_input" style="font-size: 28rpx; color: #ff6a00; font-weight: 700;">{{kwData.z}}个</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_label" style="font-size: 24rpx;">低库位</span>
					</view>
					<view class="zd-col-4">
						<span class="filter_input" style="font-size: 28rpx; color: #ff6a00; font-weight: 700;">{{kwData.d}}个</span>
					</view>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="toEmpty">清空</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': !val2 || !val3 || !index}" :disabled="disabled" @tap="_inStorageConfirm">组盘确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getStructCount, storList, getCertificateInfo, inStorageConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				val3: '',
				currentData: {},
				options: [],
				index: '',
				disabled: false,
				kwData: {}
			};
		},
		onLoad (options) {
			this.title = options.title
			this._storList()
		},
		methods: {
			async _storList () {
				let res = await storList()
				if (res.code === '200') {
					this.options = [...res.content]
					this.options.map(el => {
						this.$set(el, 'text', el.label)
					})
				}
			},
			handleChange1 (e) {
				this._getCertificateInfo(e)
			},
			async _getCertificateInfo (e) {
				let res = await getCertificateInfo(e)
				this.currentData = res
				if (JSON.stringify(this.currentData) !== '{}') {
					this.val3 = this.currentData.qty
				}
			},
			handleChange2 (e) {
				if (e) {
					if (e.charAt(0).toUpperCase() === 'T') {
						this.index ='FStockPallet'
					} else {
						this.index ='FStockId'
					}
					this._getStructCount(e)
				}
			},
			async _getStructCount (e) {
				try {
					let res = await getStructCount(e)
					if (res) {
						this.kwData = res
					} else {
						this.kwData = {}
					}
				} catch (e) {
					this.kwData = {}
				}
			},
			handleDel2 () {
				this.index = ''
			},
			toEmpty () {
				this.val1 = ''
				this.val2 = ''
				this.val3 = ''
				this.currentData = {}
				this.index = ''
				this.disabled = false
				this.kwData = {}
			},
			async _inStorageConfirm () {
				this.disabled = true
				if (!this.val2 || !this.val3 || !this.index) {
					this.disabled = false
					return
				}
				try {
					let obj = Object.assign(this.currentData, {vehicle_code: this.val2, stor_code: this.index, qty: this.val3, bar_code: this.val1})
					let res = await inStorageConfirm(obj)
					if (res.code === '200') {
						this.val3 = ''
					}
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">
</style>
