<template>
	<view class="content">
		<view class="top hflex acenter jbetween">
			<view class="left">共<span class="text_red">{{total}}</span>件商品</view>
			<view class="left" :class="is_manager?'text_blue':''" @click="manager">{{is_manager?'完成':'编辑'}}</view>
		</view>
		<view class="box">
			<block v-for="(item,index) in pageList" :key="index">
				<view class="item hflex acenter">
					<u-checkbox-group v-if="is_manager == 1" placement="column" @change="checkboxChange(index)">
						<u-checkbox v-model="item.id" :value="item.id" :checked="item.checked" shape="circle"></u-checkbox>
					</u-checkbox-group>
					<view class="">
						<image :src="item.cover" mode="aspectFill" class="img"></image>
					</view>
					<view class="vflex" style="margin-left: 24rpx;">
						<view class="name">{{item.name}}</view>
						<view class="price">￥{{item.price_selling}}</view>
					</view>
				</view>
			</block>
		</view>
		<view class="bottom hflex acenter jbetween" v-if="is_manager == 1">
			<view>
				<u-checkbox-group placement="column" @change="checkboxAll2">
					<u-checkbox :value="all" :checked="isAllSelected" shape="circle" label="全选"></u-checkbox>
				</u-checkbox-group>
			</view>
			<view class="hflex aend">
				<view class="btn2" @click="batchDelete">删除</view>
			</view>
		</view>
	</view>
</template>

<script>
	import $api from '@/static/js/api.js'
	var that = ''
	export default {
		data() {
			return {
				length: 0,
				pageList: [
					
				],
				is_manager: 0,
				all: '',
				isAllSelected: false,
				page: 1,
				total: 1
			}
		},
		onLoad() {
			that = this
			that.getList()
		},
		methods: {
			getList() {
				$api.req({
					url: '/data/api.Goods/collection_list',
					data: {
						page: that.page
					}
				}, function(res) {
					if(res.code == 1) {
						console.log(res);
						that.pageList = res.data.data
						that.total = res.data.total
					}
				})
				that.totalLength()
			},
			// 统计商品数量
			totalLength() {
				that.length = that.pageList.length
			},
			// 编辑
			manager() {
				if(that.is_manager == 0) {
					that.is_manager = 1
				} else {
					that.is_manager = 0
				}
			},
			// 全选
			checkboxAll2() {
				that.isAllSelected = !that.isAllSelected
				if(that.isAllSelected) {
					for(var i = 0;i<that.pageList.length;i++) {
						that.$set(that.pageList[i],'checked',true)
					}
					
				} else {
					for(var i = 0;i<that.pageList.length;i++) {
						that.$set(that.pageList[i],'checked',false)
					}
				}
			},
			// 单选
			checkboxChange(index) {
				that.$set(that.pageList[index],'checked',true)
			},
			batchDelete() {
				var coll_ids = []
				console.log(that.pageList);
				for(var i = 0;i<that.pageList.length;i++) {
					if(that.pageList[i].checked) {
						console.log(that.pageList);
						coll_ids.push(that.pageList[i].id)
					}
				}
				coll_ids = coll_ids.toString()
				$api.req({
					url: '/data/api.Goods/del_collection',
					data: {
						coll_ids: coll_ids
					}
				}, function(res) {
					if(res.code == 1) {
						$api.info(res.info)
						that.getList()
					}
				})
			},
			onReachBottom() {
				console.log("到底了");
				if (that.page == that.total) {
					$api.info("没有更多了")
				} else {
					this.page++
					this.getDatalist()
				}
				
			}
		},
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #F5F5F5;
		.top {
			width: 100%;
			box-sizing: border-box;
			padding: 20rpx 30rpx;
			.left {
				font-size: 32rpx;
				font-weight: 400;
				color: #555555;
				line-height: 44rpx;
			}
			.text_red {
				color: #FF5E5E;
			}
			.text_blue {
				color: #506DFF;
			}
		}
		.box {
			width: 100%;
			box-sizing: border-box;
			padding: 0 30rpx;
			background: #FFFFFF;
			.item {
				width: 100%;
				padding: 20rpx 0 32rpx;
				border-bottom: 1rpx solid #F4F4F4;
				.img {
					width: 196rpx;
					height: 196rpx;
					border-radius: 16rpx;
				}
				.name {
					font-size: 32rpx;
					font-weight: 400;
					color: #222222;
					line-height: 44rpx;
					text-overflow: ellipsis;
					overflow: hidden;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					box-orient: vertical;
					line-clamp: 2;
					-webkit-line-clamp: 2;
				}
				.price {
					font-size: 44rpx;
					font-weight: bold;
					color: #FF5E5E;
					line-height: 52rpx;
					padding-top: 32rpx;
				}
			}
			.item:nth-last-child(1) {
				border: none;
			}
		}
		.bottom {
			width: 100%;
			z-index: 9;
			position: fixed;
			bottom: 0;
			height: 166rpx;
			background: #FFFFFF;
			box-sizing: border-box;
			padding: 8rpx 30rpx 74rpx;
			.btn2 {
				width: 200rpx;
				height: 80rpx;
				border-radius: 44rpx;
				border: 1px solid #506DFF;
				font-size: 36rpx;
				font-weight: 500;
				color: #506DFF;
				line-height: 80rpx;
				text-align: center;
			}
		}
	}
</style>