<template>
	<view class="container">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<input class="search-input" v-model="searchKeyword" placeholder="搜索听书：书名/作者/主播" confirm-type="search" @confirm="submitSearch" />
			<text class="search-icon" @click="submitSearch">🔍</text>
		</view>
		
			<!-- 推荐横幅轮播 -->
		<view class="banner-section" v-if="bannerBooks && bannerBooks.length > 0">
			<swiper 
				class="banner-swiper" 
				:indicator-dots="bannerBooks.length > 1" 
				:autoplay="bannerBooks.length > 1" 
				:interval="3000" 
				:duration="500"
				indicator-color="rgba(255,255,255,0.5)"
				indicator-active-color="#FF1744"
				:circular="bannerBooks.length > 1"
			>
				<swiper-item v-for="(book, index) in bannerBooks" :key="book.id || index">
					<view class="banner-content" @click="goToListenDetail(book)">
						<image class="banner-cover" :src="book.image" mode="aspectFill" @error="handleBannerImageError(index)"></image>
						<view class="banner-info">
							<text class="banner-title">{{ book.title || '听书推荐' }}</text>
							<text class="banner-subtitle" v-if="book.subtitle">{{ book.subtitle }}</text>
							<text class="banner-desc" v-if="book.desc">{{ book.desc }}</text>
							<text class="banner-tag" v-if="book.tag">{{ book.tag }}</text>
						</view>
					</view>
				</swiper-item>
			</swiper>
		</view>
		
		<!-- 分类导航 -->
		<view class="category-nav">
			<view class="category-item" v-for="(item, index) in categories" :key="index" @click="handleCategoryClick(item, index)">
				<view class="category-icon" :style="{ backgroundColor: item.color }">
					<text class="icon-text">{{ item.icon }}</text>
				</view>
				<text class="category-text">{{ item.name }}</text>
			</view>
		</view>
		
		<scroll-view class="scroll-content" scroll-y>
			<!-- 最近上新 -->
			<view class="section" v-if="recentBooks.length > 0">
				<view class="section-header">
					<text class="section-title">最近上新</text>
					<text class="section-more" @click.stop="goToMoreBooks('recent')">更多 ></text>
				</view>
				<view class="book-grid">
					<view class="book-item" v-for="(book, index) in recentBooks" :key="book.id || index" @click="goToListenDetail(book)">
						<view class="book-cover-wrapper">
							<image class="book-cover" :src="book.image" mode="aspectFill" :lazy-load="true"></image>
							<view class="play-icon-overlay">
								<text class="play-icon">▶</text>
							</view>
						</view>
						<text class="book-name">{{ book.title }}</text>
					</view>
				</view>
			</view>
			
			<!-- 猜你喜欢 -->
			<view class="section" v-if="recommendBooks.length > 0">
				<view class="section-header">
					<text class="section-title">猜你喜欢</text>
					<text class="section-more" @click.stop="goToMoreBooks('guess')">更多 ></text>
				</view>
				<view class="book-list">
					<view class="book-list-item" v-for="(book, index) in recommendBooks" :key="book.id || index" @click="goToListenDetail(book)">
						<image class="book-cover-small" :src="book.image" mode="aspectFill" :lazy-load="true"></image>
						<view class="book-info">
							<text class="book-title">{{ book.title }}</text>
							<text class="book-desc">{{ book.desc }}</text>
							<text class="book-author">{{ book.author }}</text>
							<view class="play-button" @click.stop="playBook(book)">
								<text class="play-button-icon">▶</text>
								<text class="play-button-text">播放</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<!-- 加载中 -->
			<view class="loading-section" v-if="isLoading">
				<text class="loading-text">加载中...</text>
			</view>
			
			<!-- 空状态 -->
			<view class="empty-section" v-if="!isLoading && recentBooks.length === 0 && recommendBooks.length === 0">
				<text class="empty-text">暂无听书内容</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import { getRecentAudiobooks, getRecommendAudiobooks, getBannersByCode } from '../../utils/api.js'
	
	export default {
		data() {
			return {
				searchKeyword: '',
				bannerBooks: [],
				categories: [
					{ name: '全部分类', icon: '📄', color: '#81C784' },
					{ name: '畅听榜', icon: '👑', color: '#FFD54F' },
					{ name: '热门听书', icon: '🔥', color: '#E57373' },
					{ name: '随身听', icon: '🎧', color: '#81C784' },
					{ name: '有声小说', icon: '📚', color: '#4FC3F7' }
				],
				recentBooks: [],
				recommendBooks: [],
				isLoading: false
			}
		},
		onLoad() {
			this.loadData()
		},
		methods: {
			submitSearch() {
				const kw = (this.searchKeyword || '').trim()
				if (!kw) {
					uni.showToast({ title: '请输入关键词', icon: 'none' })
					return
				}
				uni.navigateTo({
					url: `/pages/search/search?mode=audio&keyword=${encodeURIComponent(kw)}`
				})
			},
			async loadData() {
				try {
					this.isLoading = true
					// 从数据库加载轮播图
					await this.loadBanners()
					
					// 加载最近上新
					const recentRes = await getRecentAudiobooks(8)
					if (recentRes && recentRes.code === 200 && recentRes.data) {
						this.recentBooks = recentRes.data.map(item => ({
							id: item.id,
							title: item.title,
							image: item.image || item.cover || 'https://picsum.photos/seed/default/200/300',
							author: item.author || ''
						}))
					}
					
					// 加载推荐听书
					const recommendRes = await getRecommendAudiobooks(10)
					if (recommendRes && recommendRes.code === 200 && recommendRes.data) {
						this.recommendBooks = recommendRes.data.map(item => ({
							id: item.id,
							title: item.title,
							desc: item.brief || item.desc || '',
							author: item.author || '',
							image: item.image || item.cover || 'https://picsum.photos/seed/default/200/300'
						}))
					}
				} catch (e) {
					console.error('加载听书数据失败:', e)
					uni.showToast({
						title: '加载失败，请重试',
						icon: 'none'
					})
				} finally {
					this.isLoading = false
				}
			},
			async loadBanners() {
				try {
					console.log('开始加载听书轮播图...')
					const res = await getBannersByCode('audio_banner')
					console.log('轮播图API响应:', res)
					
					if (res && res.code === 200) {
						if (Array.isArray(res.data) && res.data.length > 0) {
							this.bannerBooks = res.data.map(b => ({
								id: b.targetId || b.id,
								title: b.title || '听书推荐',
								subtitle: b.narrator ? `主播：${b.narrator}` : '',
								desc: b.desc || b.brief || '',
								tag: b.targetType === 'audiobook' ? '听书' : (b.targetType === 'book' ? '书籍' : ''),
								image: b.image || 'https://picsum.photos/seed/default/200/300',
								targetType: b.targetType,
								targetId: b.targetId,
								link: b.link
							}))
							console.log('轮播图数据加载成功，共', this.bannerBooks.length, '条')
						} else {
							console.warn('轮播图数据为空')
							this.bannerBooks = []
						}
					} else {
						console.warn('轮播图API返回错误:', res)
						this.bannerBooks = []
					}
				} catch (e) {
					console.error('加载轮播图失败:', e)
					this.bannerBooks = []
					// 不显示错误提示，避免影响用户体验
				}
			},
			handleCategoryClick(item, index) {
				if (index === 0) {
					uni.showToast({
						title: '全部分类',
						icon: 'none'
					})
				} else if (index === 1) {
					uni.navigateTo({
						url: '/pages/ranking/ranking'
					})
				} else if (index === 2) {
					uni.navigateTo({
						url: '/pages/hot-listen/hot-listen'
					})
				} else if (index === 3) {
					uni.navigateTo({
						url: '/pages/portable-listen/portable-listen'
					})
				} else if (index === 4) {
					uni.navigateTo({
						url: '/pages/audio-novel/audio-novel'
					})
				}
			},
			goToListenDetail(book) {
				if (!book) {
					uni.showToast({
						title: '听书信息不完整',
						icon: 'none'
					})
					return
				}
				// 如果是从轮播图点击，需要根据targetType和targetId判断跳转
				if (book.targetType && book.targetId) {
					if (book.targetType === 'audiobook') {
						uni.navigateTo({
							url: `/pages/listen-detail/listen-detail?audiobookId=${book.targetId}`
						})
						return
					} else if (book.targetType === 'book') {
						uni.navigateTo({
							url: `/pages/book-detail/book-detail?bookId=${book.targetId}`
						})
						return
					} else if (book.targetType === 'url' && book.link) {
						// H5可直接跳转，小程序需要特殊处理
						// #ifdef H5
						window.location.href = book.link
						// #endif
						// #ifdef MP-WEIXIN
						uni.showToast({
							title: '暂不支持外部链接',
							icon: 'none'
						})
						// #endif
						return
					}
				}
				// 普通书籍列表点击，使用book.id
				if (!book.id) {
					uni.showToast({
						title: '听书信息不完整',
						icon: 'none'
					})
					return
				}
				uni.navigateTo({
					url: `/pages/listen-detail/listen-detail?audiobookId=${book.id}`
				})
			},
			playBook(book) {
				// 跳转到详情页，然后播放第一章节
				this.goToListenDetail(book)
			},
			goToMoreBooks(type) {
				uni.navigateTo({
					url: `/pages/more-listen-books/more-listen-books?type=${type}`
				})
			},
			handleBannerImageError(index) {
				// 图片加载失败时使用备用图片
				if (this.bannerBooks[index]) {
					this.bannerBooks[index].image = 'https://picsum.photos/seed/default/200/300'
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		background-color: #FFFFFF;
		display: flex;
		flex-direction: column;
		padding-top: 60px;
		box-sizing: border-box;
	}
	
	.search-bar {
		position: relative;
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;
	}
	
	.search-input {
		width: 100%;
		height: 70rpx;
		background-color: #F5F5F5;
		border-radius: 35rpx;
		padding: 0 80rpx 0 30rpx;
		font-size: 28rpx;
		color: #333333;
		box-sizing: border-box;
	}
	
	.search-icon {
		position: absolute;
		right: 50rpx;
		top: 50%;
		transform: translateY(-50%);
		font-size: 32rpx;
	}
	
	.banner-section {
		padding: 20rpx 30rpx;
		background-color: #FFFFFF;
	}
	
	.banner-swiper {
		width: 100%;
		height: 280rpx;
		border-radius: 16rpx;
		overflow: hidden;
	}
	
	.banner-content {
		display: flex;
		background: linear-gradient(135deg, #FFE5F1 0%, #FFF0F5 100%);
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.1);
		height: 100%;
		width: 100%;
		box-sizing: border-box;
	}
	
	.banner-cover {
		width: 160rpx;
		height: 220rpx;
		border-radius: 8rpx;
		margin-right: 30rpx;
		flex-shrink: 0;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.15);
		background-color: #F5F5F5;
		object-fit: cover;
	}
	
	.banner-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		min-width: 0;
	}
	
	.banner-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.banner-subtitle {
		font-size: 28rpx;
		color: #666666;
		margin-bottom: 10rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.banner-desc {
		font-size: 24rpx;
		color: #999999;
		margin-bottom: 15rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}
	
	.banner-tag {
		font-size: 22rpx;
		color: #FF1744;
		border: 1rpx solid #FF1744;
		border-radius: 8rpx;
		padding: 4rpx 12rpx;
		align-self: flex-start;
	}
	
	.category-nav {
		display: flex;
		justify-content: space-around;
		padding: 40rpx 20rpx;
		background-color: #FFFFFF;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 16rpx;
		margin-left: 30rpx;
		margin-right: 30rpx;
		box-shadow: 0 4rpx 12rpx rgba(0,0,0,0.05);
	}
	
	.category-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.category-icon {
		width: 90rpx;
		height: 90rpx;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-bottom: 15rpx;
	}
	
	.icon-text {
		font-size: 44rpx;
	}
	
	.category-text {
		font-size: 24rpx;
		color: #333333;
	}
	
	.scroll-content {
		flex: 1;
		width: 100%;
		padding-bottom: 20rpx;
	}
	
	.section {
		padding: 40rpx 30rpx;
		background-color: #FFFFFF;
		margin-bottom: 20rpx;
	}
	
	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}
	
	.section-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}
	
	.section-more {
		font-size: 28rpx;
		color: #999999;
	}
	
	.book-grid {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	
	.book-item {
		width: calc(25% - 15rpx);
		margin-bottom: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		text-align: center;
	}
	
	.book-cover-wrapper {
		position: relative;
		width: 100%;
		padding-bottom: 140%;
		height: 0;
		border-radius: 8rpx;
		overflow: hidden;
		margin-bottom: 15rpx;
		box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
		background-color: #F5F5F5;
	}
	
	.book-cover {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
	}
	
	.play-icon-overlay {
		position: absolute;
		bottom: 0;
		right: 0;
		width: 48rpx;
		height: 48rpx;
		background-color: #4CAF50;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
		transform: translate(25%, 25%);
		box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.2);
	}
	
	.play-icon {
		font-size: 24rpx;
		color: #FFFFFF;
		margin-left: 4rpx;
	}
	
	.book-name {
		font-size: 24rpx;
		color: #333333;
		display: block;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		width: 100%;
	}
	
	.book-list {
		display: flex;
		flex-direction: column;
	}
	
	.book-list-item {
		display: flex;
		margin-bottom: 30rpx;
		align-items: center;
	}
	
	.book-cover-small {
		width: 120rpx;
		height: 160rpx;
		border-radius: 8rpx;
		margin-right: 20rpx;
		flex-shrink: 0;
		box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
		background-color: #F5F5F5;
	}
	
	.book-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		min-width: 0;
	}
	
	.book-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
		margin-bottom: 10rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.book-desc {
		font-size: 26rpx;
		color: #666666;
		line-height: 1.4;
		margin-bottom: 10rpx;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1;
		overflow: hidden;
	}
	
	.book-author {
		font-size: 24rpx;
		color: #999999;
		margin-bottom: 15rpx;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}
	
	.play-button {
		background-color: #4CAF50;
		color: #FFFFFF;
		border-radius: 30rpx;
		padding: 10rpx 25rpx;
		font-size: 26rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		width: fit-content;
	}
	
	.play-button-icon {
		font-size: 24rpx;
		margin-right: 10rpx;
	}
	
	.play-button-text {
		line-height: 1;
	}
	
	.loading-section {
		padding: 100rpx 30rpx;
		text-align: center;
	}
	
	.loading-text {
		font-size: 28rpx;
		color: #999999;
	}
	
	.empty-section {
		padding: 100rpx 30rpx;
		text-align: center;
	}
	
	.empty-text {
		font-size: 28rpx;
		color: #999999;
	}
</style>

