<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"
						/>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>任务号</th>
								<th>物料编码</th>
								<th>载具1</th>
								<th>载具2</th>
								<th>取货点1</th>
								<th>放货点1</th>
								<th>取货点2</th>
								<th>放货点2</th>
								<th>状态</th>
								<th>任务类型</th>
								<th>AGV</th>
								<th>生成时间</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @click="toCheck(e)" :class="{'checked': e.task_code === pkId}">
								<td>{{e.task_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.vehicle_code2}}</td>
								<td>{{e.point_code1}}</td>
								<td>{{e.point_code2}}</td>
								<td>{{e.point_code3}}</td>
								<td>{{e.point_code4}}</td>
								<td>{{e.task_status}}</td>
								<td>{{e.task_type}}</td>
								<td>{{e.car_no}}</td>
								<td>{{e.create_time}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-8 btn-submit btn-success" :class="{'btn-info': !pkId}" :disabled="disabled2" @tap="_taskConfirm">强制完成</button>
			<button class="zd-col-8 btn-submit btn-success" :class="{'btn-info': !pkId}" :disabled="disabled1" @tap="_againLssued">重新下发</button>
			<button class="zd-col-5 btn-submit btn-success letter-30" @tap="_taskTaskQuery">查询</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {taskTaskQuery, againLssued, taskConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				dataList: [],
				disabled1: false,
				disabled2: false,
				pkId: '',
				pkObj: {}
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			/** 初始化查询 */
			async _taskTaskQuery () {
				let res = await taskTaskQuery(this.val1)
				this.dataList = [...res.data]	
			},
			toCheck (e) {
				this.pkId = this.pkId === e.task_code ? '' : e.task_code
				this.pkObj = this.pkId === e.task_code ? e : {}
			},
			/** 重新下发 */
			async _againLssued () {
				this.disabled1 = true
				if (!this.pkId) {
					this.disabled1 = false
					return
				}
				try {
					let res = await againLssued(this.pkObj)
					this.disabled1 = false
					this.pkId = ''
					this.toClear()
					this._taskTaskQuery()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled1 = false
				}
			},
			/** 强制完成 */
			async _taskConfirm () {
				this.disabled2 = true
				if (!this.pkId) {
					this.disabled2 = false
					return
				}
				try {
					let res = await taskConfirm(this.pkObj)
					this.disabled2 = false
					this.pkId = ''
					this.toClear()
					this._taskTaskQuery()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled2 = false
				}
			},
			toClear () {
				this.val1 = ''
				this.dataList = []
				this.disabled1 = false
				this.disabled2 = false
				this.pkId = ''
				this.pkObj = {}
			}
		}
	}
</script>