<template>
	<view class="wishlist_body">
		<view class="status_bar"></view>
		<uni-nav-bar  left-icon="left"  :rightText="isDelete?'Done':'Delete'" title="Wishlist" color="#000000" @clickLeft="back" @clickRight="clickRight()"/>
		<view class="wishlist">
			<view class="wishlist_item" v-for="(item,index) in goodsList" :key="item.goods_id" >
				<view class="wishlist_item_img"@click="isDelete?selectGoods(item.goods_id,index): goDetails(item.goods_id)">
					<image :src="item.goods_img" mode="" class="wishlist_item_img_goods"></image>
					<image src="../../static/img/select1.png" @click="deleteTap(item)" mode=" "  class="select" v-if="isDelete && item.goods_id==deletelist[index].id || isAll"></image> 
				<image src="../../static/img/select.png" mode=""  class="select" v-else-if=" isDelete &&item.goods_id!=deletelist[index].id"></image>
				    <!-- <image :src="Src" @click="deleteTap(item)" mode=" " v-model="item.goods_id" class="select" v-if="isDelete || item.goods_id==goods_id"></image> -->
				</view>
				<view class="wishlist_item_bottom">
					<text>US${{item.promote_price?item.promote_price:item.shop_price}}</text>
					<image src="../../static/img/delete1.png" mode="" class="wishlist_item_bottom_icon" @click="removeL(item)"></image>
				</view>
			</view>
		</view>
		<view class="wishlist_foot" v-show="isDelete">
			<view class="wishlist_foot_left" @click="isAllTap()">
				<image src="../../static/img/select2.png" mode="" v-if="!isAll"></image>
				<image src="../../static/img/select1.png" mode="" v-else></image>
				<text>All</text>
			</view>
			<view class="wishlist_foot_right">
				<button :disabled="  !goods_id" @click="$refs.alertDialog.open()" :class="(goods_id)?'wishlist_foot_right_button':''">DELETE</button>
			</view>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="error" cancelText="Close" confirmText="Sure" title="Tips" content="Cancel the collection of this product?" 
			@confirm="removeLike()" ></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {defaultRequest2} from '../../api/index.js'
	export default {
		data() {
			return {
				isDelete:false,
				goodsList:[],
				goods_id:'',
				isAll:false,
				Src:'../../static/img/select.png',
				deletelist:[],
				Select_one:0
			}
		},
		methods:{
			selectGoods(id,index){
				if(id==this.goods_id){
					this.goods_id=''
				}else{
					this.goods_id=id
				}
				if(this.isAll ==true){
					this.isDelete=true 
				}
				let i = index
				console.log(i)
		
				
					let list  = ({
						id:this.goodsList[i].goods_id,
						index:i
					})
				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.goods_id = 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.goods_id = ''
					this.deletelist[index].id  = ''
					list2 =[]
				}
				// this.$store.commit('addSave', list)
				// if(this.$store.state.goodslist.includes(list)){
				// 	const i = this.$store.state.goodslist.indexOf(list)
				// 	this.$store.state.goodslist = this.$store.state.goodslist.splice(i,1)
				// 	console.log(i)
				// }
				
			},
			deleteTap(u){
				this.goods_id = item.goods_id
				
			},
			isAllTap(){
				this.isAll = !this.isAll
				if(this.isAll == false){
					this.goods_id = ''
					for(let i =0;i<this.deletelist.length;i++){
						 this.deletelist[i].id =''
						 this.deletelist[i].index  =''
					}
					
				}else{
					this.goods_id = 1
					for(let i =0;i<this.goodsList.length;i++){
					this.deletelist[i].index = i
					this.deletelist[i].id = this.goodsList[i].goods_id
							}
					console.log(this.deletelist)
					
				}
			},
			removeL(e){
				console.log(e.goods_id)
				let data={_action:'unsetfaviouritegoods',goods_id:e.goods_id}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:'ok!',
							icon:'none'
						})
						
						this.getList()
					}
				})
			},
			
			goDetails(id){
				uni.navigateTo({
					url:"../productDetails/index?goodid="+id
				})
			},
			getList(){
				let data={_action:'getfaviouritegoods',page_index:1,page_size:30}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						this.goodsList=res.data
						this.goodsList.map( item =>{
							this.deletelist.push({
								index:'',
								id:''
							})
						})
					   
					}
				})
			},
			// 添加收藏
			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'
						})
					}
				})
			},
			// 取消收藏
		async	removeLike(){
			
				// let data={_action:'unsetfaviouritegoods',goods_id:this.goods_id}
				// if(this.isAll)data.goods_id=0
				let list =[]
				let res 
				this.deletelist.map( item =>{
					if(item.id!=''){
						console.log(111)
						list.push(item)
					 
					}
				})
				console.log(list)
				res = await Promise.all(list.map(async (item) => {
					// 等待异步操作完成，返回执行结果 
					return await defaultRequest2({
						_action: 'unsetfaviouritegoods',
						goods_id: item.id
					})
					
				}));
				console.log(res)
				if(res[0].error==0){
					console.info(1234)
					setTimeout(() =>{
						
						uni.showToast({
							title:'ok!',
							icon:'none'
						})
					},1000)
					this.getList()
					// uni.startPullDownRefresh()
					this.isAll = false
					this.isDelete = false
					
					}
				
			},
			back(){
				uni.navigateBack()
			},
			clickRight(){
				// this.isDelete=!this.isDelete
				// this.isAll=false
				if(this.goodsList){
					this.isAll= false
					this.isDelete=!this.isDelete
					
				}
			}
		},
     onLoad() {
			this.getList()
			// uni.startPullDownRefresh()
			// uni.clearStorage()
			 this.deletelist = []
			// let list = await uni.getStorage({
			// 	key: 'deletelist'
			// 		});
			// if (list.length>1) {
				
			// this.deletelist = JSON.parse(list[1].data);
			// console.log(this.deletelist);
			// 	}
		},
		onPullDownRefresh(){
			this.getList()
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 2000);
		}
	}
</script>

<style lang="scss" scoped>
	page{
		
	}
	.uni-navbar{
		font-size: 32rpx;
		font-weight: bold;
	}
</style>