<template>
	<view class="zd_container">
		<nav-bar title="半成品入库"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">物料规格</span>
					</view>
					<view class="filter_input_wraper">
						<input type="text" class="filter_input filter_input_disabled pointer" v-model="val1" @tap="getMater">
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">物料编码</span>
					</view>
					<view class="filter_input_wraper">
						<input type="text" class="filter_input filter_input_disabled" v-model="val2">
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">物料数量</span>
					</view>
					<view class="filter_input_wraper">
						<input type="number" class="filter_input" v-model="val3">
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">订单号</span>
					</view>
					<view class="filter_input_wraper">
						<input type="text" class="filter_input" v-model="sale_id">
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">工序</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">料箱码</span>
					</view>
					<view class="filter_input_wraper">
						<search-box
						v-model="bar_code"
						/>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">点位</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
			</view>
		</view>
		<view class="submit-bar">
			<button class="submit-button" :class="{'btn-disabled': !val1 || !val3 || !bar_code || !index2}" :disabled="disabled" @tap="toSure">确认入库</button>
			<button class="submit-button" @tap="toCancle">清空</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getWork, getPoint, BcpConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				val1: '',
				val2: '',
				val3: '',
				sale_id: '',
				options1: [],
				index1: '',
				options2: [],
				index2: '',
				bar_code: '',
				dataList: [],
				pkId: '',
				pkObj: {},
				disabled: false
			};
		},
		onShow() {
			if (this.$store.getters.publicObj) {
				this.val1 = this.$store.getters.publicObj.material_spec
				this.val2 = this.$store.getters.publicObj.material_code
				this.val3 = this.$store.getters.publicObj.net_weight
			}
		},
		destroyed () {
			this.$store.dispatch('setPublicObj', '')
		},
		created () {
			this._getWork()
			this._getPoint()
		},
		methods: {
			getMater () {
				uni.navigateTo({
					url: '/pages/warehouse/SemifinishedMaterSearch'
				})
			},
			/** 选择器1 */
			selectChange1(e) {
				this.index1 = e
			},
			/** 选择器2 */
			selectChange2(e) {
				this.index2 = e
			},
			/** 工序下拉框查询 */
			async _getWork () {
				let res = await getWork()
				res.data.map(el => {
					this.$set(el, 'value', el.workprocedure_id)
					this.$set(el, 'text', el.workprocedure_name)
				})
				this.options1 = [...res.data]
			},
			/** 点位下拉框查询 */
			async _getPoint () {
				let res = await getPoint()
				res.data.map(el => {
					this.$set(el, 'value', el.point_code)
					this.$set(el, 'text', el.point_name)
				})
				this.options2 = [...res.data]
			},
			/** 确认 */
			async toSure () {
				this.disabled = true
				if (!this.val1 || !this.val3 || !this.bar_code || !this.index2 ) {
					this.disabled = false
					return
				}
				try {
					let from = {
						material_spec: this.val1,
						material_code: this.val2,
						qty: this.val3,
						sale_id: this.sale_id,
						workprocedure_id: this.index1,
						bar_code: this.bar_code,
						point_code: this.index2
					}
					let res = await BcpConfirm(from)
					this.disabled = false
					this.toCancle()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			toCancle () {
				this.val1 = ''
				this.val2 = ''
				this.val3 = ''
				this.sale_id = ''
				this.index1 = ''
				this.bar_code = ''
				this.index2 = ''
				this.$store.dispatch('setPublicObj', '')
			}
		}
	}
</script>

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