<template>
	<view class="zd_container">
		<!-- agv管制 -->
		<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> -->
						<span class="filter_label">{{$t('filter.carno')}}</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index" :localdata="options" placeholder="" @change="selectChange"></uni-data-select>
					</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>{{$t('grid.number')}}</th>
								<th>{{$t('filter.carno')}}</th>
								<th>{{$t('grid.status')}}</th>
								<th>{{$t('grid.has-task')}}</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" @click="toCheck(e)" :class="{'checked': e.create_name === pkId}">
								<td>{{i+1}}</td>
								<td>{{e.agv_no}}</td>
								<td>{{e.status}}</td>
								<td>{{e.hasTask}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<!-- <uni-load-more color="#007AFF" iconType="circle" :status="status" :icon-size="14" :content-text="contentText" v-if="dataList.length > 0"/> -->
		</view>
		<view class="zd-row submit-bar">
			<!-- <button class="zd-col-6 button-primary" @tap="searchList">查询</button>
			<button class="zd-col-7 button-primary" :class="{'button-info': !index}" :disabled="disabled" @tap="_pause">进入</button>
			<button class="zd-col-7 button-primary" :class="{'button-info': !pkId}" :disabled="disabled2" @tap="_resume">退出</button> -->
			<button class="zd-col-7 button-primary" @tap="searchList">{{$t('button.search')}}</button>
			<button class="zd-col-7 button-primary" :class="{'button-info': !index}" :disabled="disabled" @tap="handleConfirm">{{$t('button.pause')}}</button>
			<button class="zd-col-7 button-primary" :class="{'button-info': !index}" :disabled="disabled2" @tap="handleConfirm2">{{$t('button.recover')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import { confirmAction } from '@/utils/utils.js'
	import {getAgvNos, selectAgvInfo, pause, resume} from '@/utils/getData4.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				pkId: '',
				pkObj: {},
				dataList: [],
				reload: false,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				totalCount: 0,
				pageNum: 1,
				pageSize: 10,
				disabled: false,
				disabled2: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._getAgvNos()
		},
		methods: {
			async _getAgvNos () {
				try {
					let res = await getAgvNos()
					if (res) {
						this.options = res.data
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			selectChange (e) {
				this.index = e
				this.searchList()
			},
			clearUp () {
				this.index = ''
				this.pkId = ''
				this.dataList = []
				this.disabled = false
				this.disabled2 = false
			},
			searchList () {
				this.dataList = []
				this.pageNum = 1
				this._selectAgvInfo()
			},
			async _selectAgvInfo () {
				let res = await selectAgvInfo(this.index)
				if (res.code === '200') {
					this.dataList = res.data
					// this.totalCount = res.totalElements
					// if (res.totalElements > 0) {
					// 	const dataMap = res.content
					// 	this.dataList = this.reload ? dataMap : this.dataList.concat(dataMap)
					// 	this.reload = false
					// } else {
					// 	this.dataList = []
					// }
					// if (this.totalCount == this.dataList.length) {
					// 	this.reload = false
					// 	this.status = 'noMore'
					// }
				}
			},
			onReachBottom () {
				if (this.totalCount > this.dataList.length) {
					this.status = 'loading'
					setTimeout(() => {
						this.pageNum++
						this._selectAgvInfo()
					}, 1000)
				} else { //停止加载
					this.status = 'noMore'
				}
			},
			toCheck (e) {
				this.pkId = e.create_name === this.pkId ? '' : e.create_name
				this.pkObj = this.pkId === e.create_name ? e : {}
			},
			async handleConfirm() {
				if (!this.index) {
					return
				}
				const isConfirmed = await confirmAction("确认操作", "确定要执行此操作吗？")
				if (isConfirmed) {
					this._pause()
				}
			},
			async _pause () {
				this.disabled = true
				try {
					let res = await pause(this.index, '1')
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			},
			async handleConfirm2() {
				if (!this.index) {
					return
				}
				const isConfirmed = await confirmAction("确认操作", "确定要执行此操作吗？")
				if (isConfirmed) {
					this._resume()
				}
			},
			async _resume () {
				this.disabled2 = true
				try {
					let res = await resume(this.index, '2')
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
					this.disabled2 = false
				} catch (e) {
					this.disabled2 = false
				}
			}
		}
	}
</script>