<template>
	<view class="service-detail-page">
		<nav-bar title="服务详情"></nav-bar>
		<!-- 顶部主图区域 -->
		<view class="hero-section">
			<image :src="serviceData.heroImg" class="hero-img" mode="aspectFill"></image>
			<view class="hero-overlay"></view>
			<view class="hero-title-box">
				<text class="hero-main-title">{{ serviceData.heroTitle }}</text>
				<text class="hero-sub-title">{{ serviceData.heroSubTitle }}</text>
			</view>
		</view>

		<!-- 购买数据 -->
		<view class="info-section card">
			<view class="service-price-row">
				<view class="price-box">
					<text class="price-label">订单服务费:</text>
					<text class="price-symbol">￥</text>
					<text class="price-num">{{ serviceData.price }}</text>
					<text class="price-unit">{{ serviceData.unit }}</text>
					<text class="price-suffix">起</text>
				</view>
			</view>
			<text class="service-name-text">{{ serviceData.title }}</text>
		</view>

		<!-- 详情选项卡 -->
		<view class="tab-section card">
			<view class="tab-header">
				<view :class="['tab-btn', { active: activeTab === 'intro' }]" @click="activeTab = 'intro'">
					<text>服务介绍</text>
				</view>
				<view :class="['tab-btn', { active: activeTab === 'notice' }]" @click="activeTab = 'notice'">
					<text>下单须知</text>
				</view>
			</view>

			<view class="tab-content" v-if="activeTab === 'intro'">
				<text class="content-title">服务介绍</text>
				<view class="rich-container">
					<rich-text :nodes="processedIntro" class="content-text"></rich-text>
				</view>
				<view class="intro-images" v-if="serviceData.introImages && serviceData.introImages.length">
					<image v-for="(img, idx) in serviceData.introImages" :key="idx" :src="img" class="intro-img"
						mode="widthFix"></image>
				</view>
			</view>

			<view class="tab-content" v-if="activeTab === 'notice'">
				<text class="content-title">下单须知</text>
				<view class="rich-container">
					<rich-text :nodes="processedNotice" class="content-text"></rich-text>
				</view>
			</view>
		</view>

		<!-- 底部操作栏 -->
		<view class="footer-bar safe-bottom" v-if="canBuy">
			<button class="buy-btn" @click="goToOrderApply">立即预约</button>
		</view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import navBar from '@/components/nav-bar/index.vue'
import { onLoad } from '@dcloudio/uni-app'

const activeTab = ref('intro')
const serviceInfo = ref(null)
const canBuy = ref(true)

const defaultHeroImg = 'https://images.unsplash.com/photo-1544568100-847a948585b9?q=80&w=600&auto=format&fit=crop'

onLoad((options) => {
	if (options.buyFlag === 'false') {
		canBuy.value = false
	}
	const storedService = uni.getStorageSync('currentService')
	if (storedService) {
		serviceInfo.value = storedService
		console.log('获取到的服务数据:', storedService)
	}
})

const serviceData = computed(() => {
	if (!serviceInfo.value) {
		return {
			heroImg: defaultHeroImg,
			heroTitle: '服务详情',
			heroSubTitle: '加载中...',
			price: '0',
			unit: '',
			booked: '0 人已约',
			title: '服务名称',
			intro: '加载中...',
			notice: '加载中...',
			introImages: []
		}
	}

	const service = serviceInfo.value
	const priceInYuan = service.price ? (service.price / 100).toFixed(2) : '0.00'

	return {
		type: service.id,
		title: service.name,
		heroTitle: service.name,
		heroSubTitle: service.remark || '专业服务',
		heroImg: service.iconUrl || defaultHeroImg,
		price: priceInYuan,
		unit: '',
		booked: '0 人已约',
		intro: service.introduction || '暂无介绍',
		introImages: [],
		notice: service.orderInstruction || '暂无须知'
	}
})

/**
 * 富文本预处理：限制图片宽度、移除溢出样式，兼容H5富文本
 */
const processRichText = (html) => {
	if (!html) return ''
	let processed = html
	// 1. 给 img 标签强制添加 max-width:100% + height:auto
	processed = processed.replace(/<img([^>]*)>/gi, (match, attrs) => {
		let cleanAttrs = attrs
			.replace(/\s*width\s*=\s*["'][^"']*["']/gi, '')
			.replace(/\s*height\s*=\s*["'][^"']*["']/gi, '')
			.replace(/\s*style\s*=\s*["'][^"']*["']/gi, '')
		return `<img${cleanAttrs} style="max-width: 100%; height: auto; display: block;">`
	})
	// 2. 处理 table/pre/code 等可能溢出的块级元素
	processed = processed.replace(
		/<(table|pre|code)([^>]*)>/gi,
		(match, tag, attrs) => `<${tag}${attrs} style="max-width: 100%; overflow-x: auto; word-break: break-word;">`
	)
	return processed
}

const processedIntro = computed(() => processRichText(serviceData.value.intro))
const processedNotice = computed(() => processRichText(serviceData.value.notice))

const goToOrderApply = () => {
	// @Author: Antigravity
	if (!serviceInfo.value) return;

	// 根据服务的模式（mode为1代表接送）或名称关键字判断跳转类型，以匹配下单页的分类逻辑
	let serviceType = 'feed';
	const name = serviceInfo.value.name || '';
	const mode = String(serviceInfo.value.mode);

	if (mode === '1' || name.includes('接送') || name.includes('托运')) {
		serviceType = 'transport';
	} else if (name.includes('洗') || name.includes('护')) {
		serviceType = 'wash';
	}

	uni.navigateTo({
		url: `/pages/order/apply/index?service=${serviceType}&serviceId=${serviceInfo.value.id}`
	})
}
</script>

<style lang="scss" scoped>
.service-detail-page {
	background-color: #f5f5f5;
	min-height: 100vh;
	padding-bottom: 40rpx;
}

.hero-section {
	position: relative;
	width: 100%;
	height: 640rpx;
	overflow: hidden;
}

.hero-img {
	width: 100%;
	height: 100%;
}

.hero-overlay {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3) 0%, rgba(0, 0, 0, 0) 40%, rgba(0, 0, 0, 0.4) 80%, rgba(0, 0, 0, 0.7) 100%);
}

.hero-title-box {
	position: absolute;
	bottom: 48rpx;
	left: 40rpx;
	right: 40rpx;
	z-index: 2;
}

.hero-main-title {
	display: block;
	font-size: 52rpx;
	font-weight: 900;
	color: #fff;
	margin-bottom: 12rpx;
}

.hero-sub-title {
	display: block;
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.9);
	font-weight: 600;
}

.card {
	background-color: #fff;
	margin-bottom: 20rpx;
}

.info-section {
	padding: 32rpx 40rpx;
	position: relative;
	z-index: 3;
	border-radius: 24rpx 24rpx 0 0;
	margin-top: -24rpx;
}

.service-price-row {
	display: flex;
	justify-content: space-between;
	align-items: flex-end;
	margin-bottom: 20rpx;
}

.price-box {
	display: flex;
	align-items: baseline;
	color: #f44336;
}

.price-label {
	font-size: 26rpx;
	color: #333;
	font-weight: bold;
	margin-right: 12rpx;
}

.price-symbol {
	font-size: 28rpx;
	font-weight: bold;
}

.price-num {
	font-size: 56rpx;
	font-weight: 900;
	line-height: 1;
}

.price-unit {
	font-size: 24rpx;
	color: #999;
	margin-left: 4rpx;
}

.price-suffix {
	font-size: 24rpx;
	color: #999;
	margin-left: 2rpx;
}

.bought-count {
	font-size: 24rpx;
	color: #999;
	background: #f5f5f5;
	padding: 4rpx 16rpx;
	border-radius: 20rpx;
}

.service-name-text {
	font-size: 32rpx;
	color: #111;
	font-weight: bold;
}

.tab-section {
	padding: 0;
}

.tab-header {
	display: flex;
	border-bottom: 2rpx solid #EEEEEE;
}

.tab-btn {
	flex: 1;
	text-align: center;
	padding: 24rpx 0;
	font-size: 28rpx;
	color: #666;
	position: relative;
}

.tab-btn.active {
	color: #fcd53f;
	font-weight: bold;
}

.tab-btn.active::after {
	content: '';
	position: absolute;
	bottom: 0;
	left: 50%;
	transform: translateX(-50%);
	width: 60rpx;
	height: 6rpx;
	background: #fcd53f;
	border-radius: 6rpx;
}

.tab-content {
	padding: 40rpx 32rpx;
}

.content-title {
	display: block;
	font-size: 32rpx;
	color: #333;
	font-weight: 900;
	margin-bottom: 24rpx;
	padding-left: 20rpx;
	position: relative;
}

.content-title::before {
	content: '';
	position: absolute;
	left: 0;
	top: 4rpx;
	bottom: 4rpx;
	width: 8rpx;
	background: #fcd53f;
	border-radius: 4rpx;
}

.rich-container {
	overflow: hidden;
	word-break: break-word;
	line-height: 1.8;
}

.content-text {
	font-size: 28rpx;
	color: #555;
	line-height: 1.8;
}

.intro-images {
	margin-top: 32rpx;
	display: flex;
	flex-direction: column;
	gap: 24rpx;
}

.intro-img {
	width: 100%;
	border-radius: 20rpx;
}

.footer-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	padding: 20rpx 32rpx 40rpx;
	background-color: #fff;
	z-index: 10;
	box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
}

.buy-btn {
	width: 100%;
	height: 92rpx;
	font-size: 32rpx;
	font-weight: bold;
	color: #fff;
	background: linear-gradient(90deg, #ffd53f, #ff9500);
	border: none;
	border-radius: 46rpx;
	line-height: 92rpx;
}
</style>

<!-- 非 scoped 样式：rich-text 内部元素需要穿透生效 -->
<style lang="scss">
.rich-container img {
	max-width: 100% !important;
	height: auto !important;
	display: block;
}

.rich-container table,
.rich-container pre,
.rich-container code {
	max-width: 100% !important;
	overflow-x: auto;
	word-break: break-word;
}

.rich-container p {
	margin-bottom: 12px;
}

.rich-container h1,
.rich-container h2,
.rich-container h3,
.rich-container h4,
.rich-container h5,
.rich-container h6 {
	margin-top: 16px;
	margin-bottom: 8px;
	line-height: 1.4;
}
</style>
