<template>
	<view>
		<view class="list-box">
			<view class="lf-row-between list-item" v-for="(item, index) in list" :key="item.id">
				<image class="goods-img" mode="aspectFill" :src="item.agent_product.picture" @click="enterDetail(item.agent_product_id)"></image>
				<view style="width: 458rpx;">
					<view class="lf-font-28 lf-line-2" style="height: 80rpx;" @click="enterDetail(item.id)">{{ item.agent_product.title }}</view>
					<view class="lf-m-t-15 lf-font-24 lf-color-gray">{{ item.created_at || '' }}</view>
					
					<view class="lf-row-between lf-m-t-20">
						<lf-price price="3599.00"></lf-price>
						<view class="lf-row-center collect-btn" @click="switchCollect(index,item.is_collect)" :class="{'cancel': !item.is_collect}">
							<text class="lf-iconfont lf-icon-dui1 lf-m-r-1" v-if="item.is_collect"></text>
							<text>{{ !item.is_collect ? '收藏' : '已收藏' }}</text>
						</view>
					</view>
					
				</view>
			</view>
		</view>
		<!-- 加载 -->
		<view class="loading-more">
			<text v-if="list.length" :class="{'loading-more-text': loadingClass}">{{ loadingText }}</text>
			<lf-nocontent v-else></lf-nocontent>
		</view>
		<!-- 回到顶部 -->
		<u-back-top :scroll-top="pageScrollTop" :custom-style="{background: 'rgba(51, 51 51, 0.3)'}"></u-back-top>
	</view>
</template>

<script>
	export default {
		data(){
			return {
				list: [],
				loadingClass: true,
				loadingText: '正在加载中',
				page: 1,
				isPage: true,
				pageSize: 20,
				skeletonLoading: true
			}
		},
		onLoad(){
			this.getCollectList();
		},
		methods: {
			getCollectList(){
				this.$http(this.API.API_COLLECT_LIST,{page: this.page}).then(res => {
					this.skeletonLoading = false;
					let isPage = res.data.next_page_url == null?false:true;
					this.isPage = isPage;
					console.log(res)
					// let list = res.data.map(item => {
					// 	item.is_collect = true; // 默认都收藏了
					// 	return item;
					// });
					let list = res.data.data
					res.data.data.forEach((item,index) => {
						this.$set(list[index],'is_collect',true)
					})
					if(!isPage){
						this.loadingClass = false;
						this.loadingText = '已显示全部数据~';
					}
					if(this.page == 1){
						this.list = list;
					}else{
						this.list.push(...list);
					}
					
					console.log('列表',this.list)
				}).catch(err => {
					this.skeletonLoading = false;
				})
			},
			// 切换收藏状态
			switchCollect(index,if_collect){
				let goods_id = this.list[index].agent_product_id;
				if(if_collect) {
					this.$http(this.API.API_DELCOLLECT, {agent_product_id:goods_id}).then(res => {
						this.$msg('取消收藏成功!')
						console.log(res)
						this.list[index].is_collect = false;
					})
				}else {
					this.$http(this.API.API_ADDCOLLECT, {agent_product_id:goods_id}).then(res => {
						this.$msg('添加收藏成功!')
						console.log(res)
						this.list[index].is_collect = true;
					})
				}
				
			},
			// 进入商品详情页
			enterDetail(id){
				this.$url('/pages/goodsDetail/index?goods_id='+ id);
			}
		},
		onReachBottom(){
			if(this.isPage){
				this.page = this.page + 1;
				this.getCollectList();
			}
		},
		onPullDownRefresh(){
			this.page = 1;
			this.isPage = true;
			this.loadingClass = true;
			this.loadingText = '正在加载中';
			this.getCollectList();
			uni.stopPullDownRefresh();
		}
	}
</script>

<style lang="scss" scoped="scoped">
	.list-box{
		width: 750rpx;
		height: auto;
		padding: 0 32rpx;
		box-sizing: border-box;
		.list-item{
			border-bottom: 1rpx solid #EEEEEE;
			padding: 30rpx 0;
			align-items: flex-start;
			.goods-img{
				width: 210rpx;
				height: 210rpx;
				border-radius: 20rpx;
				background-color: #EEEEEE;
			}
			.shop-img{
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
			}
			.shop-name{
				width: 305rpx; 
				color: #555555;
			}
		}
	}
	.collect-btn{
		width: 155rpx;
		height: 63rpx;
		border-radius: 32rpx;
		border: 2rpx solid #1998FE;
		// text-align: center;
		// line-height: 63rpx;
		color: #1998FE;
		font-size: 24rpx;
	}
	.cancel{
		border: 2rpx solid #555555;
		color: #555555;
	}
</style>
