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

		<!-- 分类商品列表 -->
		<view class="category"
		>
			
			<view class="tabs">
				<uv-tabs 
				:list="category" 
				:activeStyle="{color : '#000', fontWeight : 600}" 
				lineColor="#000"
				:inactiveStyle="{color: '#000'}" 
				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="list.length > 0">
							<novelItem 
								v-for="(book, index) in list" 
								:key="index" 
								@click="navigateToDetail(book.id)"
								:book="book">
							</novelItem>
						</view>
						<uv-empty v-if="list.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
			}
			
			// 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()
			},
			clickTabs({index}){
				this.current = index
				this.currentChildren = 0
				// 更新对应分类的书籍列表
				this.getData()
			},
			search(){
				this.getData()
			},
			beforeGetData(){
				let data = {}
				
				if(this.categoryList[this.currentChildren].id){
					data.shopCion = this.categoryList[this.currentChildren].id
				}
				if(this.category[this.current].id){
					data.shopClass = this.category[this.current].id
				}
				
				return data
			},
            async getCategoryList() {
                // const data = await this.$fetch('getCategoryList')
                // this.category = data
				
				let [data1, data2] = await Promise.all([this.$fetch('getCategoryList'), this.$fetch('getBookAreaList')])
				this.category = [
					{
						title : '全部'
					},
					...data1,
				]
				this.categoryList = data2
				
				this.mixinsListApi = 'getRecommendList'
				
				this.queryParams.pageSize = 999999
				
				this.getData()
            },
            async getBookAreaList() {
                const data = await this.$fetch('getBookAreaList')
                this.categoryList = data
            },
			navigateToDetail(id) {
				// 跳转到小说详情页
				this.$utils.navigateTo(`/pages_order/novel/novelDetail?id=${id}`)
			},
		}
	}
</script>

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

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

		/deep/ .uv-vtabs__content {
			height: calc(100vh - 460rpx) !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>