<template>
	<view class="wishlist_body">
		<view class="status_bar">
		<uni-nav-bar  left-icon="left" :rightText="isDelete?'Done':'Delete'" title="Footprint" :fixed="true" :border="false"
		color="#fff"  backgroundColor="#000" @clickLeft="back" @clickRight="$refs.alertDialog.open()"/>
		</view>
		<view class="wishlist">
			<view class="wishlist_item" v-for="(item,index) in goodsList" :key="index" >
				<view class="wishlist_item_img">
					<image :src="item.goods_img" mode="" class="wishlist_item_img_goods" @click="isDelete?deleteTap(item.id,index): goDetails(item)"></image>
					<!-- <image src="../../static/img/select.png" mode=""   class="select" v-show="isDelete&& item.id!=deletelist[index].id" ></image>
					<image src="../../static/img/select1.png" mode=""  class="select" v-show="isDelete&& item.id==deletelist[index].id||All" ></image> -->
				</view>
				<view class="wishlist_item_bottom">
					<text>US${{item.promote_price?item.promote_price:item.shop_price}}</text>
					<image src="/static/img//like_white.png" mode="" v-show="item.is_attention==0" class="wishlist_item_bottom_icon" @click.stop="addLike(item.id)"></image>
					<image src="../../static/icon/like_red.png" mode="" v-show="item.is_attention==1" class="wishlist_item_bottom_icon" @click.stop="removeLike(item.id)"></image>
				</view>
			</view>
		</view>
		<view class="empty_item" v-if="!goodsList">
			<view class="empty_item_img">
				<image src = "../../static/img/foot_white.png" mode=""></image>
			</view>
			<view>No browsing record!</view>
			<view><button @click="gotoindex()">Go to see</button></view>
		</view>
		<!-- <view class="wishlist_foot" v-show="isDelete">
			<view class="wishlist_foot_left">
				<image src="../../static/img/select2.png" mode="" v-if="!All" @click="All=true"></image>
				<image src="../../static/img/select1.png" mode="" v-else  @click="All=false"></image>
				<text>All</text>
			</view>
			<view class="wishlist_foot_right">
				<button :class="(isdelete||All)?'wishlist_foot_right_button':''" @click="$refs.alertDialog.open()">DELETE</button>
			</view>
		</view> -->
		<uni-popup ref="alertDialog" type="dialog" v-if="goodsList">
			<uni-popup-dialog type="error" cancelText="Close" confirmText="Clear" title="Tips" content="Whether to clear browsing history?" 
			@confirm="clearFavorites" ></uni-popup-dialog>
		</uni-popup>
		<view class="bootom_last" v-if="bootom == 1">-THE END-</view>
	</view>
</template>

<script>
	import {defaultRequest2} from '../../api/index.js'
	export default {
		data() {
			return {
				isDelete:false,
				goodsList:[],
				isdelete:0,
				All:false,
				deletelist:[],
				bootom:0,
			}
		},
		methods:{
			onReachBottom(e){
				console.log('end')
				this.bootom = 1
			},
			goDetails(id){
				
				uni.navigateTo({
					url:"../productDetails/index?goodid="+id.id
				})
			},
			gotoindex(){
			  uni.switchTab({
			  	url:'../index/index'
			  })	
			},
			// 清空浏览记录
			clearFavorites(){
				
				let data={_action:'cleargoodsbrowsehistory'}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:'ok',
							icon:'none',
							duration: 2000
						})
						this.getList()
					}
				})
				
			},
			
			// 添加收藏
			addLike(id){
				let data={_action:'setfaviouritegoods',goods_id:id}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:res.message,
							icon:'none',
							duration: 2000
						})
						this.changeImage(id,0)
					}
				})
			},
			// 取消收藏
			removeLike(id){
				let data={_action:'unsetfaviouritegoods',goods_id:id}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:res.message,
							icon:'none',
							duration: 2000
						})
						this.changeImage(id,1)
					}
				})
			},
			// 切换图片
			changeImage(id,type){
				this.goodsList.map(item=>{
					if(item.id==id){
						if(type==0){
							item.is_attention=1
						}else{
							item.is_attention=0
						}
					}
				})
			},
			deleteTap(id,index){
				console.log(id)
				this.isdelete = id
				let i = index		
				let list  = ({
					id:this.goodsList[i].id,
					index:i
				})
				console.log(list.id)
			let tag = 1
			const  _list = JSON.stringify(list)
			const  _delete = JSON.stringify(this.deletelist)
			console.log( _list, _delete)
			console.log(_delete.includes(_list))
			for(let i =0;i<this.deletelist.length;i++){
								 if(this.deletelist[i].id == list.id){
									 this.deletelist[i].id =''
									 this.deletelist[i].index  =''
									 tag = 0
								 }
							 }
			 
						
							if(tag!=0){
								// this.deletelist.push(list)
								this.deletelist[index].id = list.id
								this.deletelist[index].index = list.index
								console.log(this.deletelist)
								
								uni.setStorage({
									key: 'deletelist',
									data: JSON.stringify(this.deletelist)
								});
							}
							
							let list2 =[]
							this.deletelist.map( item =>{
								if(item.id!=''){
									console.log(111)
									list2.push(item)
								 
								}
							})
							console.log(list2)
							if(list2.length>0&&list2.length !== this.goodsList.length){
								// this.Select_one  = 1
								this.isdelete = 1
								this.isAll = false
							}
			      else if(list2.length == this.goodsList.length){
								this.isAll = true
								
							}else if(list2.length == 0){
								this.isAll = false
								// this.Select_one = 0
								this.isdelete = ''
								this.deletelist[index].id  = ''
								list2 =[]
							}
			},
			back(){
				uni.navigateBack()
			},
			getList(){
				let data={_action:'getgoodsbrowsehistory'}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						this.goodsList=res.data
						this.goodsList.map( item =>{
							this.deletelist.push({
								index:'',
								id:''
							})
						})
					}
				})
			}
		},
		onLoad() {
			this.getList()
		}
	}
</script>
<style>
	page{
		height: 100vh;
		background-color: #000;
		
	}
</style>
<style lang="scss" scoped>
	.bootom_last{
		 height: 200rpx;
		 width: 100%;
		 color: #fff;
		 background-color: #000;
		
		 font-size: 25rpx;
		 padding-bottom:40rpx;
		 line-height: 200rpx;
		 text-align: center;
	}
	
	.status_bar{
		height: 100rpx;
		top:0;
		padding-top: 50rpx;
		width: 100%;
		z-index: 999;
		position: fixed;
		background-color: #000000;
	}
	.uni-navbar{
		font-size: 32rpx;
		font-weight: bold;
	}
	.wishlist{
		margin-top: 70rpx;
	}
	.wishlist_item_bottom{
		width: auto;
		box-sizing: border-box;
	}
	.empty_item{
		
		height: 500rpx;
		padding: 20rpx 100rpx;
		margin:50rpx auto;
		color:#fff;
		font-size: 24rpx;
		text-align: center;
		.empty_item_img{
			height: 180rpx;
			width: 180rpx;
			margin: 0 auto;
			margin-bottom: 30rpx;
			image{
				height: 100%;
				width: 100%;
				
			}
		}
		button {
			font-size: 24.33rpx;
			font-weight: 500;
			background-color: white;
			color: #000;
			width: 160rpx;
			height: 65.67rpx;
			border-radius: 20rpx !important;
			line-height: 65.67rpx;
			border-radius: 0;
			margin-top: 33.33rpx;
		}
		.uni-button{
			border-radius: 20rpx !important;
		}
	}
</style>