<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.StartPosition')}}</view>
					<search-box
					v-model="val2"
					@handleChange="handleChange"
					/>
				</view>
				<view class="is-required">
					<view class="filter_label">{{$t('label.CarrierCode')}}</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" @tap="_getVehicleMaterial(val1)">{{$t('button.search')}}</button>
				</view>
				<view class="is-required">
					<view class="filter_label">{{$t('label.WarehouseArea')}}</view>
					<view class="filter_select">
						<uni-data-select :placeholder="$t('utils.selectPlaceholder')" :emptyTips="$t('utils.selectEmptyTips')" v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="is-required">
					<view class="filter_label">{{$t('label.WarehousingType')}}</view>
					<view class="filter_select">
						<uni-data-select :placeholder="$t('utils.selectPlaceholder')" :emptyTips="$t('utils.selectEmptyTips')" v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialQRCode')}}</view>
					<search-box
					v-model="qrcode"
					/>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialName')}}</view>
					<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_name" disabled>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialSpecification')}}</view>
					<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_spec" disabled>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialCode')}}</view>
					<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_code" disabled>
				</view>
				<view>
					<view class="filter_label">{{$t('label.MaterialQuantity')}}</view>
					<view class="zd-row">
						<view class="zd-col-20"><input type="text" class="filter_input filter_input_disabled" v-model="currentData.qty" disabled></view>
						<view class="zd-col-4 filter_unit">{{currentData.qty_unit_name}}</view>
					</view>
				</view>
			</view>
			<view v-if="dataList.length" class="zd_wrapper grid-wraper">
				<view v-if="dataList.length" class="slide_new">
					<table>
						<thead>
							<tr>
								<th width="60%">{{$t('th.MaterialBoxNumber')}}</th>
								<th width="40%">{{$t('th.Quantity')}}</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.mater_frame}}</td>
								<td>{{e.qty}}</td>
							</tr>
						</tbody>
					</table>
				</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 || !index || !index1}" :disabled="disabled" @tap="_confirmIn">{{$t('button.confirm')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getSect, getVehicleMaterial, confirmIn, dictDetail} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				options: [],
				index: '',
				options1: [],
				index1: '',
				qrcode: '',
				disabled: false,
				currentData: {},
				dataList: []
			};
		},
		onLoad (options) {
			this.title = options.title
			this._getSect()
			this._dictDetail()
		},
		methods: {
			async _getSect () {
				try {
					let res = await getSect()
					if (res) {
						this.options = [...res]
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			async _dictDetail () {
				try {
					let res = await dictDetail('ST_INV_IN_TYPE')
					if (res && res.code === '200') {
						this.options1 = [...res.content]
					} else {
						this.options1 =[]
					}
				} catch (e) {
					this.options1 = []
				}
			},
			selectChange (e) {
				this.index = e
			},
			selectChange1 (e) {
				this.index1 = e
			},
			handleChange (e) {
				if (e) {
					this._getVehicleMaterial(e)
				}
			},
			async _getVehicleMaterial (e) {
				try {
					let res = await getVehicleMaterial(e)
					if (res) {
						this.currentData = res
						this.dataList = res.dtlList
					} else {
						this.currentData = {}
						this.dataList = []
					}
				} catch (e) {
					this.currentData = {}
					this.dataList = []
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.index = ''
				this.index1 = ''
				this.qrcode = ''
				this.disabled = false
				this.currentData = {}
				this.dataList = []
			},
			async _confirmIn () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.index || !this.index1) {
					this.disabled = false
					return
				}
				try {
					let res = await confirmIn(this.val1, this.val2, this.index, this.index1, this.qrcode)
					if (res.status === '200') {
						this.clearUp()
					}
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
.slide_new
	table
		table-layout auto
		th,td
			&:first-child
				box-shadow: none
</style>
