<template>
	<view class="pages producttype-wrap">
		<view class="producttype fix-content">
			<view class="producttype-nav">
				<view v-for="(item,index) in navitem" :key="index" @click="typeclick(index)" :class="{active:index==ins}" class="producttype-nav-item">{{ item }}</view>
			</view>
			<view class="producttype-con">
				<view class="search">
					<view class="searchTxt">
						<uni-search-bar placeholder="产品名称" @confirm="search" @input="search" ></uni-search-bar>
					</view>
				</view>
				<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
					<view class="search-typelist">
						<view v-for="(item,index) in typelist" :key="index" @click="publishtype(item.name)" class="search-typelist-item">
							<view class="search-typelist-item-serial">{{ index +1 }}</view>
							<view class="search-typelist-item-name">{{item.name}}</view>
						</view>					
					</view>	 
				</mescroll-body>				
			</view>
			<!-- producttype-con end -->
		</view>
		<!-- fix-content end -->
		
	</view>
</template>

<script>
	// 引入mescroll-mixins.js
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";	
	// 引入mescroll-body组件 (如已在main.js注册全局组件,则省略此步骤)
	import MescrollBody from "@/components/mescroll-uni/mescroll-body.vue"; // 注意.vue后缀不能省
	
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
	export default {
		mixins: [MescrollMixin], // 使用mixin
		components: {
			MescrollBody,
			uniSearchBar,
			
		
		},
		data() {
			return {
				params:{
					token:'',
					product:'',
				},
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: { 
					// ...
				},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				//nav active
				ins:0,
				//菜单类别
				navitem:['竹','油茶','花椒','皂角','刺梨','无患子','核桃','石斛','茶','林下食用菌','鲜果','林下肉蛋禽','板栗','林下蜂蜜','林下中药材'],
				//热门列表
				// hotlist:[
				// ],
				//typelist
				typelist:[
					{name:'松树'},
					{name:'枫树'},
					{name:'柏树'},
					{name:'紫薇'},
					{name:'槐树'},
					{name:'女贞'},
				],
				//显示的分类标题
				nowtype:'',
				
				
				
			}
		},
		onShow() {
			let serf = this;
			//检查登录,获取token
			// let loginRes = this.checkLogin('/pages/index/index', '2');
			// if(!loginRes){return false;}
			// serf.params.token=loginRes[0];			
		},
		onLoad() {
			this.nowtype = this.navitem[this.ins];
			this.params.product = this.nowtype;
		},
		methods: {
			//搜索
			search(e) {
				console.log(e);
				// this.poorList = [];
				// this.params.farmerName = e.value;
				// this.pagination = Object.assign(this.pagination,{"farmerName":e.value});
				// this.pagination.pageNo = 1;
				// this.getPoorList(this.pagination)
			},
			
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			downCallback(){
				this.mescroll.resetUpScroll(); // 重置列表为第一页 (自动执行 page.num=1, 再触发upCallback方法 )
			},
			/*上拉加载的回调*/
			upCallback(page) {
				let pageNum = page.num; // 页码, 默认从1开始
				let pageSize = page.size; // 页长, 默认每页10条
				this.params = Object.assign(this.params,{page:pageNum,rows:pageSize});
				this.$api.http.post(this.config.apiBaseurl+'hotel/reserve/data',this.params,{
					header: {
						Accept:'application/json',
						Authorization: 'Bearer '+ this.params.token, //注意Bearer后面有一空格
					}
				}).then(data => {
					if(data.data.code=='1001'){
						// uni.redirectTo({
						// 	url:'/pages/login/login?backpage=/pages/index/index'+'&backtype='+2,
						// });							
					};
					console.log(data);
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.rows; 
					console.log('curPageData',curPageData);
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					// let totalPage = data.xxx; 
					// 接口返回的总数据量(如列表有26个数据,每页10条,共3页; 则totalSize=26)
					let totalSize = data.data.total; 
					// 接口返回的是否有下一页 (true/false)
					// let hasNext = data.xxx; 
					
					//设置列表数据
					if(page.num == 1) this.dataList = []; //如果是第一页需手动置空列表
					this.dataList = this.dataList.concat(curPageData); //追加新数据
					
					//方法二(推荐): 后台接口有返回列表的总数据量 totalSize
					this.mescroll.endBySize(curPageLen, totalSize); 
			
					// setTimeout(()=>{
					// 	this.mescroll.endSuccess(curPageLen)
					// },20)
				
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				
				});								
			},
			
			//左侧导航点击
			typeclick(num){
				this.ins=num;
				this.nowtype = this.navitem[num];
				this.params.product = this.nowtype;
				this.downCallback();
			},
			//产品点击
			publishtype(product){
				uni.navigateTo({
					url:`/pages/publish/publish?type=${product}`,					
				})				
			},

		}
	}
</script>

<style scoped>
	@import url("./publishtype.css");
</style>
