<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">点位</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val1"  @handleChange="handleChange" />
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">托盘号</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val2" />
					</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>更新时间</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.point_code1}}</td>
								<td>{{e.point_code2}}</td>
								<td>{{options1 | findByValue(e.vehicle_type)}}</td>
								<td>{{options2 | findByValue(e.task_status)}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.create_time}}</td>
								<td>{{e.update_time}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-5 btn-submit btn-default" @tap="clearUp">清空</button>
			<button class="zd-col-5 btn-submit btn-success" @tap="_taskTubeVehicle">刷新数据</button>
			<button class="zd-col-6 btn-submit btn-success" :class="{'btn-info': !val1 || !val2}" :disabled="disabled" @tap="_emptyTubeVehicleOut">托盘移出</button>
			<button class="zd-col-6 btn-submit btn-success" :class="{'btn-info': !val1 || !val2}" :disabled="disabled" @tap="_fullTubeVehicleIn">备货入库</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getTubeVehicleInfo, taskTubeVehicle, fullTubeVehicleIn, emptyTubeVehicleOut} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				dataList: [],
				disabled: false,
				options1: [{value: '010823', text: '备货区托盘入库'}, {value: '010824', text: '备货区托盘出库'}],
				options2: [{value: '04', text: '起点终点确认'}, {value: '05', text: '下发'}, {value: '06', text: '执行中'}, {value: '071', text: '取货完成，执行中'}, {value: '07', text: '完成'}]
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._taskTubeVehicle()
		},
		methods: {
			handleChange (e) {
				if (e) {
					this._getTubeVehicleInfo(e)
				}
			},
			async _getTubeVehicleInfo (e) {
				try {
					const res = getTubeVehicleInfo(e)
					if (res && res.status === 200) {
						this.val2 = res.data
					}
				} catch (e) {
					this.val2 = ''
				}
			},
			async _taskTubeVehicle () {
				this.dataList = []
				try {
					let res = await taskTubeVehicle()
					if (res && res.status === 200) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			async _emptyTubeVehicleOut () {
				this.disabled = true
				if (!this.val1 || !this.val2) {
					this.disabled = false
					return
				}
				try {
					let res = await emptyTubeVehicleOut(this.val1, this.val2)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
					this.disabled = false
					this._taskTubeVehicle()
				} catch (e) {
					this.disabled = false
				}
			},
			async _fullTubeVehicleIn () {
				this.disabled = true
				if (!this.val1 || !this.val2) {
					this.disabled = false
					return
				}
				try {
					let res = await fullTubeVehicleIn(this.val1, this.val2)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
					this.disabled = false
					this._taskTubeVehicle()
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
			}
		}
	}
</script>