<template>
	<view class="container">
		<view class="topBox">
			<!-- <view>
				<span>任务编号：</span>
				<span>111223334455555</span>
			</view>
			<view>
				<span>任务描述：</span>
				<span>样品入库</span>
			</view>
			<view>
				<span>物料编码：</span>
				<span>112222323</span>
			</view>
			<view>
				<span>作业数量：</span>
				<span>4</span>
			</view>
			<view>
				<span>起始位置：</span>
				<span>A1111111</span>
			</view>
			<view>
				<span>目标位置：</span>
				<span>G222222</span>
			</view>
			<view>
				<span>创建时间：</span>
				<span>2025-07-01 11：:11：:22</span>
			</view>
			<view>
				<span>启用联动：</span>
			</view> -->
			<uv-form labelPosition="left" :model="taskDetail"  ref="form" labelWidth="120">
				<uv-form-item label="任务编号"  borderBottom >
					<uv-input v-model="taskDetail.taskNo" border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="任务描述"  borderBottom >
					<uv-input v-model="taskDetail.taskDtl" border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="物料编码"  borderBottom >
					<uv-input v-model="taskDetail.mid" border="none">
					</uv-input>
				</uv-form-item>
				<!-- <uv-form-item label="作业数量"  borderBottom >
					<uv-input v-model="taskDetail.num" border="none">
					</uv-input>
				</uv-form-item> -->
				<uv-form-item label="起始位置"  borderBottom >
					<uv-input v-model="taskDetail.sourceName" border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="目标位置"  borderBottom >
					<uv-input v-model="taskDetail.targetName" border="none">
					</uv-input>
				</uv-form-item>
				<uv-form-item label="创建时间"   >
					<uv-input v-model="taskDetail.createTime" border="none">
					</uv-input>
				</uv-form-item>
			</uv-form>
		</view>
		<view class="lineBox"></view>
		<view class="statusBox">
			<view class="titleBox">运行状态</view>
			<view class="statusShowBox">
				<view v-for="(item, index) in taskTips" :key="index" class="everyLine">{{ item }}</view>
				<!-- <view class="everyLine">发送物资移库出入台指令成功</view>
				<view class="everyLine">发送物资移库出入台指令成功</view>
				<view class="everyLine">发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view>
				<view>发送物资移库出入台指令成功</view> -->
			</view>
		</view>
		<view class="btn">
			<!-- :disabled="endTask" -->
			<view class="everyBtn"><uv-button type="primary" shape="circle" text="执行任务" class="mainBtn"   @click="startTask" :disabled="endTask"></uv-button></view>
			<!-- <view class="everyBtn"><uv-button type="error" :plain="true" shape="circle" text="取消任务"   @click="submit" :disabled="endTask"></uv-button></view> -->
			<view class="everyBtn"><uv-button type="error" :plain="true" shape="circle" text="删除任务"   @click="delTaskFun" ></uv-button></view>
			<!-- :disabled="endTask" -->
		</view>
		<uv-modal ref="modal" title="提示" cancelText="取消" showCancelButton content='确定要删除吗？'  @confirm="confirmDel"></uv-modal>
	</view>
</template>
<script >
// import {executeTask,agvStatus,taskDetailF,wcsTask,wcsStatus} from "@/api/dispatch"
import {executeTask,agvStatus,wcsTask,wcsStatus,outGoods,taskDetailF,delTask} from "@/api/dispatch"
export default{
	data(){
		return{
			taskDetail:{
				taskNo:"",
				taskDtl:"",
				mid:"",
				num:null,
				sourceName:"",
				targetName:"",
				createTime:"",
				taskType:"",
				id:null
			},
			taskStatus:false,	//任务状态
			endTask:false,
			taskTips:[],
			wcs_TaskID:-1
		}
	},
	onLoad(options){
		// console.log(11111)
		if(options){
			// console.log(options)
			this.taskDetailFun(options.id)
		}
		// console.log(options)
	},
	methods:{
		//任务详情
		taskDetailFun(id){
			taskDetailF(id).then(res => {
			//   console.log(1111)
				if(res.code==200){
					this.taskDetail = res.data
					if(this.taskDetail.taskStatus==2 || this.taskDetail.taskStatus==1){
						this.endTask = true
					}
				}
			//   console.log(res)
			})
			
		},
		//执行任务
		startTask(){
			if(this.taskStatus){
				uni.showToast({
					title: '任务执行中,请勿重复执行',
					icon: 'error',
					mask: true
				})
				return
			}
			if(this.taskDetail.taskType=='0'){	//上架
				this.in_storage()
			}else if(this.taskDetail.taskType=='1'){	//下架
				this.out_storage()
			}
			// if(!confirm("确定执行调度任务?")){
			// 	return
			// }
		},
		async in_storage(){
			this.taskStatus = true
			// console.log(2222)
			var first = this.taskDetail.targetName.substr(0,1);
			var aindex = this.taskDetail.targetName.substr(5,1);
			let requestId = ""
			if(first == "A"){
				var owner = "wms";
				var type = 0;
				var priority = 1;
				// var sourceName = startLoc;
				var agvTargetName = "V01-010101";	//履带的位置叫V01-010101，也是叉车要放货的位置；页面上输入的起始位置V01-230101，指的是叉车取货的开始位置
				if(aindex == '5' || aindex == '6')
				{
					agvTargetName = "V01-090101";
				} 
				// 调用AGV执行接口
				// const agvObj = {"taskNo":this.taskDetail.taskNo,"owner":owner, "type":type, "priority":priority, "sourceName":this.taskDetail.sourceName, "targetName":agvTargetName}
				const agvObj = {"id":this.taskDetail.id,"targetName":agvTargetName,"materialStatus":1}
				const agvResult = await executeTask(agvObj)
				// console.log(1111111)
				// console.log(agvResult)
				// return
				//调用AGV成功
				// agvResult.code==200
				if(agvResult.code==200){
					requestId = agvResult.data.requestId
					this.taskTips.push("发送物资移库出入台指令成功")
					var flag2 = false;
					var ishow = false;
					if(this.taskDetail.targetName == "A01-040903" || this.taskDetail.targetName == "A01-043204"){

					}else{
						const that = this
						var timId2 = setInterval(function(){
							if(flag2) return;
							flag2 = true;
							//获取AGV执行任务的状态
							agvStatus({"requestId": requestId,"status":"TAKE_DOWN_COMPLETED"}).then(res => {
								flag2 = false;
								if(res.code==200 && res.data !=201){
									clearInterval(timId2);
									that.taskTips.push("AGV放货至出入台完成")
									var Materialstatus = 1;
									if(that.taskDetail.mid == "" || that.taskDetail.mid == "0" || that.taskDetail.mid == 0)
									{
										Materialstatus = 0;
									}
									// this.taskTips.push("WCS入库指令发送成功")
									// 向WCS发送入库指令（可能后端自动调用）
									wcsTask({"taskNo":that.taskDetail.taskNo,"materialStatus":Materialstatus,"Location":that.taskDetail.targetName,"mid":that.taskDetail.mid,"num":that.taskDetail.num}).then(res=>{
										if(res.code==200){
											that.wcs_TaskID = res.taskId;
											that.taskTips.push("WCS入库指令发送成功")
										}else{
											that.taskTips.push("WCS入库指令发送失败!")
										}
									})
								}else{
									if(!ishow)
									{
										that.taskTips.push("等待AGV指令执行完成......")
										ishow = true;
									}
								}
							
							})
						},5000)
					}
					var flag3 = false;
					var wcs_finish = false;	
					const that = this
					var timId3 = setInterval(function(){
						// console.log(11111111)
						// console.log(that.wcs_TaskID)
						if(!flag3){
							flag3 = true;
							if(!wcs_finish){
								wcsStatus({"TaskID": that.wcs_TaskID}).then(res=>{
									flag3 = false;
									if(res.code==200 && res.msg=="200"){
										clearInterval(timId3);   
										wcs_finish = true;
										that.taskTips.push("立库物资上架完成")  
										//后端修改的任务状态，所以不调接口，直接提示任务完成
										that.taskTips.push("作业任务完成！")  
										that.taskStatus = false;
										that.endTask = true
									}
								})
							}
						}
					},5000)
					
					
				}
				// const agvResult = await taskList({pageNum: 1,pageSize: 10})
				// console.log(11111)
				// console.log(agvResult)
				
			}else if(first == "B"){
				//1、给AGV发指令
				//2、检测AGV执行结果
			}
		},
		//下架
		out_storage(){
			this.taskStatus = true
			//1、调用WCS发送下架指令
			var first = this.taskDetail.sourceName.substr(0,1);
			let requestId = ""
			if(first == "A"){
				var Materialstatus = 1;
                if(this.taskDetail.mid == "" || this.taskDetail.mid == "0" || this.taskDetail.mid == 0)
                {
                    Materialstatus = 0;
                }
				
				//调立库（后端调立库、调叉车）
				executeTask({"id":this.taskDetail.id,"targetName":this.taskDetail.targetName,"materialStatus":Materialstatus}).then(res=>{
					if(res.code==200){
						// this.wcs_TaskID = res.TaskID;
						requestId = res.data.requestId
						this.taskTips.push("向WCS发送物资出库指令成功")
						this.taskTips.push("发送AGV取货指令成功")
						//调AGV
						// var sourceName = "V01-010101";
						// const agvObj = {"id":this.taskDetail.id}
						// executeTask(agvObj).then(res=>{
						// 	if(res.code==200){
						// 		requestId = res.requestId
						// 		this.taskTips.push("发送AGV取货指令成功")
						// 		//agvLinkWcs
						// 	}
						// })
					}else{
						uni.showToast({
							title: '任务执行中,操作失败',
							icon: 'error',
							mask: true
						})
						this.taskStatus = false;
						return
					}
				})
				//定时查询 叉车 执行状态
				let flag3 = false;
				var agv_finish = false;	
				const that = this
				let timId3 = setInterval(function(){
					if(!flag3){
						flag3 = true;
						if(!agv_finish){
							// END_ARRIVE
							agvStatus({"requestId": requestId,"status":"TAKE_DOWN_COMPLETED"}).then(res=>{
								flag3 = false;
								if(res.code == 200 && res.data != 201){
									clearInterval(timId3);
									agv_finish = true;
									that.taskTips.push("物资移库完成")
									that.taskTips.push("作业任务完成")
									that.taskStatus = false;
									that.endTask = true
									that.$forceUpdate();
								}
							})
						}
					}
				},8000)

			}else{
				uni.showToast({
					title: '起始位置有误',
					icon: 'error',
					mask: true
				})
				
			}
			
		},
		//删除任务
		delTaskFun(){
			// this.taskDetail
			this.$refs.modal.open();
		},
		confirmDel(){
			delTask(this.taskDetail.id).then(res=>{
				
				if(res.code==200){
					uni.showToast({
						title: '操作成功',
						icon: 'success',
						mask: true
					})
					uni.navigateTo({
						url: '/pagesDispatch/dispatchList',
					})
				}
			})
		}
	}
}
</script>
<style scoped lang="scss">
.container{
	// height: calc(100vh - var(--window-top));
	height: 100vh;
	.topBox{
		padding:0 40rpx;
		height: 40%;
	}
	// background-color: #ededed;
	.lineBox{
		background-color: #ededed;
		height: 40rpx;
		margin-top: 40rpx;
		margin-bottom: 20rpx;
	}
	.statusBox{
		height: 42%;
		
		padding:0 20rpx;
		.titleBox{
			// padding-left: 20rpx;
			margin-bottom: 20rpx;
			// font-size: 28rpx;
			font-weight: bold;
		}
		.statusShowBox{
			// background-color: #ededed;
			height: 80%;
			border-radius: 20rpx;
			border:2rpx solid rgb(214, 215, 217);
			overflow: auto;
			.everyLine{
				line-height: 40rpx;
			}
		}
	}
	.btn{
		display: flex;
		justify-content: space-around;
		.everyBtn{
			width: 30%;
		}
	}
}
</style>