<template>
	<view>
		<lf-nav title="兑换记录" :showIcon="true" bgColor="#fff"></lf-nav>
		<view class="head">
			<u-search placeholder="搜你想要的" v-model="value" @custom="onSearch" @search="onSearch" @clear="clearSearch"></u-search>
		</view>
		<scroll-view :style="{height: autoHeight}" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="isRefresher"
			@scrolltolower="onScrolltolower" @refresherrefresh="onRefresherrefresh" >
			<view class="content">
				<view class="card" v-for="(item, index) in list" :key="index" @click="$url('/pages/order/newdetail/newdetail?type=point&order_id='+item.order_no)">
					<view>
						<text class="lf-iconfont icon-Group- lf-font-30"></text>
						<text class="shop-name">{{item.brand.name}}</text>
						<text class="lf-iconfont icon-xiangyou lf-font-24"></text>
					</view>
					<view class="lf-flex lf-m-t-20" v-for="(item2,index2) of item.items" :key="index2">
						<image class="goods-img" :src="item2.item_meta.image"></image>
						<view class="info">
							<view class="lf-font-26 lf-color-333 lf-line-2">{{item2.item_name}}</view>
							<view class="lf-row-between" style="line-height: 1;">
								<text class="lf-font-24 lf-color-777">{{item2.item_meta.specs_text}}</text>
								<text class="lf-font-32 lf-color-primary lf-font-bold">{{item2.redeem_point}}积分</text>
							</view>
						</view>
					</view>
					<view class="order-num">
						<text>{{item.status_text}}</text>
						<!-- <text style="color: #F63434;">删除订单</text> -->
					</view>
				</view>
				<!-- 空数据的情况 -->
				<view class="loading-more">
					<text v-if="list.length != 0"
						:class="{'loading-more-text': loadingClass}">{{ loadingText }}</text>
					<lf-nocontent src="/static/images/empty.png" v-else></lf-nocontent>
				</view>
			</view>
		</scroll-view>
		<u-back-top :scrollTop="pageScrollTop"></u-back-top>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				value: '',
				list: [],
				page: 1,
				isPage: true,
				loadingClass: true,
				loadingText: '正在加载中',
				scrollH: 0,
				nav_height: 0,
				isRefresher: true,
				pageSize: 10,
				is_search: false // 当前是否处于搜索中状态
			}
		},
		computed: {
			autoHeight(){
				return `calc(${this.scrollH}px - ${this.nav_height}px - 180rpx - 120rpx)`;
			}
		},
		onLoad(){
			let info = uni.getSystemInfoSync();
			this.scrollH = info.screenHeight;
			this.getPointOrder()
		},
		methods: {
			// 页面触底，加载下一页
			onScrolltolower(){
				if(this.isPage){
					this.page = this.page + 1;
					this.getPointOrder();
				}
			},
			// 下拉刷新处理
			refreshFn(options){
				this.page = 1;
				this.isPage = true;
				this.loadingClass = true;
				this.list = []
				this.loadingText = '正在加载中';
				this.getPointOrder(options);
			},
			// scroll-view 下拉刷新
			onRefresherrefresh(){
				this.isRefresher = true;
				this.refreshFn({type: 'scrollRefresh'});
			},
			// 触发搜索
			onSearch(event){
				console.log(event)
				if(event){
					this.is_search = true;
				}else{
					this.is_search = false;
				}
				this.page = 1;
				this.isPage = true;
				this.loadingClass = true;
				this.list = []
				this.loadingText = '正在加载中';
				this.getPointOrder();
			},
			// 清除搜索内容
			clearSearch(){
				if(this.is_search){
					this.onSearch(false);
				}
			},
			getPointOrder(options = {}){
				let par = {
					page: this.page
				}
				if(this.is_search && this.value){
					par.keyword = this.value;
				}
				
				this.$http.get({
					api: 'api/order/point/list',
					header: {
					    Authorization: this.$cookieStorage.get('user_token')
					},
					data: par
				}).then(res => {
					console.log("----", res);
					let isPage = this.page < res.data.meta.pagination.total_pages?true:false;
					this.isPage = isPage;
					if(!isPage) {
						this.loadingClass = false;
						this.loadingText = '没有更多数据啦~';
					}
					if(options.type == 'pageRefresh') {
						uni.stopPullDownRefresh();
					}else if(options.type == 'scrollRefresh') {
						this.isRefresher = false;
					}
					if(this.page == 1) {
						this.list = res.data.data;
					}else {
						this.list.push(...res.data.data);
					}
					
				})
			}
		}
	}
</script>

<style>
	page{
		background-color: #F8F8F8;
	}
</style>
<style lang="scss" scoped="scoped">
	.head{
		padding: 30rpx 32rpx;
		background-color: #FFFFFF;
	}
	.content{
		padding: 30rpx 32rpx;
		.card{
			width: 686rpx;
			height: 323rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			&:nth-child(n+2){
				margin-top: 20rpx;
			}
			.shop-name{
				font-size: 28rpx;
				color: #222222;
				font-weight: bold;
				margin: 0 15rpx;
			}
			.goods-img{
				width: 130rpx;
				height: 130rpx;
				border-radius: 5rpx;
				margin-right: 15rpx;
				background-color: #EEEEEE;
			}
			.info{
				width: 480rpx;
				height: 130rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}
			.order-num{
				font-size: 24rpx;
				color: #555555;
				display: flex;
				justify-content: space-between;
				margin-top: 28rpx;
			}
		}
	}
</style>
