<template>
	<view class="zd_container">
		<!-- <nav-bar title="空木箱出库"></nav-bar> -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">{{$t('filter.box-no')}}</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val1" @handleChange="handleChange1" />
					</view>
				</view>
				<view v-if="editValue.length" class="msg_item" style="margin-bottom:16rpx;">已扫木箱号：{{editValue.toString()}}</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">{{$t('filter.select-box-num')}}</span>
					</view>
					<view class="filter_input_wraper">
						<view class="filter_input" style="font-size: 34rpx; color: #ff6a00;text-align: center;">{{selectedNum}}</view>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">{{$t('filter.selectable-box-num')}}</span>
					</view>
					<view class="filter_input_wraper">
						<view class="filter_input" style="font-size: 34rpx; color: #ff6a00;text-align: center;">{{selectedNum}}</view>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>{{$t('grid.select')}}</th>
								<th>{{$t('filter.box-no')}}</th>
								<th>{{$t('filter.box-type')}}</th>
								<th>{{$t('filter.wooden-box-description')}}</th>
								<th>{{$t('filter.max-sub-num')}}</th>
								<th>{{$t('grid.wooden-box-length')}}</th>
								<th>{{$t('grid.wooden-box-width')}}</th>
								<th>{{$t('grid.wooden-box-height')}}</th>
								<th>{{$t('filter.vehicle-type')}}</th>
								<th>{{$t('grid.wooden-box-weight')}}</th>
								<th>{{$t('filter.storage-location')}}</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" :class="{'checked': e.checked}" @tap="toCheck(e)">
								<td><span class="iconfont icon_unchecked" :class="{'icon_checked': e.checked}">&#xe66b;</span></td>
								<td>{{e.box_no}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.num}}</td>
								<td>{{e.box_length}}</td>
								<td>{{e.box_width}}</td>
								<td>{{e.box_high}}</td>
								<td>{{e.vehicle_type}}</td>
								<td>{{e.box_weight}}</td>
								<td>{{e.struct_code}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-6 btn-submit btn-default" @tap="clearUp">{{$t('button.clear')}}</button>
			<button class="zd-col-15 btn-submit btn-success" :class="{'btn-info': !checkArr.length}" :disabled="disabled" @tap="_twoBoxOut">{{$t('button.outstore-confirm')}}</button>
		</view>
		<up-top ref="UT" :scrollTop="top"></up-top>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import UpTop from '@/components/upTop.vue'
	import { confirmAction } from '@/utils/utils.js'
	import {twoQueryBoxIvt, twoBoxOut} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			UpTop
		},
		data() {
			return {
				title: '',
				top: 0,
				val1: '',
				editValue: [], // 扫描木箱号集合
				selectedNum: null, // 已选木箱数
				dataList: [],
				checkArr: [],
				disabled: false
			};
		},
		onPageScroll(e) {
			this.$refs.UT.topData(e.scrollTop)
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleChange1 (e) {
				if (e) {
					this._twoQueryBoxIvt(e)
				}
			},
			async _twoQueryBoxIvt (e) {
				try {
					let res = await twoQueryBoxIvt(e)
					if (res && res.data.length) {
						res.data.map(el => {
							this.$set(el, 'checked', false)
						})
						this.dataList = [...this.dataList, ...res.data]
						this.editValue.push(this.val1)
					}
				} catch (e) {
					this.val1 = ''
				}
			},
			toCheck (e) {
				e.checked = !e.checked
				this.checkArr = this.dataList.filter(i => { return i.checked === true })
				this.selectedNum = this.checkArr.length
				if (this.selectedNum > 10) {
					e.checked = false
					this.checkArr = this.dataList.filter(i => { return i.checked === true })
					this.selectedNum = this.checkArr.length
				}
			},
			async handleConfirm() {
				if (!this.checkArr.length) {
					return
				}
				const isConfirmed = await confirmAction(this.$t('toast.prompt'), this.$t('toast.sure-perform-operation'))
				if (isConfirmed) {
					this._twoPdaVehicleIn()
				}
			},
			async _twoBoxOut () {
				this.disabled = true
				try {
					let res = await twoBoxOut(this.checkArr)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.val1 = ''
				this.editValue = []
				this.selectedNum = null
				this.checkArr = []
				this.dataList = []
				this.disabled = false
			}
		}
	}
</script>