<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-9">
						<span class="filter_label">入库单类型</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">单据编码</span>
					</view>
					<view class="zd-col-12">
						<search-box v-model="code" @handleChange="handleChange1" @handleDel="handleDel1" />
					</view>
					<button class="mini-btn" type="primary" size="mini" @tap="toSearch">查询</button>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label filter_input_disabled">物料名称</span>
					</view>
					<view class="zd-col-18">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_name" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label filter_input_disabled">物料规格</span>
					</view>
					<view class="zd-col-18">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_spec" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label filter_input_disabled">单位</span>
					</view>
					<view class="zd-col-18">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.unit_name" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料批次</span>
					</view>
					<view class="zd-col-18">
						<input type="text" class="filter_input" v-model="currentData.pcsn">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料数量</span>
					</view>
					<view class="zd-col-18">
						<input type="number" class="filter_input" v-model="val1">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">载具编码</span>
					</view>
					<view class="zd-col-18">
						<search-box v-model="val2" @handleChange="handleChange2" @handleDel="handleDel2" />
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">仓库编码</span>
					</view>
					<view class="zd-col-18 filter_select">
						<!-- <search-box v-model="currentData.stor_code"/> -->
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</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': !val1 || !val1 || !index || !index1}" :disabled="disabled" @tap="_inStorageConfirm">组盘确认</button>
		</view>
		<view class="zd_content msg_wrapper" :class="show ? 'popshow' : 'pophide'">
			<view class="pop-line"></view>
			<view class="msg_content">
				<view class="zd_wrapper grid-wraper">
					<view class="slide_new">
						<table>
							<thead>
								<tr>
									<th>序号</th>
									<th>物料编码</th>
									<th>物料名称</th>
									<th>物料数量</th>
									<th>单位</th>
									<th>单据状态</th>
									<th>物料批次</th>
									
								</tr>
							</thead>
							<tbody>
								<tr v-for="(e, i) in dataList" :key="i" :class="{'checked': e.material_code === pkId}" @tap="toCheck(e)">
									<td>{{i+1}}</td>
									<td>{{e.material_code}}</td>
									<td>{{e.material_name}}</td>
									<td>{{e.qty}}</td>
									<td>{{e.unit_name}}</td>
									<td>{{e.bill_status}}</td>
									<td>{{e.pcsn}}</td>
									
								</tr>
							</tbody>
						</table>
					</view>
				</view>
			</view>
			<view class="zd-row submit-bar">
				<button class="zd-col-11 button-default" @tap.stop="show = false">关闭</button>
				<button class="zd-col-11 button-primary" :disabled="disabled" @tap="confirmModal">确定</button>
			</view>
		</view>
		<view v-if="show" class="msg_mask"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getStructCount, inStorageOrder, getBillNoInfo, storList, inStorageConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				code: '',
				show: false,
				dataList: [],
				pkId: '',
				pkObj: {},
				currentData: {},
				val1: null,
				val2: null,
				options: [],
				index: '',
				disabled: false,
				options1: [],
				index1: '',
				kwData: {}
			};
		},
		onLoad (options) {
			this.title = options.title
			this._inStorageOrder()
			this._storList()
		},
		methods: {
			/** 下拉框*/
			async _inStorageOrder () {
				let res = await inStorageOrder()
				this.options1 = [...res]
			},
			selectChange1 (e) {
				this.index1 = e
			},
			handleChange1 (e) {
				if (e) {
					this._getBillNoInfo(e)
				}
			},
			handleDel1 () {
				this.pkId = ''
				this.currentData = {}
				this.index = ''
				this.val1 = ''
				this.val2 = ''
			},
			toSearch () {
				if (this.code) {
					this._getBillNoInfo(this.code)
				} else {
					uni.showToast({
						title: '请扫码',
						icon: 'none'
					})
				}
			},
			async _getBillNoInfo (e) {
				try {
					let res = await getBillNoInfo(e, this.index1)
					if (res && res.length) {
						this.dataList = [...res]
						this.pkId = ''
						this.pkObj = {}
						this.currentData = {}
						this.index = ''
						this.val1 = null
						this.val2 = null
						this.show = true
					}
				} catch (e) {}
			},
			toCheck (e) {
				if (e.status === '80') {
					return
				}
				this.pkId = this.pkId === e.material_code ? '' : e.material_code
				this.pkObj = this.pkId === e.material_code ? e : {}
			},
			confirmModal () {
				if (!this.pkId) {
					uni.showToast({
						title: '请选择一行',
						icon: 'none'
					})
					return
				}
				this.show = false
				this.currentData = this.pkObj
				this.val1 = this.currentData.qty
				this.val2 = this.currentData.vehicle_code
				if(this.currentData.stor_code !== '' && this.currentData.stor_code !== null && this.currentData.stor_code !== undefined) {
					this.options.map(el => {
						if (el.value === this.currentData.stor_code) {
							this.index = el.value
						}
					})
				} else {
					if (this.val2.charAt(0).toUpperCase() === 'T') {
						this.index ='FStockPallet'
					} else {
						this.index ='FStockId'
					}
				}
			},
			selectChange (e) {
				this.index = e
			},
			async _storList () {
				let res = await storList()
				if (res.code === '200') {
					this.options = [...res.content]
					this.options.map(el => {
						this.$set(el, 'text', el.label)
					})
				}
			},
			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.code = ''
				this.pkId = ''
				this.currentData = {}
				this.index = ''
				this.val1 = null
				this.val2 = null
				this.index1 = ''
				this.disabled = false
				this.kwData = {}
			},
			async _inStorageConfirm () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.index || !this.index1) {
					this.disabled = false
					return
				}
				try {
					let obj = Object.assign(this.currentData, {qty: this.val1, vehicle_code: this.val2, stor_code: this.index, form_type: this.index1})
					let res = await inStorageConfirm(obj)
					if (res.code === '200') {
						this.index = ''
						this.val1 = null
						this.val2 = null
					}
					this.disabled = false
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
.msg_wrapper
	height: calc(100% - var(--status-bar-height) - 72rpx)
</style>
