<template>
	<view class="my-product">
		<view class="header">
			<view class="title">
				收藏的商品
			</view>
			<view style="position: relative;">
				<view class="sub-title">全部宝贝</view>
				<view class="release" @tap="edit=!edit">
					{{edit?'取消':'管理'}}
				</view>
			</view>
		</view>
		<view v-if="collections.length===0"
			style="margin: 20upx auto;text-align: center; font-size: 28upx;color: #999999;">
			暂未收藏任何商品
		</view>
		<view class="product-list">
			<view class="product-item" v-for="collection in collections" @tap="openDetails(collection.product.id)">
				<view v-if="edit" class="select">
					<checkbox class="checkbox" :checked="collection.checked"
						@tap="collection.checked=!collection.checked" />
				</view>

				<view class="product-image">
					<image class="image" :src="collection.product.images|imagesFilter" mode="scaleToFill"></image>
				</view>
				<view>
					<view class="row row-1">
						<text class="title"><text class="sxzg-icon">省心直供</text>{{collection.product.name}}</text>
					</view>
					<view class="row row-2">
						<text class="org-price">￥{{collection.product.org_price}}</text>
					</view>
					<view class="row row-3">
						<text class="sxj-icon">省心价</text>
						<text class="price">￥{{collection.product.price}}</text>
					</view>
					<view class="row row-4">
						<text>日期：</text>
						<text class="time">{{collection.createtime|datetimeFilter}}</text>
					</view>
				</view>

			</view>
		</view>

		<view v-if="edit" class="footer">
			<view class="btn-wrapper">
				<button class="btn del-btn" type="default" @tap="del">删除已勾选的商品</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				page: 1,
				pageLoading: false,
				collections: [],
				edit: false
			}
		},
		onLoad() {
			// this.getProductData();
		},

		onPageScroll(e) {
			const query = uni.createSelectorQuery();
			query.select("#my-product").boundingClientRect(data => {
				if (e.scrollTop > data.height - uni.getSystemInfoSync().windowHeight * 2) {
					// this.getProductData();
				}
			}).exec();
		},
		onPullDownRefresh() {
			this.refresh();
		},
		onShow() {
			this.refresh();
		},
		methods: {
			refresh() {
				this.page = 1;
				this.collections = [];
				this.getCollectionsData()
					.then(uni.stopPullDownRefresh())
					.catch(uni.stopPullDownRefresh());
				
			},
			getCollectionsData() {
				return new Promise((resolve, reject) => {

					this.$http.get({
						url: "/collection/lists",
						data: {
							limit: 10,
							page: this.page
						},
						success: (res) => {
							this.collections = [
								...this.collections,
								...res.data.data.rows
							]
						}
					})
				})
			},
			openDetails(id) {
				if (this.edit) {
					return;
				}
				uni.navigateTo({
					url: "/pages/product/product-details?id=" + id
				})
			},
			del() {
				let checked = this.collections.filter((cur) => {
					console.log(cur)
					return cur.checked
				}).map((cur) => {
					return cur.product_id
				})
				this.$http.post({
					url: "/collection/del",
					data: {
						product_id: checked.join(",")
					},
					success: (res) => {
						uni.showToast({
							icon: "none",
							title: "删除成功",
							success: () => {
								this.collections = this.collections.filter((cur) => {
									return !checked.includes(cur.product_id)
								})
								this.edit = false;
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.my-product {
		overflow: hidden;
	}

	.header {
		margin: 20upx;
		background: white;
		text-align: center;
		border-radius: 20upx;
		padding-bottom: 10upx;

		.title {
			height: 100upx;
			font-size: 32upx;
			line-height: 100upx;
			// font-weight: bold;
		}

		.sub-title {
			font-size: 28upx;
			color: #999999;
		}

		.release {
			position: absolute;
			right: 20upx;
			font-size: 24upx;
			bottom: 4upx;
		}
	}


	.product-list {
		.product-item {
			display: flex;
			background: white;
			margin: 20upx;
			padding: 20upx;

			.select {
				align-self: center;
				position: relative;
				margin-top: -4upx;
				margin-right: 14upx;

				.checkbox {
					transform: scale(0.7);

					/deep/ .uni-checkbox-input {
						border: 2upx solid #CCCCCC;

						&.uni-checkbox-input-checked {
							color: $primary-color !important;
						}
					}

					/deep/ .uni-checkbox-input:hover {
						border: 2upx solid #CCCCCC;
					}
				}
			}

			.product-image {
				width: 180upx;
				height: 180upx;
				margin-right: 20upx;
			}

			.image {
				width: 180upx;
				height: 180upx;
				background: #EEEEEE;
			}
		}

		.sxzg-icon {
			color: $primary-color;
			font-size: 16rpx;
			width: 80rpx;
			text-align: center;
			line-height: normal;
			border: 2rpx solid $primary-color;
			border-radius: 20rpx;
			display: inline-block;
			position: relative;
			top: -4rpx;
			margin-right: 10upx;
			// transform: scale(0.9);
		}

		.title {
			font-size: 24rpx;
			display: inline-block;
			white-space: normal;
			display: -webkit-box;
			-webkit-box-orient: vertical;
			-webkit-line-clamp: 2;
			overflow: hidden;
			height: 68upx;
		}

		.row {
			// padding: 0 10upx;
		}

		.row-2 {
			display: flex;
			justify-content: space-between;
			margin-top: 4upx;
		}

		.org-price {
			font-size: 26rpx;
			color: #cccccc;
		}

		.org-price {
			text-decoration: line-through;
		}

		.sxj-icon {
			background: $primary-color;
			color: white;
			font-size: 20upx;
			padding: 0 5upx;
			border-radius: 5upx;
			vertical-align: middle;
		}

		.price {
			font-size: 26rpx;
			color: $primary-color;
			font-weight: bold;			
		}
		.row-3 {
			display: flex;
			align-items: center;
			margin-top: 4upx;
		}

		.row-4 {
			font-size: 24upx;
			color: #999999;
			margin-top: 4upx;
		}
	}

	.footer {
		display: flex;
		position: fixed;
		height: 100upx;
		background: $primary-color;
		bottom: 0;
		width: 100%;
		box-shadow: 0 0 10upx #999999;
		align-items: center;

		.btn-wrapper {
			flex: 1;
			margin: 0 20upx;
		}

		.del-btn {
			background: $primary-color;
			padding: 15upx 60upx;
			color: white;
			line-height: normal;
			font-size: 32upx;
			border-radius: 50upx;

		}
	}
</style>
