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

		<!-- 分类商品列表 -->
		<view class="category"
		>
			
			<view class="tabs">
				<uv-tabs 
				:list="category" 
				:activeStyle="{color : '#0A2463', fontWeight : 600}" 
				lineColor="#0A2463"
				:inactiveStyle="{color: 'rgba(0,0,0,.8)'}" 
				lineHeight="8rpx" 
				lineWidth="50rpx" 
				:current="current"
                keyName="title"
				@click="clickTabs"></uv-tabs>
			</view>
		
			<uv-vtabs 
			:list="categoryList" 
			:current="currentChildren"
			keyName="title" 
			:chain="false"
            :barItemActiveLineStyle="{background : '#0A2463'}"
            :barItemActiveStyle="{color : '#0A2463', fontWeight : 600}"
			@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'
    import mixinsList from '@/mixins/list.js'
	export default {
		mixins: [mixinsList],
		components: {
			tabber,
			novelItem
		},
		data() {
			return {
				mixinsListApi: '',
				current : 0,
				currentChildren : 0,
				categoryList: [],
				category: [],
			}
		},
		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
			// }
		},
        onShow() {
            this.getCategoryList()
            this.getBookAreaList()
        },
		methods: {
			change(e) {
				// this.queryParams.classId = this.category[e].id
				this.currentChildren = e
				// 更新对应分类的书籍列表
				this.getData(this.category[this.current].children[e].id)
			},
			clickTabs({index}){
				this.current = index
				this.currentChildren = 0
				// 更新对应分类的书籍列表
				this.getData(this.category[index].children[0].id)
			},
			search(){
				this.getData()
			},
            async getCategoryList() {
                const data = await this.$fetch('getCategoryList')
                this.category = data
            },
            async getBookAreaList() {
                const data = await this.$fetch('getBookAreaList')
                this.categoryList = data
            },
		}
	}
</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>