<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-7">
						<span class="filter_label">物料编码</span>
					</view>
					<view class="zd-col-17">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">管芯规格</span>
					</view>
					<view class="zd-col-17 filter_select">
						<zxz-uni-data-select v-model="index" filterable :localdata="options"></zxz-uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-5">
						<span class="filter_label">管芯数量</span>
					</view>
					<view class="zd-col-19">
						<input v-model=" val2" type="number" class="filter_input" @blur="handleBlur">
					</view>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-7 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-15 button-primary" :class="{'button-info': !val1 || !val2 || index === ''}" :disabled="disabled" @tap="_gxconfirmedInStorage">确认入库</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {gxgetGxSpecification, gxremainingQty, gxconfirmedInStorage} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				index: '',
				options: [],
				val1: '',
				val2: '',
				qty: '',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this._gxgetGxSpecification()
		},
		methods: {
			async _gxgetGxSpecification () {
				let res = await gxgetGxSpecification()
				this.options = [...res]
			},
			async _gxremainingQty () {
				let res = await gxremainingQty()
				this.qty = res.qty
				if (Number(this.val2) > Number(this.qty)) {
					uni.showToast({
						title: '管芯库空位不足！',
						icon: 'none'
					})
					this.val2 = ''
				}
			},
			handleBlur () {
				if (this.val2 !== '') {
					this._gxremainingQty()
				} else if (Number(this.val2) > 5) {
					uni.showToast({
						title: '一次性最多只能入5根管芯！',
						icon: 'none'
					})
					this.val2 = ''
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.index = ''
				this.qty = ''
				this.disabled = false
			},
			async _gxconfirmedInStorage () {
				this.disabled = true
				if (!this.val1 || !this.val2 || this.index === '') {
					this.disabled = false
					return
				}
				try {
					let res = await gxconfirmedInStorage(this.val2, this.val1, this.index)
					this.clearUp()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

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