<template>
	<view class="content">
		<nav-bar title="成品入库"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="search-wrap">
				<view class="search-item">
					<label class="search-label">入库点</label>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">载具号</label>
					<view class="filter_input_wraper">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
			</view>
			<view class="confirm-button-wrap">
				<button class="confirm-button" @tap="toDelect">清空</button>
				<button class="confirm-button" @tap="toSelect">选择订单</button>
				<button class="confirm-button" :class="{'confirm-button_disabled': !index1 || !val1 || dataList.length === 0}" :disabled="disabled" @tap="toSure">入库确认</button>
			</view>
		</view>
		<view class="grid-wrap">
			<table class="grid-table">
				<thead>
					<tr>
						<th>订单行号</th>
						<th>规格</th>
						<th>入库数量</th>
						<th>物料编码</th>
						<th>物料名称</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="(e, i) in dataList" :key="i">
						<td>{{e.order_line_code}}</td>
						<td>{{e.material_spec}}</td>
						<td>{{e.in_qty}}</td>
						<td>{{e.material_code}}</td>
						<td>{{e.material_name}}</td>
					</tr>
				</tbody>
			</table>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getCacheLine, InstoreOrder} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				options1: [{text: 'A1', value: 'A1'}, {text: 'A2', value: 'A2'}],
				index1: 'A1',
				val1: '',
				dataList: [],
				pkId: '',
				disabled: false
			};
		},
		onShow() {
			if (this.$store.getters.publicArr) {
				this.dataList = this.$store.getters.publicArr
			}
		},
		destroyed () {
			this.$store.dispatch('setPublicArr', '')
		},
		methods: {
			/** 选择器1 */
			selectChange1(e) {
				this.index1 = e
			},
			toDelect () {
			  this.dataList = []
			  this.pkId = ''
			  this.$store.dispatch('setPublicArr', '')
			},
			async _inOutExceptionInstQuery () {
				let res = await inOutExceptionInstQuery(this.index2)
				this.dataList = [...res]
			},
			async toSure () {
			  this.disabled = true
			  if (!this.index1) {
				uni.showToast({
					title: '请选择入库点',
					icon: 'none'
				})
				this.disabled = false
				return
			  }
			  if (!this.val1) {
				uni.showToast({
					title: '请输入载具号',
					icon: 'none'
				})
				this.disabled = false
				return  
			  }
			  if (this.dataList.length === 0) {
				uni.showToast({
					title: '请选择订单信息',
					icon: 'none'
				})
				this.disabled = false
				return  
			  }
			  try {
				let res = await InstoreOrder(this.index1, this.val1, this.dataList) 
				this.disabled = false
				this.index1 = ''
				this.val1 = ''
				this.toDelect()
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled = false
			  }
			},
			toSelect () {
				uni.navigateTo({
					url: '/pages/management/MaterList'
				})
			}
		}
	}
</script>
