<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-24 filter_select">
						<search-box
						placeholder="扫码或输入点位/载具号"
						v-model="val1"
						@handleChange="handleChange"
						@handleDel="handleDel"
						/>
					</view>
				</view>
			</view>
			<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>重量(kg)</th>
								<th>生产日期</th>
								<th>供应商编码</th>
								<th>供应商名称</th>
								<th>烘干次数</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{i+1}}</td>
								<td>{{e.struct_code}}</td>
								<td>{{e.storagevehicle_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.produce_time}}</td>
								<td>{{e.supp_code}}</td>
								<td>{{e.supp_name}}</td>
								<td>{{e.bake_num}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<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-24 filter_select">
						<search-box
						placeholder="扫码或输入点位/载具号"
						v-model="val2"
						@handleChange="handleChange2"
						@handleDel="handleDel2"
						/>
					</view>
				</view>
			</view>
			<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>重量(kg)</th>
								<th>生产日期</th>
								<th>供应商编码</th>
								<th>供应商名称</th>
								<th>烘干次数</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList2" :key="i">
								<td>{{i+1}}</td>
								<td>{{e.struct_code}}</td>
								<td>{{e.storagevehicle_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.produce_time}}</td>
								<td>{{e.supp_code}}</td>
								<td>{{e.supp_name}}</td>
								<td>{{e.bake_num}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-24 button-primary" :class="{'button-info': !dataList.length || !dataList2.length}" :disabled="disabled" @tap="_materialBoxMoveConfirm">确认移库</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getMoveDocumentInfo, materialBoxMoveConfirm} from '@/utils/getData1.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				dataList: [],
				dataList2: [],
				mdid: '',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleDel() {
				this.dataList = []
			},
			handleDel2() {
				this.dataList2 = []
			},
			handleChange (e) {
				if (e) {
					this.dataList = []
					this._getMoveDocumentInfo()
				}
			},
			handleChange2 (e) {
				if (e && !this.mdid && this.dataList.length) {
					this.dataList2 = []
					this._getMoveDocumentInfo2()
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.dataList = []
				this.dataList2 = []
				this.disabled = false
			},
			async _getMoveDocumentInfo () {
				try {
					let res = await getMoveDocumentInfo('1', this.val1, this.mdid)
					if (res && res.data.length > 0) {
						this.dataList = [...res.data]
						this.mdid = this.dataList[0].moveinvdtl_id
						if (this.dataList.length && this.mdid) {
							this._getMoveDocumentInfo2()
						}
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			async _getMoveDocumentInfo2 () {
				try {
					let res = await getMoveDocumentInfo('2', this.val2,  this.mdid)
					if (res && res.data.length > 0) {
						this.dataList2 = [...res.data]
					} else {
						this.dataList2 = []
					}
				} catch (e) {
					this.dataList2 = []
				}
			},
			async _materialBoxMoveConfirm () {
				this.disabled = true
				if (!this.dataList.length || !this.dataList2.length) {
					this.disabled = false
					return
				}
				try {
					let res = await materialBoxMoveConfirm(this.dataList, this.dataList2)
					if (res) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					}
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>