<template>
	<view class="app-container">
		<view class="list-container">
			<block v-for="(item, index) in dataList" :key='index'>
				<view class="item" @tap="toGoodsDetail(item.goodsId)" @longpress="handleDelete(item.goodsFavoriteId)">
					<image :src="item.imgUrl" mode="aspectFill"></image>
					<view class="content">
						<view class="title ellipsis-2">{{item.goodsName}}</view>
						<view class="bottom">
							<view class="price">
								<view class="price-1">¥{{item.goodsPrice | numToFixed}}</view>
								<view class="price-2">¥{{item.orgGoodsPrice | numToFixed}}</view>
							</view>
							<view class="btn"><image src="@/static/icon/cart.png" mode="aspectFill"></image></view>
						</view>
					</view>
				</view>
			</block>
		</view>
		<no-data v-if="!dataList.length" :showText="'暂无收藏商品'"></no-data>
		<loading-text v-if="dataList.length"  :loading="loading" :noMore="noMore" ></loading-text>
		
		<modal-dialog showText="确定要取消收藏吗？" :isShowDialog="isDialog" @cancel="isDialog = false" @confirm="confirmDelete"></modal-dialog>
		
		<drag-button :isDock="true" :customBar="true" ref="dragButton"></drag-button>
	</view>
</template>

<script>
	import {mapState} from 'vuex';
	import modalDialog from '@/components/modalDialog.vue';
	import dragButton from '@/components/drag-button.vue';
	
	export default {
		components:{
			modalDialog,
			dragButton
		},
		data() {
			return {
				dataList: [],
				pageNum: 1,
				pageSize: 8,
				noMore: false,
				loading: false,
				deleteId: null,
				isDialog: false,
			}
		},
		
		computed:{
			...mapState(['userInfo', 'isLogin', 'userId'])
		},
		
		onShow() {
			this.$refs.dragButton.init();
		},
		
		onLoad() {
			this.getGoodsList();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.pageNum = 1;
			this.getGoodsList();
		},
		
		// 上拉加载
		onReachBottom() {
			this.getGoodsList(1);
		},
		
		methods: {
			// 获取商品列表
			getGoodsList(loadMore) {
				if(this.noMore && loadMore)return;
				this.noMore = false
				if(!loadMore){
					this.pageNum = 1;
				}else{
					this.loading = true;
				}
				this.$axios({
					url: '/goods/favorite/query',
					method: 'get',
					params: {
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						userId: this.userId,
					},
					isLoading: !loadMore
				}).then(res => {
					let _list = res.data.list;
					let pageTotal = res.data.pages;
					if(this.pageNum >= pageTotal){
						this.noMore = true;
					}
					if (_list && _list.length) {
						this.pageNum += 1
					}
					if (loadMore) {
						this.dataList = this.dataList.concat(_list);
						this.loading = false;
					} else {
						this.dataList = _list;
					}
					
					uni.stopPullDownRefresh();
				})
			},
			
			handleDelete(id) {
				this.deleteId = id;
				this.isDialog = true;
			},
			
			// 取消收藏
			confirmDelete() {
				this.$axios({
					url: '/goods/favorite/del',
					params: {
						goodFavoriteId: this.deleteId
					},
					isLoading: 1,
				}).then(res => {
					this.isDialog = false;
					this.$successToast('取消收藏成功');
					this.getGoodsList();
				})
			},
			
			toGoodsDetail(id) {
				uni.navigateTo({
					url: '/packageGoods/pages/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	.app-container {
		background: #F4F2F2;
		box-sizing: border-box;
	}
	.list-container {
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
		.item {
			width: 348rpx;
			background: #FFFFFF;
			margin-right: 14rpx;
			margin-bottom: 20rpx;
			border-radius: 20rpx;
			overflow: hidden;
			&:nth-child(2n) {
				margin-right: 0;
			}
			image {
				width: 348rpx;
				height: 348rpx;
			}
			.content {
				padding: 15rpx 20rpx;
				.title {
					font-size: 30rpx;
					color: #333333;
					line-height: 36rpx;
					font-weight: 600;
					height: 72rpx;
				}
				.bottom {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 10rpx;
					.price {
						display: flex;
						flex-direction: column;
					}
					.price-1 {
						font-size: 32rpx;
						color: #FF3F42;
						line-height: 36rpx;
					}
					.price-2 {
						font-size: 26rpx;
						color: #666666;
						line-height: 30rpx;
						text-decoration: line-through;
					}
					.btn {
						width: 60rpx;
						height: 60rpx;
						background: #FE781F;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;
						image {
							width: 41rpx;
							height: 36rpx;
							display: block;
						}
					}
				}
			}
		}
	}
</style>
