<template>
	<view class="" v-if="isEmpty">
		<u-navbar
			:placeholder="true"
			title="品牌导览"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<u-empty mode="list" text="暂无数据" marginTop="200"></u-empty>
	</view>
	<view class="" v-else>
		<!-- <view class="" :style="{height: navHeight+'px' }"></view> -->
		<u-navbar
			:placeholder="true"
			title="品牌导览"
			:autoBack="true"
			 :safeAreaInsetTop="true"
		>
		</u-navbar>
		<view class="search-wrap">
			<u-search
				placeholder="请搜索" 
				:clearabled="true"
				:showAction="false"
				height="80rpx"
				@search="search"
				@custom="search"
				@clear="reloadList"
				bgColor="#fff"
				borderColor="#00A447"
				v-model="params.name">
			</u-search>
		</view>
		<view class="out-wrap u-flex u-col-top">
			<view class="floors">
				<view class="floor" 
				:class="{active:index==floorIndex}" 
				@click.stop="floorClick(index)" 
				v-for="(item,index) in floorTree" :key="item.parentId">
					<view class="floorName u-flex u-row-center">
						{{item.floorName}}
						<u-icon :name="index==floorIndex?'arrow-down-fill':'play-right-fill'" :customStyle="{'margin-left': '20rpx'}" color="#666" size="10"></u-icon>
					</view>
					<view v-if="index==floorIndex" class="children" @click.stop="childrenClick(i)" :class="{active:children.id==childrenId&&!params.name}" v-for="(children,i) in item.children" :key="children.id">
						{{children.floorName}}
					</view>
				</view>
			</view>
			<view class="right">
				<view class="tabs-wrap">
					<u-tabs 
					:list="tabsList"
					lineWidth="0"
					:current="tabsCurrent"
					:inactiveStyle="{background:'#F5F5F5','border-radius':'11px',padding:'10rpx 22rpx','font-size':'22rpx'}"
					:activeStyle="{background:'rgba(0,164,71,0.1)',color:'#00A447','border-radius':'11px',padding:'10rpx 22rpx','font-size':'22rpx'}"
					@click="tabsClick"></u-tabs>
				</view>
				<!-- <view class="second-types">
					<view class="u-flex u-row-between u-col-top  u-border-bottom">
						<view class="u-flex u-flex-wrap">
							<view class="type" 
							:class="{active:index==secondTypesIndex}" 
							@click="secondTypesClick(index)" 
							v-for="(item,index) in subTypeList" :key="item.id">
								{{item.name}}
							</view>
						</view>
						<u-icon 
							@click="showMoreSecondTypes"
							 v-if="subTypeListMore.length>0"
							size="24px" 
							:name="showMoreSecondTypesIcon"></u-icon>
					</view>
					<view class="more-second-types u-flex" v-if="moreSecondTypes">
						<view class="type" 
						:class="{active:index==moreSecondTypesIndex}" 
						@click="moreSecondTypesClick(index)" 
						v-for="(item,index) in subTypeListMore" :key="item.id">
							{{item.name}}
						</view>
					</view>
				</view> -->
				<mescroll-body class="mescroll-body" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
					<view class="page-wrap" @click="$u.route('/brand/branddetails',{id:item.id})" v-for="item in dataList" :key="item.id">
						<view class="brand-list">
							<view class="brand u-flex">
								<u--image :showLoading="true" :src="item.logo" width="145rpx" height="145rpx"></u--image>
								<view class="text">
									<view class="name u-line-1">{{item.name}}</view>
									<view class="typeName"> <text class="type">{{item.typeName}}</text> </view>
									<view class="businessTime">营业时间：{{item.businessTime}}</view>
								</view>
							</view>
						</view>
					</view>
				</mescroll-body>
			</view>
		</view>
		<!-- <tabbar :tabbarIndexProps="Number(1)" /> -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import { systemInfo } from "@/mixin.js";
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import tabbar from "../components/tabbar.vue";
	export default {
		mixins: [MescrollMixin,systemInfo], // 使用mixin
		components: {
			tabbar
		},
		data() {
			return {
				isEmpty:false,
				staticUrl:this.$commonConfig.staticUrl,
				floorIndex:0,
				secondTypesIndex:0,
				moreSecondTypesIndex:null,
				moreSecondTypes:false,
				showMoreSecondTypesIcon:'list',
				tabsList:[{name:'全部',id:''}],
				downOption: {},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				//菜单
				floorTree:[],
				childrenIndex:0,
				childrenId:undefined,
				//子菜单
				subTypeList:[],
				subTypeListMore:[],
				// 列表数据
				dataList: [],
				params:{
					floorId:'',//楼层id
					typeName:'',//品牌类型名称
					name:'',//品牌名称
				},
				tabsCurrent:0,
			}
		},
		watch:{
			floorIndex:{
				handler(val){
					// console.log('floorIndex',val);
					this.params.name = '' ;
					this.childrenIndex = 0;
					this.mescroll.resetUpScroll();
				},
				immediate:false
			}
		},
		onLoad() {
			this.getSystemInfo();
		},
		onShow() {
			this.selectBrandFloorTree();
			this.brandCategoryType();
		},
		methods: {
			/*下拉刷新的回调, 重置列表为第一页 (此处可删,mixins已默认)
			downCallback(){
				this.mescroll.resetUpScroll();
			},
			/*上拉加载的回调*/
			upCallback(page) {
				// 此处可以继续请求其他接口
				// if(page.num == 1){
				// 	// 请求其他接口...
				// }
				let childrenArr = this.floorTree[this.floorIndex]?.children||[];
				this.childrenId = childrenArr[this.childrenIndex]?.id;
				// console.log('childrenId',this.childrenId);
				this.params.floorId = this.childrenId;
				// console.log('floorId',this.params.floorId);
				
				this.params.typeName = this.tabsList[this.tabsCurrent].name;
				if(this.params.typeName=='全部'){this.params.typeName=''}
				
				// 如果希望先请求其他接口,再触发upCallback,可参考以下写法
				if(!this.params.floorId){
					this.mescroll.endErr();
					this.isEmpty = true;
					// this.selectBrandFloorTree();
					return // 此处return,先获取xx
				}
				if(this.params.name){
					this.params.floorId = ''
				}
				// console.log('this.params.floorId',this.params.floorId);

				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				this.params = Object.assign(this.params,{pageNum:pageNum,pageSize:pageSize});
				this.$u.api.brandList(this.params).then(data => {
					// console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.rows;
					// console.log('curPageData',JSON.parse(JSON.stringify(curPageData)));
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					// let totalPage =  data.data.data.totalPage; 
					// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
					let totalSize = data.data.total; 
					// 接口返回的是否有下一页 (true/false)
					// let hasNext = data.xxx; 
					// console.log('totalPage',totalPage,'curPageLen',curPageLen);
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
					this.dataList = this.dataList.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					//方法一(推荐): 后台接口有返回列表的总页数 totalPage
					// this.mescroll.endByPage(curPageLen, totalPage); 
					//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
					this.mescroll.endBySize(curPageLen, totalSize); 
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				});	

			},
			/*若希望重新加载列表,只需调用此方法即可(内部会自动page.num=1,再主动触发up.callback)*/
			reloadList() {
				this.mescroll.resetUpScroll();
			},
			search(e){
				// this.floorIndex = '';
				this.tabsCurrent = 0;
				this.reloadList();
			},
			selectBrandFloorTree(){
				this.$u.api.selectBrandFloorTree().then(res=>{
					this.floorTree = res.data;
					if(res.data.length>0){
						this.isEmpty = false;
					}
					this.mescroll.resetUpScroll();
				}).catch(err=>{
					this.mescroll.endErr()
					console.log('selectBrandFloorTree',err);
				})				
			},
			brandCategoryType(){
				this.$u.api.brandCategoryType().then(res=>{
					let data = res.data.map(item=>{
						return {name:item.dictLabel,id:item.dictValue}
					});
					this.tabsList = data;
					this.tabsList.unshift({name:'全部',id:''});
					this
				}).catch(err=>{
					this.mescroll.endErr()
					console.log('brandCategoryType',err);
				})				
			},
			floorClick(index){
				let childrenArr = this.floorTree[index]?.children||[];
				if(childrenArr.length<1){
					uni.$u.toast('该区域暂无品牌信息')
					return
				}
				this.floorIndex = index;
			},
			tabsClick(e){
				// console.log('tabsClick',e);
				this.tabsCurrent = e.index;
				this.reloadList();
			},
			secondTypesClick(index){
				this.secondTypesIndex = index;
				this.moreSecondTypesIndex = null;
				this.mescroll.resetUpScroll();
				
			},
			childrenClick(i){
				if(!this.floorIndex){
					this.floorIndex = 0
				}
				// console.log('children',this.floorTree[this.floorIndex].children[i]);
				let children = this.floorTree[this.floorIndex].children[i];
				// console.log('childrenClick',children);
				// this.floorIndex = children.parentId;
				// console.log('parentId',children.parentId);
				this.childrenIndex = i;
				this.params.name = '' ;
				this.tabsCurrent = 0;
				this.reloadList()
			}
		}
	}
</script>
<style>
page{
	/* padding-top: 0; */
}
</style>
<style lang="scss" scoped>
.out-wrap{
	background-color: #F5F5F5;
	.right{
		background-color: #fff;
		width: calc( 100vw - 200rpx );
		// flex: 1;
	}
}
.tabs-wrap{
	/deep/ .u-tabs__wrapper__nav__item{
		padding: 0 10rpx;
	}
}
.floors{
	width: 200rpx;
	font-size: 26rpx;
	text-align: center;
	.floor{
		position: relative;
		padding: 25rpx 0;
		&.active{
			background-color: #fff;
		}
		.floorName{
			padding: 27rpx 0;
			font-weight: 600;
		}
		.children{
			padding: 27rpx 0;
			&.active{
				background: rgba(0,164,71,0.1);
				color: #00A447;
			}
		}
	}
}
.brand-list{
	.brand{
		border-bottom: 0.5px solid #eee;
		padding-bottom: 30rpx;
		margin-bottom: 30rpx;
		.text{
			margin-left: 20rpx;
			font-size: 22rpx;
			color: #999999;
			line-height: 30rpx;
			.name{
				font-size: 30rpx;
				font-weight: 600;
				color: #333333;
				line-height: 42rpx;
				margin-bottom: 20rpx;
			}
			.typeName{
				margin-bottom: 20rpx;
				.type{
					background: rgba(0,164,71,0.1);
					border-radius: 4rpx;
					font-size: 24rpx;
					color: #00A447;
					padding: 8rpx 16rpx;
					margin-left: 0;
				}
			}
		}
	}
}

.mescroll-body{
	background-color: #fff;
}
</style>