<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
						v-model="val1"
						/>
					</view>
				</view>
				<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
						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" @click="toCheck(e)" :class="{'checked': e.task_id === pkId}">
								<td>{{e.task_code}}</td>
								<td>{{e.priority}}</td>
								<td>{{e.point_code1}}</td>
								<td>{{e.point_code2}}</td>
								<td>{{e.inst_status}}</td>
								<td>{{e.car_no}}</td>
								<td>{{e.create_time}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-primary" @tap="searchList">查询</button>
			<button class="zd-col-6 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="toSure('1')">取消</button>
			<button class="zd-col-6 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="toSure('2')">完成</button>
			<button class="zd-col-6 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="toSure('3')">创建指令</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {tasks, taskoperation} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				dataList: [],
				pkId: '',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			searchList () {
				this.dataList = []
				this._tasks()
			},
			async _tasks () {
				try {
					let res = await tasks(this.val1, this.val2)
					if (res && res.data) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
				} catch (e) {
					this.dataList = []
				}
			},
			toCheck (e) {
				this.pkId = e.task_id === this.pkId ? '' : e.task_id
			},
			async toSure (status) {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					let res = await taskoperation(status, this.pkId)
					if (res) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.toEmpty()
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>