<template>
	<view id="category" :style="{paddingTop:CustomBar+'px'}">
		<!--顶部搜索导航栏-->
		<view class="'search-input-fixed" :style="{top:CustomBar/2+'px'}">
			<cat-search :systeminfo='systeminfo' :navbar-data='nvabarData' :headerBtnPosi ="headerBtnPosi" @click="hanldTabClick"></cat-search>
		</view>
		<view class="category-list">
			<!-- 左侧分类导航 -->
			<scroll-view class="left-aside" scroll-y="true" scroll-with-animation :scroll-top="verticalNavTop" :style="{top:(CustomBar+38)+'px',height:(windowHeight-CustomBar+38)+'px'}">
				<view class="row" v-for="(m, index) in categoryList" :key="index" :class="[index==showCategoryIndex?'on':'']" @tap="showCategory($event,index)" :data-id="index">
					<view class="text">{{m.name}}</view>
					<view class="block"></view>
				</view>
			</scroll-view>
			<!--右侧子导航-->
			<scroll-view  scroll-y="true" class="right-aside" :style="{top:(CustomBar+38)+'px',height:(windowHeight-CustomBar+38)+'px'}">
				<view class="category" v-for="(first,index) in categoryList" :key="index" v-show="index==showCategoryIndex" >
					<template v-if="first.smallTypeList && first.smallTypeList.length > 0">
						<view class="category-box AAA" v-for="(small,oIndex) in first.smallTypeList" :key="oIndex">
							<view class="title">{{small.name}}</view>
							<view class="list" v-if="small.tinyTypeList && small.tinyTypeList.length > 0">
								<view class="box" v-for="(pro,proIndex) in small.tinyTypeList" :key="proIndex" @click.stop="navToListPage(pro,3)">
									<image :src="pro.crmIcon ? pro.crmIcon : 'https://static.caimei365.com/app/img/icon/icon-noneproduct.jpg'"></image>
									<view class="text">{{pro.name}}</view>
								</view>
							</view>
							<view v-else class="no-data">
								<view class="box" @click.stop="navToListPage(small,2)">
									<image :src="small.crmIcon ? small.crmIcon : 'https://static.caimei365.com/app/img/icon/icon-noneproduct.jpg'"></image>
									<view class="text">全部商品</view>
								</view>
							</view>
						</view>
					</template>
					<template v-else>
						<view class="category-box BBB">
							<view class="title">{{first.name}}</view>
							<view class="no-data">
								<view class="box" @click.stop="navToListPage(first,1)">
									<image :src="first.crmIcon ? first.crmIcon : 'https://static.caimei365.com/app/img/icon/icon-noneproduct.jpg'"></image>
									<view class="text">全部商品a</view>
								</view>
							</view>
						</view>
					</template>
				</view>
			</scroll-view>
		</view>
	</view>
</template>
<script>
	import catSearch from '@/components/uni-search/cat-search.vue'
	export default {
		components: {
			catSearch
		},
		data() {
			return {
				nvabarData: {							 //顶部自定义导航
					showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
					title: ''		// 导航栏 中间的标题
				},
				CustomBar:this.CustomBar,// 顶部导航栏高度
				headerBtnPosi:	this.setHeaderBtnPosi(), //获取设备顶部胶囊高度
				systeminfo: this.setSysteminfo(),		 //获取设备信息
				headerShow:true,
				showCategoryIndex: 0,
				categoryList: [],//分类列表
				search: '',
				cateTop: {},
				verticalNavTop: 0,
				categoryTabIndex:1,
				windowHeight:0,
				scrollHeight:0,
			}
		},
		onLoad() {
			this.initData();
		},
		methods: {
			initData() {// 数据初始化
				this.GetProductCate(this.categoryTabIndex);
				const {windowHeight, pixelRatio} = wx.getSystemInfoSync();
				this.windowHeight = windowHeight - 1;
				this.scrollHeight = windowHeight - 1;
			},
			hanldTabClick(data){
				this.categoryList = []
				this.GetProductCate(data);
			},
			GetProductCate (index) {// 获取商品分类列表
				this.CommonService.GetFirstClassly({typeSort:index,source :'crm'}).then(response =>{
					this.categoryList = response.data
					// 查询第一个拥有二级菜单的子菜单
					for (let i = 0; i < this.categoryList.length; i++) {
						if (this.categoryList[i].smallTypeList.length > 0) {
							this.showCategoryIndex = i;
							break;
						}
					}
				}).catch(error =>{
					this.$util.msg(error.msg,2000)
				})
			},
			showCategory(e,index){//分类切换显示
				this.showCategoryIndex = index;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			toSearch () {// 跳转至搜索详情页
				uni.navigateTo({
					url: `/pages/search/search?search=${JSON.stringify(this.search)}`
				})
			},
			navToListPage(item,index){//分类导航跳转
				switch(index){
					case 1:
						this.$api.navigateTo(`/pages/goods/goods-classify?classType=1&id=${item.bigTypeId}&title=${item.name}`)
						break;
					case 2:
						this.$api.navigateTo(`/pages/goods/goods-classify?classType=2&id=${item.smallTypeId}&title=${item.name}`)
						break;
					case 3:
						this.$api.navigateTo(`/pages/goods/goods-classify?classType=3&id=${item.tinyTypeId}&title=${item.name}`)
						break;
				}
			},
			setHeaderBtnPosi(){
				// 获得胶囊按钮位置信息
				let headerBtnPosi = uni.getMenuButtonBoundingClientRect();
				return headerBtnPosi
			},
			setSysteminfo(){
				let systeminfo;
				uni.getSystemInfo({ // 获取设备信息
					success: (res) => {
						systeminfo = res
					},
				})
				return systeminfo
			},
		}
	}
</script>
<style scoped lang="scss">
	page {
		background-color: #fff;
	}
	.search-input-fixed{
		width: 100%;
		height:auto;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1000;
		background: #FFFFFF;
		border-bottom: 1px solid #F7F7F7;
	}		
	#category {
		/*模块分类*/
		.category-list{
			width: 100%;
			background-color: #fff;
			display: flex;
			.left-aside,.right-aside{
				position: absolute;
				top: 100rpx;
				/*  #ifdef  APP-PLUS  */
				top: calc(100rpx + var(--status-bar-height));
				/*  #endif  */
				bottom: 0rpx;
			}
			.left-aside{
				width: 200rpx;
				left: 0rpx;
				background-color: #f2f2f2;
				.row{
					width: 100%;
					height: 100rpx;
					display: flex;
					align-items: center;
					position: relative;
					.text{
						width: 100%;
						position: relative;
						font-size: $font-size-28;
						display: flex;
						justify-content: center;
						color: $text-color;
						/* transition: transform 0.3s ease;*/
						transition-property: transform;
						transition-duration: 0.2s;
						transition-timing-function: ease;
					}
					.block{
						position: absolute;
						width: 0rpx;
						left: 0;
						/* transition: transform 0.3s ease;*/
						transition-property: transform;
						transition-duration: 0.2s;
						transition-timing-function: ease;
					}
					&.on{
						height: 100rpx;
						background-color: #fff;
						.text{
							font-size: $font-size-28;
							color: $color-system;
						}
						.block{
							width: 10rpx;
							height: 60rpx;
							top: 20rpx;
							background-color: $color-system;
							border-radius: 0 15rpx 15rpx 0;
						}
					}
				}
			}
			.right-aside{
				width: 550rpx;
				left: 200rpx;
				.category{
					width: calc(100%);
					padding: 0 15rpx 20rpx 0;
					background: #F7F7F7;
					.category-box{
						background: #FFFFFF;
						margin-bottom: 20rpx;
						.title{
							padding: 0 24rpx;
							line-height: 80rpx;
							height: 80rpx;
							text-align: left;
							color: $text-color;
							font-size: $font-size-26;
							border-bottom: 1px solid #F7F7F7;
							font-weight: 600;
						}
						.list{
							margin-top: 24rpx;
							width: 100%;
							display: flex;
							flex-wrap: wrap;
							.box{
								width: calc(71.44vw / 3);
								margin-bottom: 40rpx;
								display: flex;
								justify-content: center;
								align-items: center;
								flex-wrap: wrap;
								image{
									width: 140rpx;
									height: 140rpx;
								}
								.text{
									margin-top: 8rpx;
									width: 100%;
									display: flex;
									justify-content: center;
									font-size: $font-size-24;
								}
							}
						}
						.no-data {
							text-align: center;
							margin: 30rpx 0;
							color: #666666;
							font-size: 24rpx;
							line-height: 80rpx;
							.box{
								width: calc(71.44vw / 3);
								margin-bottom: 40rpx;
								display: flex;
								justify-content: center;
								align-items: center;
								flex-wrap: wrap;
								image{
									width: 140rpx;
									height: 140rpx;
								}
								.text{
									margin-top: 8rpx;
									width: 100%;
									display: flex;
									justify-content: center;
									font-size: $font-size-24;
								}
							}
						}
					}
				}
			}
		}
	}
</style>
