<template>
	<view class="pages">
		
		<view class="search-wrap nomal-bottom">
			<view class="search">
				<view class="searchTxt">
					<uni-search-bar :placeholder="searchtxt" @confirm="search" @input="search" ></uni-search-bar>
				</view>
			</view>	
		</view>
		<!-- 搜索 结束 -->
		<mescroll-body class="wrap" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :down="downOption" :up="upOption">
			<view class="results write-radius">
				<view v-for="(item,index) in dataList" :key="index" @click="productdetails(item.id)" class="results-item">
					<view class="results-item-img-wrap"><image :src="$getimg+item.pic|miniImg(50)+'/thumbnail/358x358'" mode="aspectFit" class="results-item-img"></image></view>				
					<view class="results-item-info">
						<view class="results-item-info-til">{{item.name}}</view>
						<view class="results-item-info-brand">{{item.brandName}}</view>
						<!-- <view class="results-item-info-addr">{{item.placeOfProduction}}</view> -->						
						<view class="results-item-info-label">
							<text v-if="item.postage" class="postage">包邮</text>
						</view>
						<view class="results-item-info-company">{{item.umsCompanyInfo}}</view>
						<view class="results-item-info-price">
							<text class="rmb">¥</text>
							<text class="price">{{item.price}}</text>
							<text class="unit">/{{item.unit}}</text>
							<!-- <text class="minsell">4000斤起售</text> -->
						</view>						
					</view>
				</view>			
			</view>		 
		</mescroll-body>	
	</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 {
				searchtxt:'产品名称',
				params:{
					token:'',
					keyword:'',
					productCategoryId:'',
				},
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				// 下拉刷新的配置(可选, 绝大部分情况无需配置)
				downOption: { 
					// ...
				},
				// 上拉加载的配置(可选, 绝大部分情况无需配置)
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					noMoreSize: 5, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '暂无相关数据'
					}
				},
				// 列表数据
				dataList:[],
				
			}
		},
		onShow() {
			let serf = this;
			//检查登录,获取token
			// let loginRes = this.checkLogin('/pages/index/index', '2');
			// if(!loginRes){return false;}
			// serf.params.token=loginRes[0];			
		},
		onLoad(option) {
			
			if(option.productCategoryId){
				this.params.productCategoryId = option.productCategoryId;
				this.searchtxt = option.name;
			}
			if(option.keyword){
				this.params.keyword = option.keyword;
				this.searchtxt = option.keyword;
			}
			// console.log(this.params.keyword);

		},
		methods: {
			//搜索
			search(e) {
				console.log(e);
				this.params.keyword = e.value;
				this.params.productCategoryId = '';
				this.searchtxt = e.value;
				this.downCallback();
				// 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,{pageNum:pageNum,pageSize:pageSize});
				this.$api.http.get(this.config.apiBaseurl+'/product/search',{params: this.params}).then(data => {
					if(data.data.code=='1001'){
						// uni.redirectTo({
						// 	url:'/pages/login/login?backpage=/pages/index/index'+'&backtype='+2,
						// });							
					};
					console.log('data',JSON.parse(JSON.stringify(data)));
					// 接口返回的当前页数据列表 (数组)
					let curPageData = data.data.data.list;
					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.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); 
			
					// setTimeout(()=>{
					// 	this.mescroll.endSuccess(curPageLen)
					// },20)
				
				}).catch(err => {
					this.mescroll.endErr()
					console.log(err)
				
				});								
			},
			//跳转详情
			productdetails(id){
				uni.navigateTo({
					url:`/pages/product/product?id=${id}`
				})				
			},

		}
	}
</script>

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