<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-6">
						<span class="filter_label">关键字</span>
					</view>
					<view class="zd-col-13">
						<search-box v-model="keyword"/>
					</view>
					<button class="mini-btn" type="primary" @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>
								<th>作业类型</th>
								<th>设备号</th>
								<th>备注</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @click="toCheck(e)" :class="{'checked': e.task_id === pkId}">
								<td>{{e.vehicle_code}}</td>
								<td>{{e.point_code1}}</td>
								<td>{{e.point_code2}}</td>
								<td>{{['生成', '', '', '下发', '执行中', '完成'][Number(e.task_status)]}}</td>
								<td>{{e.task_code}}</td>
								<td>{{e.config_name}}</td>
								<td>{{e.car_no}}</td>
								<td>{{e.remark}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-8 button-primary" :class="{'button-info': !pkId}" :disabled="disabled1" @tap="_taskOperation">重新生成</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !pkId}" :disabled="disabled2" @tap="_forceConfirmTask">强制完成</button>
			<button class="zd-col-6 button-primary" @tap="toJump('operation?title=历史查询')">历史查询</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryTask, taskOperation, forceConfirmTask} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				keyword: '',
				dataList: [],
				disabled1: false,
				disabled2: false,
				pkId: '',
				pkObj: {}
			};
		},
		onLoad (options) {
			this.title = options.title
			this._queryTask()
		},
		methods: {
			toJump (name) {
				uni.navigateTo({
					url: `/pages/General/${name}`
				})
			},
			async _queryTask () {
				try {
					let res = await queryTask(this.keyword)
					if (res && res.data) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				} catch (e) {
					this.dataList = []
				}
			},
			async _taskOperation () {
				this.disabled1 = true
				if (!this.pkId) {
					this.disabled1 = false
					return
				}
				try {
					let res = await taskOperation(this.pkObj)
					if (res) {
						this._queryTask()
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.disabled1 = false
					this.disabled2 = false
					this.pkId = ''
					this.pkObj = {}
				} catch (err) {
					this.disabled1 = false
					this.disabled2 = false
					this.pkId = ''
					this.pkObj = {}
				}
			},
			async _forceConfirmTask () {
				this.disabled2 = true
				if (!this.pkId) {
					this.disabled2 = false
					return
				}
				try {
					let res = await forceConfirmTask(this.pkObj)
					if (res) {
						this._queryTask()
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.disabled1 = false
					this.disabled2 = false
					this.pkId = ''
					this.pkObj = {}
				} catch (err) {
					this.disabled1 = false
					this.disabled2 = false
					this.pkId = ''
					this.pkObj = {}
				}
			},
			toCheck (e) {
				this.pkId = this.pkId === e.task_id ? '' : e.task_id
				this.pkObj = this.pkId === e.task_id ? e : {}
			}
		}
	}
</script>

<style lang="stylus">
</style>
