<template>
	<view class="hazard-detail-panel-v2">
		<view v-if="loading" class="loading-wrap">
			<text class="loading-text">加载中...</text>
		</view>

		<view v-else-if="historyList.length === 0" class="empty-wrap">
			<text class="empty-text">暂无历史记录</text>
		</view>

		<view v-else class="detail-body" :style="detailBodyStyle">
			<scroll-view
				class="steps-card"
				scroll-y
				:style="scrollAreaStyle"
				:show-scrollbar="false"
				:scroll-into-view="stepScrollIntoView"
				:scroll-with-animation="true"
			>
				<view
					v-for="(item, index) in historyList"
					:key="'step-' + index"
					:id="'hazard-step-' + index"
					class="step-item"
					:class="{ 'step-item--active': activeIndex === index }"
					@tap="scrollToNode(index)"
				>
					<view class="step-track">
						<view
							class="step-dot"
							:class="{ 'step-dot--active': activeIndex === index }"
						>
							<image
								class="step-icon"
								:src="getStepIconPath(item.type, activeIndex === index)"
								mode="aspectFit"
							/>
						</view>
						<text
							class="step-label"
							:class="{ 'step-label--active': activeIndex === index }"
						>{{ item.nodeName }}</text>
						<view v-if="index < historyList.length - 1" class="step-line"></view>
					</view>
				</view>
			</scroll-view>

			<scroll-view
				class="content-column content-scroll"
				scroll-y
				:style="scrollAreaStyle"
				:show-scrollbar="false"
				:scroll-into-view="contentScrollIntoView"
				:scroll-with-animation="scrollWithAnimation"
				@scroll="onContentScroll"
				@scrolltolower="onScrollToLower"
			>
				<view
					v-for="(item, index) in historyList"
					:key="'node-' + index"
					:id="'hazard-node-' + index"
					class="node-section"
					:class="{ 'node-section--last': index === historyList.length - 1 }"
				>
					<view class="detail-card">
						<view class="card-header-v2">
							<view class="card-header-main">
								<text class="operator">{{ item.titlePrefix }}人员：{{ item.operator }}</text>
								<text class="time">{{ item.time }}</text>
							</view>
							<view
								v-if="item.type === 'submit' && item.content.levelName"
								:class="['level-badge', getLevelTagClass(item.content)]"
							>
								{{ item.content.levelName }}
							</view>
						</view>
						<view class="card-divider"></view>

						<view class="card-body">
								<!-- 提交 -->
								<block v-if="item.type === 'submit'">
									<view class="detail-row-v2">
										<text class="label">隐患标题</text>
										<text class="value">{{ item.content.title }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">检查形式</text>
										<text class="value">{{ item.content.source }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">隐患区域</text>
										<view class="value value--inline">
											<view
												v-if="item.content.areaColor"
												class="area-dot"
												:style="{ backgroundColor: item.content.areaColor }"
											></view>
											<text>{{ item.content.areaName }}</text>
										</view>
									</view>
									<view class="detail-row-v2">
										<text class="label">位置描述</text>
										<text class="value">{{ item.content.address }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">隐患等级</text>
										<view class="value">
											<view :class="['level-tag', getLevelTagClass(item.content)]">
												{{ item.content.levelName || '-' }}
											</view>
										</view>
									</view>
									<view class="detail-row-v2">
										<text class="label">隐患标签</text>
										<text class="value">{{ item.content.tagName }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">问题描述</text>
										<text class="value">{{ item.content.description }}</text>
									</view>
									<view
										v-if="item.content.attachments && item.content.attachments.length"
										class="detail-row-v2 detail-row-v2--block"
									>
										<text class="label">隐患附件</text>
										<view class="attachment-list">
											<image
												v-for="(file, idx) in item.content.attachments"
												:key="idx"
												class="attachment-img"
												:src="resolveFileUrl(file.filePath)"
												mode="aspectFill"
												@tap="previewImages(item.content.attachments, idx)"
										@load="scheduleMeasureLayout"
											/>
										</view>
									</view>
									<view class="detail-row-v2">
										<text class="label">参考法规</text>
										<text class="value">{{ item.content.legalBasis }}</text>
									</view>
								</block>

								<!-- 交办 -->
								<block v-else-if="item.type === 'assign'">
									<view class="detail-row-v2">
										<text class="label">指定整改责任人</text>
										<text class="value">{{ item.content.assigneeName }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">指定整改截至日期</text>
										<text class="value">{{ item.content.deadline }}</text>
									</view>
								</block>

								<!-- 整改 -->
								<block v-else-if="item.type === 'rectify'">
									<view class="detail-row-v2">
										<text class="label">整改状态</text>
										<text class="value">{{ item.content.rectifyStatusName }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">整改方案</text>
										<text class="value">{{ item.content.rectifyPlan }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">整改结果</text>
										<text class="value">{{ item.content.rectifyResult }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">整改措施</text>
										<text class="value">{{ item.content.rectificationMeasures }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">管控措施</text>
										<text class="value">{{ item.content.controlMeasures }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">限定整改时间</text>
										<text class="value">{{ item.content.deadline }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">整改责任人</text>
										<text class="value">{{ item.content.rectifierNames }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">管理人员</text>
										<text class="value">{{ item.content.managerNames }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">预计费用</text>
										<text class="value">{{ formatCost(item.content.planCost) }}</text>
									</view>
									<view class="detail-row-v2">
										<text class="label">实际费用</text>
										<text class="value">{{ formatCost(item.content.actualCost) }}</text>
									</view>
									<view
										v-if="item.content.attachments && item.content.attachments.length"
										class="detail-row-v2 detail-row-v2--block"
									>
										<text class="label">整改附件</text>
										<view class="attachment-list">
											<template v-for="(file, idx) in item.content.attachments" :key="idx">
												<image
													v-if="file.fileType === 'image'"
													class="attachment-img"
													:src="resolveFileUrl(file.filePath)"
													mode="aspectFill"
													@tap="previewImages(item.content.attachments, idx)"
										@load="scheduleMeasureLayout"
												/>
												<text v-else class="file-link">{{ file.fileName || '附件' }}</text>
											</template>
										</view>
									</view>
									<view v-if="item.content.signPath" class="detail-row-v2 detail-row-v2--block">
										<text class="label">整改签字</text>
										<image
											class="sign-img"
											:src="resolveFileUrl(item.content.signPath)"
											mode="aspectFit"
											@tap="previewSingle(item.content.signPath)"
											@load="scheduleMeasureLayout"
										/>
									</view>
								</block>

								<!-- 验收 -->
								<block v-else-if="item.type === 'verify'">
									<view class="detail-row-v2">
										<text class="label">验收结果</text>
										<text
											class="result-tag"
											:class="item.content.resultName === '通过' ? 'result-tag--pass' : 'result-tag--fail'"
										>
											{{ item.content.resultName }}
										</text>
									</view>
									<view v-if="item.content.remark" class="detail-row-v2">
										<text class="label">验收备注</text>
										<text class="value">{{ item.content.remark }}</text>
									</view>
									<view
										v-if="item.content.attachments && item.content.attachments.length"
										class="detail-row-v2 detail-row-v2--block"
									>
										<text class="label">验收附件</text>
										<view class="attachment-list">
											<template v-for="(file, idx) in item.content.attachments" :key="idx">
												<image
													v-if="file.fileType === 'image'"
													class="attachment-img"
													:src="resolveFileUrl(file.filePath)"
													mode="aspectFill"
													@tap="previewImages(item.content.attachments, idx)"
										@load="scheduleMeasureLayout"
												/>
												<text v-else class="file-link">{{ file.fileName || '附件' }}</text>
											</template>
										</view>
									</view>
									<view v-if="item.content.signPath" class="detail-row-v2 detail-row-v2--block">
										<text class="label">验收签字</text>
										<image
											class="sign-img"
											:src="resolveFileUrl(item.content.signPath)"
											mode="aspectFit"
											@tap="previewSingle(item.content.signPath)"
											@load="scheduleMeasureLayout"
										/>
									</view>
								</block>

								<!-- 销号 -->
								<block v-else-if="item.type === 'writeoff'">
									<view class="detail-row-v2">
										<text class="label">销号结果</text>
										<text
											class="result-tag"
											:class="item.content.resultName === '通过' ? 'result-tag--pass' : 'result-tag--fail'"
										>
											{{ item.content.resultName }}
										</text>
									</view>
									<view v-if="item.content.remark" class="detail-row-v2">
										<text class="label">销号备注</text>
										<text class="value">{{ item.content.remark }}</text>
									</view>
									<view
										v-if="item.content.attachments && item.content.attachments.length"
										class="detail-row-v2 detail-row-v2--block"
									>
										<text class="label">销号附件</text>
										<view class="attachment-list">
											<template v-for="(file, idx) in item.content.attachments" :key="idx">
												<image
													v-if="file.fileType === 'image'"
													class="attachment-img"
													:src="resolveFileUrl(file.filePath)"
													mode="aspectFill"
													@tap="previewImages(item.content.attachments, idx)"
										@load="scheduleMeasureLayout"
												/>
												<text v-else class="file-link">{{ file.fileName || '附件' }}</text>
											</template>
										</view>
									</view>
									<view v-if="item.content.signPath" class="detail-row-v2 detail-row-v2--block">
										<text class="label">销号签字</text>
										<image
											class="sign-img"
											:src="resolveFileUrl(item.content.signPath)"
											mode="aspectFit"
											@tap="previewSingle(item.content.signPath)"
											@load="scheduleMeasureLayout"
										/>
									</view>
								</block>
							</view>
						</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script setup>
import { computed, toRefs, watch } from 'vue';
import { toImageUrl } from '@/request/request.js';
import { getStepIconPath } from './hazardDetail.js';
import { useHazardDetailScroll } from './useHazardDetailScroll.js';

const props = defineProps({
	detail: {
		type: Object,
		default: () => ({})
	},
	loading: {
		type: Boolean,
		default: false
	},
	bodyHeight: {
		type: Number,
		default: 0
	}
});

const { detail, loading } = toRefs(props);

const {
	historyList,
	activeIndex,
	contentScrollIntoView,
	stepScrollIntoView,
	scrollWithAnimation,
	onContentScroll,
	onScrollToLower,
	scrollToNode,
	scheduleMeasureLayout
} = useHazardDetailScroll(detail, loading);

const scrollAreaStyle = computed(() => {
	const height = props.bodyHeight > 0 ? props.bodyHeight : 400;
	return { height: `${height}px` };
});

const detailBodyStyle = computed(() => {
	const height = props.bodyHeight > 0 ? props.bodyHeight : 400;
	return { height: `${height}px` };
});

watch(
	() => props.bodyHeight,
	(height) => {
		if (height > 0) {
			scheduleMeasureLayout();
			setTimeout(scheduleMeasureLayout, 100);
		}
	}
);

const LEVEL_CLASS_MAP = {
	2: 'level-normal',
	3: 'level-major'
};

const LEVEL_NAME_CLASS_MAP = {
	一般: 'level-normal',
	一般隐患: 'level-normal',
	重大: 'level-major',
	重大隐患: 'level-major'
};

const getLevelTagClass = (content) => {
	const level = content?.level;
	const levelName = content?.levelName;
	if (level != null && LEVEL_CLASS_MAP[level]) {
		return LEVEL_CLASS_MAP[level];
	}
	if (levelName && LEVEL_NAME_CLASS_MAP[levelName]) {
		return LEVEL_NAME_CLASS_MAP[levelName];
	}
	return '';
};

const resolveFileUrl = (path) => toImageUrl(path);

const formatCost = (cost) => {
	if (cost == null || cost === '') return '-';
	const num = Number(cost);
	return Number.isNaN(num) ? '-' : `${num} 元`;
};

const previewSingle = (path) => {
	const url = resolveFileUrl(path);
	if (!url) return;
	uni.previewImage({ current: url, urls: [url] });
};

const previewImages = (attachments, index) => {
	const urls = (attachments || [])
		.filter((f) => f.fileType === 'image' || !f.fileType)
		.map((f) => resolveFileUrl(f.filePath))
		.filter(Boolean);
	if (!urls.length) return;
	uni.previewImage({ current: urls[index] || urls[0], urls });
};
</script>

<style lang="scss" scoped>
.hazard-detail-panel-v2 {
	flex: 1;
	min-height: 0;
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	overflow: hidden;
}

.loading-wrap,
.empty-wrap {
	flex: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	min-height: 0;
}

.loading-text,
.empty-text {
	font-size: 28rpx;
	color: #909399;
}

.detail-body {
	display: flex;
	gap: 20rpx;
	align-items: stretch;
	overflow: hidden;
	box-sizing: border-box;
}

.steps-card {
	width: 140rpx;
	flex-shrink: 0;
	background: #fff;
	border-radius: 20rpx;
	box-sizing: border-box;
}

.step-item {
	padding: 0 12rpx;
}

.step-track {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 28rpx 0 0;
}

.step-dot {
	width: 66rpx;
	height: 66rpx;
	border-radius: 50%;
	background: #f3f3f3;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: background 0.2s;
}

.step-dot--active {
	background: #2667e9;
}

.step-icon {
	width: 36rpx;
	height: 36rpx;
}

.step-label {
	margin-top: 12rpx;
	font-size: 24rpx;
	color: #999;
	line-height: 1.2;
}

.step-label--active {
	color: #2667e9;
	font-weight: 600;
}

.step-line {
	width: 0;
	height: 40rpx;
	margin: 10rpx 0;
	border-left: 2rpx dashed #dcdfe6;
}

.content-column {
	flex: 1;
	width: 0;
	min-height: 0;
	box-sizing: border-box;
}

.content-scroll {
	width: 100%;
	box-sizing: border-box;
}

.node-section {
	box-sizing: border-box;
	padding: 0;
	margin-bottom: 24rpx;
}

.node-section--last {
	padding-bottom: 40rpx;
	margin-bottom: 0;
}

.detail-card {
	background: #fff;
	border-radius: 20rpx;
	overflow: hidden;
	padding: 28rpx 38rpx;
	box-sizing: border-box;
}

.card-header-v2 {
	display: flex;
	align-items: flex-start;
	justify-content: space-between;
	gap: 16rpx;
	padding: 0;
}

.card-header-main {
	flex: 1;
	min-width: 0;
}

.operator {
	font-size: 28rpx;
	font-weight: 600;
	color: #303133;
	line-height: 1.5;
	word-break: break-all;
}

.time {
	display: block;
	margin-top: 8rpx;
	font-size: 24rpx;
	color: #999;
	line-height: 1.4;
}

.level-badge {
	flex-shrink: 0;
	padding: 6rpx 16rpx;
	border-radius: 8rpx;
	font-size: 22rpx;
	font-weight: 500;
	white-space: nowrap;
}

.level-badge.level-normal {
	background: #fff7e6;
	border: 2rpx solid #ffd591;
	color: #fa8c16;
}

.level-badge.level-major {
	background: #fff1f0;
	border: 2rpx solid #ffa39e;
	color: #f5222d;
}

.card-divider {
	height: 0;
	margin: 20rpx 0;
	border-top: 2rpx dashed #eee;
}

.card-body {
	padding: 0;
}

.detail-row-v2 {
	display: flex;
	align-items: flex-start;
	justify-content: flex-start;
	gap: 24rpx;
	padding: 18rpx 0;
}

.detail-row-v2--block {
	flex-wrap: wrap;
}

.label {
	flex-shrink: 0;
	// width: 156rpx;
	font-size: 26rpx;
	color: #999;
	line-height: 1.5;
}

.value {
	flex: 1;
	min-width: 0;
	font-size: 26rpx;
	color: #333;
	line-height: 1.6;
	text-align: left;
	word-break: break-all;
}

.value--inline {
	display: flex;
	align-items: center;
}

.area-dot {
	width: 20rpx;
	height: 20rpx;
	border-radius: 50%;
	margin-right: 12rpx;
	flex-shrink: 0;
}

.level-tag {
	display: inline-flex;
	align-items: center;
	padding: 4rpx 16rpx;
	border-radius: 8rpx;
	font-size: 24rpx;
	font-weight: 500;
	line-height: 1.4;
	white-space: nowrap;
}

.level-normal {
	background: #fff7e6;
	border: 2rpx solid #ffd591;
	color: #fa8c16;
}

.level-major {
	background: #fff1f0;
	border: 2rpx solid #ffa39e;
	color: #f5222d;
}

.tag-badge {
	display: inline-flex;
	align-items: center;
	padding: 4rpx 16rpx;
	border-radius: 8rpx;
	font-size: 24rpx;
	background: #eef3ff;
	border: 2rpx solid #aac5fc;
	color: #2667e9;
}

.attachment-list {
	display: flex;
	flex-wrap: wrap;
	gap: 16rpx;
	flex: 1;
	min-width: 0;
}

.attachment-img {
	width: 160rpx;
	height: 160rpx;
	border-radius: 12rpx;
	background: #f5f7fa;
}

.sign-img {
	width: 300rpx;
	height: 160rpx;
	border: 1rpx solid #e4e7ed;
	border-radius: 8rpx;
	background: #fafafa;
	flex: 1;
	min-width: 0;
	max-width: 100%;
}

.file-link {
	display: inline-block;
	padding: 12rpx 20rpx;
	background: #f5f7fa;
	border: 1rpx solid #e4e7ed;
	border-radius: 8rpx;
	color: #2667e9;
	font-size: 24rpx;
}

.result-tag {
	padding: 6rpx 16rpx;
	border-radius: 6rpx;
	font-size: 24rpx;
}

.result-tag--pass {
	background: #f0f9eb;
	color: #67c23a;
}

.result-tag--fail {
	background: #fef0f0;
	color: #f56c6c;
}
</style>
