<template>
	<view class="page">

		<view class="section-title">巡检点</view>

		<!-- 时间轴列表 -->
		<view class="timeline">
			<view v-for="(item, idx) in taskList" :key="idx" class="node"
				:class="[{ 'is-last': idx === taskList.length - 1 }]">
				<!-- 左侧：点 + 竖线 -->
				<view class="rail">
					<view class="dot" :class="{
              'dot-active': item.inspectionState == 1,
              
            }"></view>
				</view>

				<!-- 右侧内容 -->
				<view class="card">
					<!-- 标题块 + 操作区（打包在一起，方便宽度同步） -->
					<view class="title-ops-wrapper">
						<!-- 标题块 -->
						<view v-if="item.inspectionState ==0 " class="title-solid">
							{{ item.pointName }}
							<view>
								{{ item.pointStartTime.substring(0,16) }} - {{ item.pointEndTime.substring(0,16) }}
							</view>
						</view>
						<view v-else class="title-dashed">
							{{ item.pointName }}
							<view>
								{{ item.pointStartTime.substring(0,16) }} - {{ item.pointEndTime.substring(0,16) }}
							</view>
							<!-- 添加3个子项 -->
							<view v-if="item.inspectionResults != 1" class="order-container">
								<view class="status2"   @click="goDetail(item)">
									<view>完成巡检</view>
									<view class="badge" :class="'badge-warn'">
										{{'异常' }}
									</view>
								</view>
								<view v-if="getOptTime(item.mserviceWorkOrdersVo.recordVoList,0)" class="sub-item">
									<view class="sub-dot"></view>
									<view class="sub-text">提报时间：{{ getOptTime(item.mserviceWorkOrdersVo.recordVoList,0) }}</view>
								</view>
								<view v-if="getOptTime(item.mserviceWorkOrdersVo.recordVoList,1)" class="sub-item">
									<view class="sub-dot"></view>
									<view class="sub-text">处理时间：{{ getOptTime(item.mserviceWorkOrdersVo.recordVoList,1) }}</view>
								</view>
								<view v-if="getOptTime(item.mserviceWorkOrdersVo.recordVoList,4)" class="sub-item">
									<view class="sub-dot"></view>
									<view class="sub-text">完成时间：{{ getOptTime(item.mserviceWorkOrdersVo.recordVoList,4) }}</view>
								</view>
								<view class="order-id">工单号{{item.mserviceWorkOrdersVo.orderNo}}</view>
							</view>
						</view>

						<!-- 操作区：宽度跟随标题块，内部居中 -->
						<view class="ops" v-if="item.inspectionState ==0">
							<view class="btn-outline" @click="startTask(item)">立即巡检</view>
						</view>

						<view class="status" v-else  v-if="item.inspectionResults == 1" @click="goDetail(item)">
							<view>完成巡检</view>
							<view class="badge" :class="item.inspectionResults ==1 ? 'badge-success' : 'badge-warn'">
								{{ item.inspectionResults == 1 ? '正常' : '异常' }}
							</view>
						</view>
				
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				taskInfo: {},
				taskList: []
			}
		},
		onLoad(options) {
			if (options.item) {
				console.log('options.item:', options.item);
				try {
					// 首先尝试解析原始字符串（可能未编码）
					const item = JSON.parse(options.item);
					console.log('parsed item:', item);
					this.taskInfo = item;
				} catch (e) {
					// 如果直接解析失败，再尝试解码后解析
					try {
						const item = JSON.parse(decodeURIComponent(options.item));
						console.log('parsed decoded item:', item);
						this.taskInfo = item;
					} catch (e2) {
						// 如果两种方式都失败，记录错误并使用原始数据
						console.error('解析item失败:', e2);
						this.taskInfo = options.item;
					}
				}
			}
			this.getTaskListMock();
		},
		onShow() {
			uni.$on('refreshData', () => {
				this.getTaskListMock();
			});
		},
		// 页面卸载时移除事件监听器
		onUnload() {
			uni.$off('refreshData');
		},
		methods: {
			async getTaskListMock() {
				let params = {
					taskId: this.taskInfo.id,
				};
				let res = await this.$u.api.getTaskList(params);
				if (res.code == 200 && res.rows) {
					// 提取res.data数组中每个对象的url字段
					this.taskList = res.rows

				}
			},

			startTask(item) {
				const detailItemStr = encodeURIComponent(JSON.stringify(item));
				uni.navigateTo({
					url: `/pages/sys/workbench/inspection/inspectionOpt?item=${detailItemStr}`
				});
			},
			
			goDetail(item) {
				// const detailItemStr = encodeURIComponent(JSON.stringify(item));
				// uni.navigateTo({
				// 	url: `/pages/sys/workbench/inspection/inspectionDetail?item=${detailItemStr}`
				// });
			},
			getOptTime(process, status) {
				if (!process || !Array.isArray(process) || process.length === 0) {
					return null;
				}
				
				const record = process.find(item => item.status == status);
				return record ? record.createTime : null;
			},
		}
	}
</script>

<style scoped>
	/* 页面基础 */
	.page {
		background: #f7f8fa;
		min-height: 100vh
	}

	.section-title {
		font-size: 28rpx;
		color: #0B0B0B;
		font-weight: bold;
		padding-top: 24rpx;
		padding-left: 40rpx;
	}

	/* 时间轴容器 */
	.timeline {
		position: relative;
		padding: 16rpx 24rpx 40rpx 24rpx
	}

	/* 每个节点 */
	.node {
		position: relative;
		padding-left: 72rpx;
		margin-bottom: 32rpx
	}

	.node:last-child {
		margin-bottom: 0
	}

	/* 左侧导轨与连线 */
	.node::after {
		content: "";
		position: absolute;
		left: 37rpx;
		top: 35rpx;
		bottom: -32rpx;
		width: 2rpx;
		background: #e8e9ee
	}

	.node.is-last::after {
		display: none
	}

	/* 左栏（点的容器） */
	.rail {
		position: absolute;
		left: 0;
		top: 0;
		width: 72rpx;
		height: 100%;
		display: flex;
		justify-content: center
	}

	.dot {
		width: 16rpx;
		height: 16rpx;
		margin-top: 20rpx;
		background: #cfd3dc;
		border-radius: 50%
	}

	.dot-active {
		background: #2f6aff
	}



	/* 子项 */
	.sub-item {
		display: flex;
		align-items: center;
		margin-bottom: 16rpx;
	}

	.sub-item:last-child {
		margin-bottom: 0;
	}

	/* 子项圆点 */
	.sub-dot {
		width: 16rpx;
		height: 16rpx;
		border-radius: 50%;
		background-color: #BFBFBF;
		margin-right: 20rpx;
		flex-shrink: 0;
	}

	/* 子项文字 */
	.sub-text {
		font-size: 26rpx;
		color: #666;
	}

	/* 右侧卡片 */
	.card {}

	/* 标题 + 操作区包裹（宽度由标题决定） */
	.title-ops-wrapper {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	/* 标题两种样式 */
	.title-solid {
		background: #2f6aff;
		color: #fff;
		border-radius: 12rpx;
		padding: 12rpx 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 28rpx;
		width: 100%;
		box-sizing: border-box;
	}

	.title-dashed {
		border-radius: 12rpx;
		background: #fff;
		padding: 12rpx 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		font-size: 28rpx;
		color: #000;
		width: 100%;
		box-sizing: border-box;
	}

	/* 操作区：宽度继承标题块，内部居中 */
	.ops {
		display: flex;
		align-items: center;
		justify-content: center;
		margin-top: 20rpx;
		margin-bottom: 40rpx;
	}

	.status {
		width: 280rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #BFBFBF;
		margin-top: 20rpx;
		color: #BFBFBF;
		padding: 12rpx 24rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		margin-bottom: 40rpx;
	}
	
	
	.status2 {
		width: 280rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1px solid #BFBFBF;
		margin-top: 30rpx;
		color: #BFBFBF;
		padding: 12rpx 24rpx;
		border-radius: 12rpx;
		font-size: 26rpx;
		margin-bottom: 20rpx;
	}

	.btn-outline {
		border: 2rpx solid #2f6aff;
		color: #2f6aff;
		background: #fff;
		padding: 12rpx 28rpx;
		border-radius: 12rpx;
		font-size: 26rpx
	}

	.btn-disabled {
		background: #f2f3f5;
		color: #a0a0a0;
		padding: 12rpx 24rpx;
		border-radius: 12rpx;
		font-size: 26rpx
	}

	/* 结果徽标 */
	.badge {
		font-size: 24rpx;
		margin-left: 20rpx;
	}

	.badge-success {
		color: #16a34a;
	}

	.badge-warn {
		color: #f59e0b;
	}

	/* 底部占位文字 */
	.footer-placeholder {
		text-align: center;
		color: #e5e6eb;
		font-size: 28rpx;
		margin-top: 80rpx;
		letter-spacing: 2rpx
	}

	/* 工单信息容器 */
	.order-container {
		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.order-id{
		color: #296AEF;
		font-size: 24rpx;
		margin-bottom: 20rpx;
	}
</style>