<template>
	<view class="page">
		<!-- 导航栏 -->
		<navbar title="书城" 
		leftClick 
		@leftClick="$utils.navigateBack" 
		bgColor="#E3441A" 
		color="#fff" />

		<!-- 分类商品列表 -->
		<view class="category"
		>
			
			<view class="tabs">
				<uv-tabs 
				:list="category" 
				:activeStyle="{color : '#f00', fontWeight : 600}" 
				lineColor="#f00"
				:inactiveStyle="{color: 'rgba(0,0,0,.8)'}" 
				lineHeight="8rpx" 
				lineWidth="50rpx" 
				:current="current"
				@click="clickTabs"></uv-tabs>
			</view>
		
			<uv-vtabs 
			:list="category[current].children" 
			:current="currentChildren"
			keyName="name" 
			:chain="false"
			@change="change">
				<uv-vtabs-item>
					<view class="category-item">
						<view class="novel-list" v-if="categoryList.shopList.length > 0">
							<novelItem 
								v-for="(book, index) in categoryList.shopList" 
								:key="index" 
								:book="book">
							</novelItem>
						</view>
						<uv-empty v-if="categoryList.shopList.length == 0"  text="还没有呢"/>
					</view>
				</uv-vtabs-item>
			</uv-vtabs>
		</view>

		<!-- tabbar -->
		<tabber select="category" />
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	import tabber from '@/components/base/tabbar.vue'
	import novelItem from '@/components/novel/novelItem.vue'
	export default {
		// mixins: [mixinsList],
		components: {
			tabber,
			novelItem
		},
		data() {
			return {
				mixinsListApi: '',
				current : 0,
				currentChildren : 0,
				categoryList: {
					shopList: [
						{
							id: '1',
							title: '我是半妖',
							cover: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=facearea&w=400&h=400',
							author: '东方不败',
							desc: '这是一部关于半妖少年成长的玄幻小说，讲述了主角在人妖两界的冒险故事。',
							tags: ['玄幻', '冒险', '热血'],
							status: '连载中'
						},
						{
							id: '2',
							title: '兽王进化',
							cover: 'https://images.unsplash.com/photo-1519125323398-675f0ddb6308?auto=format&fit=facearea&w=400&h=400',
							author: '天下霸唱',
							desc: '一场意外让主角获得兽王血脉，开始了进化之路...',
							tags: ['奇幻', '冒险'],
							status: '连载中'
						},
						{
							id: '3',
							title: '魔法少女纯爷们',
							cover: 'https://images.unsplash.com/photo-1465101046530-73398c7f28ca?auto=format&fit=facearea&w=400&h=400',
							author: '南派三叔',
							desc: '一个普通男孩意外获得魔法少女的力量，开始了奇妙冒险...',
							tags: ['搞笑', '奇幻'],
							status: '已完结'
						}
					]
				},
				category: [
					{
						id: '1',
						name: '男生',
						children: [
							{ id: '101', name: '玄幻' },
							{ id: '102', name: '奇幻' },
							{ id: '103', name: '武侠' },
							{ id: '104', name: '仙侠' },
							{ id: '105', name: '都市' },
							{ id: '106', name: '历史' },
							{ id: '107', name: '军事' },
							{ id: '108', name: '游戏' },
							{ id: '109', name: '体育' }
						]
					},
					{
						id: '2',
						name: '女生',
						children: [
							{ id: '201', name: '古代言情' },
							{ id: '202', name: '现代言情' },
							{ id: '203', name: '幻想言情' },
							{ id: '204', name: '青春校园' },
							{ id: '205', name: '纯爱' }
						]
					},
					{
						id: '3',
						name: '出版',
						children: [
							{ id: '301', name: '文学' },
							{ id: '302', name: '传记' },
							{ id: '303', name: '励志' },
							{ id: '304', name: '名著' },
							{ id: '305', name: '经管' }
						]
					}
				]
			}
		},
		computed: {
		},
		onLoad({
			search,
			cid
		}) {
			if (search) {
				this.queryParams.title = search
			}
			
			// 初始化默认分类的书籍列表
			if (this.category.length > 0) {
				if (cid) {
					// 如果有指定分类ID，则显示对应分类
					let foundCategory = false
					this.category.forEach((n, i) => {
						if (n.id == cid) {
							this.current = i
							foundCategory = true
						}
					})
					
					if (foundCategory) {
						this.updateBookList(this.category[this.current].children[0].id)
					} else {
						// 如果未找到指定分类，显示第一个分类
						this.updateBookList(this.category[0].children[0].id)
					}
				} else {
					// 没有指定分类ID，显示第一个分类
					this.updateBookList(this.category[0].children[0].id)
				}
			}
			
			// this.$store.commit('getCategoryList')
			// if(this.category.length > 0 && cid){
			// 	this.category.forEach((n, i) => {
			// 		if(n.id == cid){
			// 			this.current = i
			// 		}
			// 	})
			// 	// this.queryParams.classId = cid
			// }else if (this.category.length > 0) {
			// 	// this.queryParams.classId = this.category[0].id
			// }
		},
		methods: {
			change(e) {
				// this.queryParams.classId = this.category[e].id
				this.currentChildren = e
				// 更新对应分类的书籍列表
				this.updateBookList(this.category[this.current].children[e].id)
			},
			clickTabs({index}){
				this.current = index
				this.currentChildren = 0
				// 更新对应分类的书籍列表
				this.updateBookList(this.category[index].children[0].id)
			},
			updateBookList(categoryId) {
				// 模拟获取不同分类的书籍数据
				// 实际项目中应该调用API获取数据
				console.log('获取分类ID为: ' + categoryId + ' 的书籍')
				
				// 这里只是简单模拟不同分类有不同数量的书
				const bookCount = Math.floor(Math.random() * 3) + 1
				const tempList = []
				
				for (let i = 0; i < bookCount; i++) {
					tempList.push({
						id: categoryId + '-' + i,
						title: '分类' + categoryId + '的书' + i,
						cover: 'https://images.unsplash.com/photo-1506744038136-46273834b3fb?auto=format&fit=facearea&w=400&h=400',
						author: '作者' + categoryId,
						desc: '这是分类' + categoryId + '的第' + i + '本书...',
						tags: ['分类' + categoryId, '热门'],
						status: i % 2 === 0 ? '连载中' : '已完结'
					})
				}
				
				this.categoryList.shopList = tempList
			},
			search(){
				for(let i = 0;i < 10;i++){
					delete this.queryParams[i]
				}
				this.queryParams.pageSize = 10
				this.getData()
			},
		}
	}
</script>

<style scoped lang="scss">
	.page {
		/deep/ .uv-vtabs {
			height: calc(100vh - 600rpx) !important;
		}

		/deep/ .uv-vtabs__bar {
			height: calc(100vh - 600rpx) !important;
		}

		/deep/ .uv-vtabs__content {
			height: calc(100vh - 600rpx) !important;
		}

		.search {
			position: relative;
			background: #FFFFFF;
			margin: 20rpx;
			border-radius: 41rpx;
			padding: 10rpx 20rpx;
			display: flex;
			align-items: center;

			/deep/ .uv-search__action {
				background-color: $uni-color;
				color: #FFFFFF;
				padding: 10rpx 20rpx;
				border-radius: 30rpx;
			}
		}

		&::v-deep .uv-vtabs__content {
			background: transparent !important;
			overflow: hidden;
		}
	}

	.category {
		font-size: 30rpx;
		color: #333;
		.category-title{
			position: relative;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 120rpx;
			
			&::before,
			&::after {
				position: absolute;
				top: 50%;
				content: '';
				width: 10%;
				border-top: 2rpx solid black;
			}
			
			&::before {
				left: 25%;
			}
			
			&::after {
				right: 25%;
			}
		}
		
		&::v-deep .uv-vtabs {
			width: 750rpx;
			overflow: hidden;
		}
		
		.list {
			width: 100%;
			padding: 0rpx 20rpx;
			box-sizing: border-box;
		}

		.category-item {
			padding: 0 20rpx;
			
			.novel-list {
				width: 100%;
				display: flex;
				flex-direction: column;
				gap: 10rpx;
			}
		}
	}
</style>