<template>
	<view class="home-page">
		<!-- 搜索栏 -->
		<view class="search-wrapper">
<!-- <view class="location-box" @click="onLocationClick">
				<text class="location-text">昆明市</text>
				<uni-icons type="bottom" size="10" color="#fff"></uni-icons>
			</view> -->
			<view class="search-bar" @click="onSearchClick">
				<uni-icons type="search" size="16" color="#999"></uni-icons>
				<text class="search-placeholder">搜索宠物名/主人</text>
			</view>
		</view>

		<!-- 轮播图 -->
		<view class="banner-swipe-wrapper">
			<swiper class="my-swipe" :autoplay="true" :interval="3000" :circular="true" indicator-dots
				indicator-active-color="#ffffff" indicator-color="rgba(255,255,255,0.5)">
				<swiper-item v-for="(image, index) in bannerImages" :key="index">
					<image :src="image" class="swipe-img" mode="aspectFill"></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- 系统通知栏 -->
		<view class="notice-section">
			<view class="notice-bar">
				<uni-icons type="sound" size="14" color="#ed6a0c"></uni-icons>
				<scroll-view scroll-x class="notice-scroll" :show-scrollbar="false">
					<text class="notice-text">【重要通知】尊敬的用户，由于近期接送需求激增，请各位宠主尽量提前24小时进行服务预约，感谢您的理解与配合！</text>
				</scroll-view>
			</view>
		</view>

		<!-- 重构后的对称服务栅格布局 -->
		<view class="new-service-grid" v-if="services.length >= 3">
			<!-- 顶部主打服务 (Hero Card) -->
			<view class="hero-card" @click="goToDetail(services[0])">
				<view class="card-content">
					<text class="card-title">{{ services[0].name }}</text>
					<text class="card-desc">{{ services[0].remark }}</text>
				</view>
				<image :src="services[0].iconUrl" class="hero-icon" mode="aspectFit"></image>
			</view>

			<!-- 底部双列服务 (Secondary Cards) -->
			<view class="card-row">
				<view class="sub-card" v-for="(item, index) in services.slice(1, 3)" :key="index"
					@click="goToDetail(item)">
					<view class="card-content">
						<text class="sub-title">{{ item.name }}</text>
						<text class="sub-desc">{{ item.remark }}</text>
					</view>
					<image :src="item.iconUrl" class="sub-icon" mode="aspectFit"></image>
				</view>
			</view>
		</view>

		<!-- 全部服务列表 -->
		<view class="section-header">
			<text class="section-title">全部服务</text>
			<view class="more-link" @click="goToServices">
				<text>查看全部</text>
				<uni-icons type="right" size="12" color="#999"></uni-icons>
			</view>
		</view>

		<view class="recommend-list">
			<view class="recommend-card" v-for="(item, index) in services.slice(3, 8)" :key="index"
				@click="goToDetail(item)">
				<image :src="item.iconUrl" class="item-img" mode="aspectFill"></image>
				<view class="item-info">
					<view class="item-header">
						<text class="item-title">{{ item.name }}</text>
					</view>
					<text class="item-desc">{{ item.remark }}</text>
					<view class="item-bottom">
						<text class="tag" v-if="serviceModeEnum[item.mode]">{{ serviceModeEnum[item.mode] }}</text>
					</view>
				</view>
			</view>
		</view>

		<custom-tabbar></custom-tabbar>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import customTabbar from '@/components/custom-tabbar/index.vue'
import { listAllService } from '@/api/service/list'
import { getAppSetting } from '@/api/system/appSetting'
import serviceModeEnum from '@/json/serviceMode.json'

const bannerImages = ref([])
const services = ref([])

const fetchServices = async () => {
	try {
		// @Author: Antigravity
		const res = await listAllService()
		// 兼容直接返回数组或返回包装对象的情况
		services.value = res.rows || res || []
	} catch (error) {
		console.error("Failed to fetch services", error)
	}
}

// 获取首页轮播图配置 @Author: Antigravity
const fetchBanners = async () => {
	try {
		// 获取“好萌友”端配置 (ID 2) @Author: Antigravity
		const res = await getAppSetting(2)
		const data = res.data || res
		if (data && data.homeBannerUrls) {
			bannerImages.value = data.homeBannerUrls.split(',').filter(Boolean)
		}
	} catch (error) {
		console.error('Failed to fetch banners', error)
	}
}

onShow(() => {
	fetchServices()
	fetchBanners()
})

const onLocationClick = () => {
	uni.showToast({ title: '城市选择功能即将上线', icon: 'none' })
}

const onSearchClick = () => {
	// 跳转搜索页（预留）
}

const goToServices = () => {
	uni.reLaunch({ url: '/pages/service/all/index' })
}

const goToDetail = (item) => {
	// @Author: Antigravity
	// 存储完整的服务数据，供详情页读取
	uni.setStorageSync('currentService', item)
	
	if (item.name === '托运') {
		uni.reLaunch({ url: '/pages/service/all/index' })
		return
	}
	uni.navigateTo({
		url: `/pages/service/detail/index?service=${item.name}`
	})
}
</script>

<style lang="scss" scoped>
.home-page {
	background-image: linear-gradient(to bottom, #FFD93D 0%, #FFD93D 440rpx, #f2f2f2 440rpx, #f2f2f2 100%);
	min-height: 100vh;
	padding-bottom: 120rpx;
}

.search-wrapper {
	padding: 24rpx 32rpx;
	padding-top: calc(var(--status-bar-height, 44px) + 20rpx);
	display: flex;
	align-items: center;
	gap: 16rpx;
}

.location-box {
	display: flex;
	align-items: center;
	gap: 6rpx;
	flex-shrink: 0;
}

.location-text {
	font-size: 28rpx;
	color: #fff;
	font-weight: bold;
}

.search-bar {
	flex: 1;
	display: flex;
	align-items: center;
	background: rgba(255, 255, 255, 0.85);
	border-radius: 40rpx;
	padding: 16rpx 24rpx;
	gap: 12rpx;
}

.search-placeholder {
	font-size: 26rpx;
	color: #999;
}

.banner-swipe-wrapper {
	padding: 0 32rpx 24rpx;
}

.my-swipe {
	border-radius: 32rpx;
	overflow: hidden;
	height: 320rpx;
}

.swipe-img {
	width: 100%;
	height: 320rpx;
}

.notice-section {
	padding: 0 32rpx 24rpx;
}

.notice-bar {
	display: flex;
	align-items: center;
	background: #fffbe8;
	border-radius: 16rpx;
	padding: 12rpx 20rpx;
	gap: 12rpx;
	height: 64rpx;
	overflow: hidden;
}

.notice-scroll {
	flex: 1;
	white-space: nowrap;
}

.notice-text {
	font-size: 24rpx;
	color: #ed6a0c;
}

.new-service-grid {
	padding: 0 32rpx;
	display: flex;
	flex-direction: column;
	gap: 24rpx;
	margin-bottom: 24rpx;
}

.hero-card {
	height: 260rpx;
	background: linear-gradient(135deg, #60a5fa 0%, #2563eb 100%);
	border-radius: 32rpx;
	padding: 40rpx;
	position: relative;
	overflow: hidden;
	display: flex;
	align-items: center;
	box-shadow: 0 12rpx 32rpx rgba(37, 99, 235, 0.18);
}

.card-content {
	z-index: 1;
	display: flex;
	flex-direction: column;
}

.card-title {
	font-size: 38rpx;
	color: #fff;
	font-weight: 800;
	letter-spacing: 2rpx;
}

.card-desc {
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.9);
	margin-top: 12rpx;
}

.hero-icon {
	position: absolute;
	right: 20rpx;
	bottom: -10rpx;
	width: 240rpx;
	height: 240rpx;
	opacity: 0.85;
}

.card-row {
	display: flex;
	gap: 24rpx;
}

.sub-card {
	flex: 1;
	height: 200rpx;
	border-radius: 32rpx;
	padding: 30rpx;
	position: relative;
	overflow: hidden;
	box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.06);
}

.sub-card:nth-child(1) {
	background: linear-gradient(135deg, #34d399 0%, #059669 100%);
}

.sub-card:nth-child(2) {
	background: linear-gradient(135deg, #fb923c 0%, #ea580c 100%);
}

.sub-title {
	font-size: 30rpx;
	color: #fff;
	font-weight: bold;
	display: block;
}

.sub-desc {
	font-size: 20rpx;
	color: rgba(255, 255, 255, 0.85);
	margin-top: 8rpx;
	display: block;
}

.sub-icon {
	position: absolute;
	right: 4rpx;
	bottom: 4rpx;
	width: 100rpx;
	height: 100rpx;
	opacity: 0.8;
}

.section-header {
	padding: 50rpx 32rpx 20rpx;
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.section-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.more-link {
	display: flex;
	align-items: center;
	gap: 4rpx;
	font-size: 26rpx;
	color: #999;
}

.recommend-list {
	padding: 20rpx 32rpx 40rpx;
}

.recommend-card {
	background: #fff;
	border-radius: 24rpx;
	padding: 24rpx;
	display: flex;
	gap: 24rpx;
	margin-bottom: 24rpx;
	box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
}

.item-img {
	width: 180rpx;
	height: 180rpx;
	border-radius: 16rpx;
}

.item-info {
	flex: 1;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
}

.item-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
}

.item-title {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
}

.booked {
	font-size: 24rpx;
	color: #999;
}

.item-desc {
	font-size: 26rpx;
	color: #666;
	margin: 12rpx 0;
}

.item-bottom {
	display: flex;
	align-items: center;
}

.tag {
	background-color: #e3f2fd;
	color: #2196f3;
	font-size: 22rpx;
	padding: 6rpx 16rpx;
	border-radius: 8rpx;
}
</style>
