<template>
	<view class="common-nav-container">
		<!-- 功能筛选区 -->
		<uni-card :is-shadow="false" is-full>
			<uni-forms label-width="100">
				<!-- 选择小车 -->
				<uni-forms-item label="AGV小车">
					<uni-data-select v-model="agvCode" :localdata="agvChouseOptions" :clear="false"></uni-data-select>
				</uni-forms-item>
				<!-- 点位筛选 -->
				<uni-forms-item label="点位筛选">
					<uni-data-select v-model="areaCode" :localdata="areaOptions" :clear="false"
						@change="getDict"></uni-data-select>
				</uni-forms-item>
				<!-- 起始点位 -->
				<uni-forms-item label="起始点位">
					<uni-data-select v-model="start_point" :localdata="locationOptions"
						:clear="false"></uni-data-select>
				</uni-forms-item>
				<!-- 结束点位 -->
				<uni-forms-item label="结束点位">
					<uni-data-select v-model="end_point" :localdata="locationOptions" :clear="false"></uni-data-select>
				</uni-forms-item>
				<!-- 任务状态 -->
				<uni-forms-item label="当前任务">
					<view v-if="reqCode === ''">
						<u-tag text="无任务" size="large" type="info"></u-tag>
					</view>
					<view v-else>
						<u-tag :text="reqCode" size="large" type="primary"></u-tag>
					</view>
				</uni-forms-item>
				<!-- 按钮区域 -->
				<u-row customStyle="margin-bottom: 10px" :gutter="40">
					<u-col span="6">
						<u-button :disabled="loading" :loading="loading" shape="circle" type="success" text="执行任务" @click="doStartAgv"></u-button>
					</u-col>
					<u-col span="6">
						<u-button :disabled="loading" :loading="loading" shape="circle" type="error" text="取消任务" @click="doStopAgv"></u-button>
					</u-col>
				</u-row>
				<view style="margin-top: 60px;">
					<u-button :disabled="loading" :loading="loading" shape="circle" type="info" text="清空任务记录" @click="clearTask"></u-button>
				</view>
			</uni-forms>
		</uni-card>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		go_workshop,
		emergency_doStopAgv
	} from '@/api/materialManagement/MaterialRequsition.js';
	import {
		queryMmAgvLocation
	} from '@/api/materialManagement/MmAgvLocation.js';
	export default {
		data() {
			return {
				loading: false,
				// 区域区分
				areaCode: 0,
				areaOptions: [{
						value: 0,
						text: '全部'
					},
					{
						value: 2,
						text: '毛坯'
					},
					{
						value: 3,
						text: '成品'
					},
					{
						value: 4,
						text: '一楼'
					},
					{
						value: 5,
						text: '二楼'
					},
				],
				// 开始点位
				start_point: '',
				// 结束点位
				end_point: '',
				// AGV点位地址
				locationOptions: [],
				// 任务code
				reqCode: '',
				// AGV小车选择列表
				agvChouseOptions: [{
						value: '1743',
						text: '涂装1号AGV'
					},
					{
						value: '1744',
						text: '涂装2号AGV'
					}
				],
				// AGV小车选择
				agvCode: '1743'
			};
		},
		watch: {},
		filters: {},
		mounted() {
			// this.init();
		},
		onShow() {
			this.init();
		},
		methods: {
			init() {
				this.loading = true;
				this.getDict();
				this.getTaskStorage();
				this.loading = false;
			},
			getDict() {
				const query = {
					pageNum: 1,
					pageSize: 1000,
					areaCode: this.areaCode
				}
				queryMmAgvLocation(query).then((res) => {
					const {
						code,
						data
					} = res;
					if (code === 200) {
						this.locationOptions = this.formatDict(data.result);
						if (this.start_point === '' && this.locationOptions.length > 0) {
							this.start_point = this.locationOptions[0].value;
						}
						if (this.end_point === '' && this.locationOptions.length > 0) {
							this.end_point = this.locationOptions[0].value;
						}
					}
				});
			},
			formatDict(list) {
				try {
					const newList = list.map(item => {
						return {
							value: item.coordinate,
							text: item.coordinate
						}
					})
					return newList;
				} catch (e) {
					this.$refs.uToast.show({
						type: 'error',
						message: '字典数据解析异常!' + e.message
					});
					return [];
				}
			},
			// 任务启动
			doStartAgv() {
				if (this.loading) {
					return;
				}
				if (this.start_point == '' || this.end_point == '') {
					this.$refs.uToast.show({
						type: 'error',
						message: '起点或者终点不能为空'
					});
					return;
				}
				const query = {
					start_point: this.start_point,
					end_point: this.end_point,
					agvCode: this.agvCode
				};
				this.loading = true;
				setTimeout(() => {
					this.loading = false;
				}, 30000);
				go_workshop(query).then((res) => {
					if (res.code == 200) {
						try {
							let json = JSON.parse(res.data);
							this.reqCode = json.data;
							this.$refs.uToast.show({
								type: 'success',
								message: 'agv起动成功' + this.reqCode
							});
							this.saveTaskStorage();
						} catch (e) {
							this.$refs.uToast.show({
								type: 'error',
								message: 'agv起动失败：' + this.reqCode
							});
						}
					}
					this.loading = false;
				});
			},
			// 取消任务
			doStopAgv() {
				if (this.loading) {
					return;
				}
				if (this.reqCode === '') {
					this.$refs.uToast.show({
						type: 'error',
						message: '无任务编号，无法取消！'
					});
					return;
				}
				const query = {
					reqCode: this.reqCode
				};
				this.loading = true;
				setTimeout(() => {
					this.loading = false;
				}, 30000);
				emergency_doStopAgv(query).then((res) => {
					if (res.code == 200) {
						this.reqCode = '';
						this.$refs.uToast.show({
							type: 'success',
							message: '成功取消任务：' + res.data
						});
						this.cancelTaskStorage();
					}
					this.loading = false;
				});
			},
			// 保存任务缓存
			saveTaskStorage() {
				try {
					const data = {
						start_point: this.start_point,
						end_point: this.end_point,
						agvCode: this.agvCode,
						reqCode: this.reqCode
					}
					uni.setStorageSync('AGVRemoteControlTaskData', JSON.stringify(data))
				} catch (err) {

				}
			},
			// 获取任务缓存
			getTaskStorage() {
				try {
					const data = JSON.parse(uni.getStorageSync('AGVRemoteControlTaskData'));
					if (data != null) {
						this.start_point = data.start_point;
						this.end_point = data.end_point;
						this.agvCode = data.agvCode;
						this.reqCode = data.reqCode;
					}
				} catch (err) {

				}
			},
			// 清空任务缓存
			cancelTaskStorage() {
				uni.removeStorageSync('taskData');
			},
			// 清空任务记录
			clearTask() {
				this.cancelTaskStorage();
				this.reqCode = '';
			}
		}
	};
</script>

<style scoped>
</style>