<template>
	<view class="book-detail-container">
		<!-- 加载状态 -->
		<view v-if="loading" class="loading-container">
			<view class="loading-text">加载中...</view>
		</view>
		
		<!-- 图书详情 -->
		<view v-else-if="bookInfo" class="book-detail">
			<!-- 图书基本信息 -->
			<view class="book-header">
				<view class="book-cover">
					<u-image :src="bookInfo.image" width="240rpx" height="320rpx" border-radius="12"></u-image>
				</view>
				<view class="book-info">
					<text class="book-title">{{ bookInfo.title }}</text>
					<text class="book-author">作者：{{ bookInfo.author }}</text>
					<text class="book-publisher">出版社：{{ bookInfo.publisher }}</text>
					<text class="book-year">出版年份：{{ bookInfo.year }}</text>
					<text class="book-isbn">ISBN：{{ bookInfo.isbn }}</text>
					<view class="book-category">
						<u-tag :text="bookInfo.category" type="primary" size="mini" shape="circle" mode="light" />
					</view>
				</view>
			</view>
			
			<!-- 图书描述 -->
			<view class="book-description">
				<view class="section-title">图书简介</view>
				<text class="description-text">{{ bookInfo.description || '暂无简介' }}</text>
			</view>
			
			<!-- 图书状态 -->
			<view class="book-status">
				<view class="section-title">图书状态</view>
				<view class="status-item">
					<text class="status-label">状态：</text>
					<u-tag :text="getStatusText(bookInfo.status)" :type="getStatusType(bookInfo.status)" size="mini" />
				</view>
				<view class="status-item">
					<text class="status-label">创建时间：</text>
					<text class="status-value">{{ formatDate(bookInfo.created_at) }}</text>
				</view>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view v-else class="empty-container">
			<view class="empty-text">图书信息不存在</view>
		</view>
	</view>
</template>

<script>
	import uImage from '@/uview-ui/components/u-image/u-image.vue';
	import uTag from '@/uview-ui/components/u-tag/u-tag.vue';
	
	export default {
		components: {
			uImage,
			uTag
		},
		data() {
			return {
				loading: false,
				bookId: null,
				bookInfo: null
			}
		},
		onLoad(options) {
			if (options.id) {
				this.bookId = options.id;
				this.loadBookDetail();
			} else {
				uni.showToast({
					title: '图书ID不存在',
					icon: 'none'
				});
			}
		},
		methods: {
			// 加载图书详情
			loadBookDetail() {
				this.loading = true;
				
				this.$u.api.bookDetail({
					id: this.bookId
				}).then(res => {
					if (res) {
						// 处理图书信息 - 接口直接返回图书对象
						const book = res;
						this.bookInfo = {
							id: book.id,
							title: book.title,
							author: book.author,
							publisher: book.publisher,
							year: book.publish_year,
							isbn: book.isbn,
							category: book.category,
							description: book.description,
							status: book.status,
							created_at: book.created_at,
							image: book.cover ? book.cover.url : ''
						};
					} else {
						uni.showToast({
							title: '图书信息不存在',
							icon: 'none'
						});
					}
				}).catch(err => {
					console.error('获取图书详情失败:', err);
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				}).finally(() => {
					this.loading = false;
				});
			},
			
			// 获取状态文本
			getStatusText(status) {
				const statusMap = {
					0: '可借阅',
					1: '已借出',
					2: '维护中',
					3: '已下架'
				};
				return statusMap[status] || '未知状态';
			},
			
			// 获取状态类型
			getStatusType(status) {
				const typeMap = {
					0: 'success',
					1: 'warning',
					2: 'info',
					3: 'error'
				};
				return typeMap[status] || 'info';
			},
			
			// 格式化日期
			formatDate(dateStr) {
				if (!dateStr) return '未知';
				const date = new Date(dateStr);
				return date.toLocaleDateString('zh-CN');
			}
		}
	}
</script>

<style lang="scss" scoped>
	.book-detail-container {
		background-color: #f5f5f5;
		min-height: 100vh;
		padding: 20rpx;
	}

	.loading-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 400rpx;
	}

	.loading-text {
		font-size: 28rpx;
		color: #999;
	}

	.empty-container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 400rpx;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
	}

	.book-detail {
		background-color: #fff;
		border-radius: 20rpx;
		padding: 30rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
	}

	.book-header {
		display: flex;
		margin-bottom: 40rpx;
	}

	.book-cover {
		margin-right: 30rpx;
		flex-shrink: 0;
	}

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

	.book-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
		line-height: 1.4;
	}

	.book-author,
	.book-publisher,
	.book-year,
	.book-isbn {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 15rpx;
		line-height: 1.5;
	}

	.book-category {
		margin-top: 20rpx;
	}

	.book-description {
		margin-bottom: 40rpx;
		padding-bottom: 30rpx;
		border-bottom: 1rpx solid #eee;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}

	.description-text {
		font-size: 28rpx;
		color: #666;
		line-height: 1.6;
		text-align: justify;
	}

	.book-status {
		.section-title {
			margin-bottom: 20rpx;
		}
	}

	.status-item {
		display: flex;
		align-items: center;
		margin-bottom: 15rpx;
	}

	.status-label {
		font-size: 28rpx;
		color: #666;
		margin-right: 20rpx;
		min-width: 120rpx;
	}

	.status-value {
		font-size: 28rpx;
		color: #333;
	}
</style>
