<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-5">
						<span class="filter_label">关键字</span>
					</view>
					<view class="zd-col-14 filter_select">
						<input type="text" placeholder="输入任务号、载具号、点位号" class="filter_input" v-model="keyWord">
					</view>
					<button type="primary" size="mini" style="margin-right: 0" @tap="_queryTask">查询</button>
				</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>
							</tr>
						</thead>
						<tbody>
							<tr v-for="e in dataList" :key="e.task_code" :class="{'checked': e.task_code === pkId}" @tap="toCheck(e)">
								<td>{{e.task_code}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.point_code1}}</td>
								<td>{{e.point_code2}}</td>
								<td>{{e.task_status}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_againTask">重新下发</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_forceConfirm">强制确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryTask, againTask, forceConfirm} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				keyWord: '',
				dataList: [],
				pkId: '',
				pkObj: {},
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			async _queryTask () {
				try {
					let res = await queryTask(this.keyWord)
					if (res) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []	
					}
				} catch (e) {
					this.dataList = []
				}
			},
			toCheck (e) {
				this.pkId = this.pkId === e.task_code ? '' : e.task_code
				this.pkObj = this.pkId === e.task_code ? e : {}
			},
			clearUp () {
				this.keyWord = ''
				this.dataList = []
				this.pkId = ''
				this.disabled = false
			},
			async _againTask () {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					const { task_code, vehicle_code, point_code1, point_code2, task_status } = this.pkObj
					const obj = { task_code, vehicle_code, point_code1, point_code2, task_status }
					let res = await againTask(obj)
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this._queryTask()
				} catch (e) {
					this.disabled = false
				}
			},
			async _forceConfirm () {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					const { task_code, vehicle_code, point_code1, point_code2, task_status } = this.pkObj
					const obj = { task_code, vehicle_code, point_code1, point_code2, task_status }
					let res = await forceConfirm(obj)
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this._queryTask()
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">

</style>
