<template>
	<view class="wishlist_body">
		<view class="status_bar"></view>
		<uni-nav-bar  left-icon="left"  :rightText="isDelete?'Done':'Delete'" title="Favorites" color="#000000" @clickLeft="back" @clickRight="$refs.alertDialog.open()"/>
		<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"></image>
					<image src="../../static/img/select.png" mode=""  class="select" v-show="isDelete"></image>
				</view>
				<view class="wishlist_item_bottom">
					<text>US${{item.promote_price?item.promote_price:item.shop_price}}</text>
					<image src="/static/img/like.png" mode="" v-show="item.is_attention==0" class="wishlist_item_bottom_icon" @click="addLike(item.id)"></image>
					<image src="../../static/img/like1.png" mode="" v-show="item.is_attention==1" class="wishlist_item_bottom_icon" @click="removeLike(item.id)"></image>
				</view>
			</view>
		</view>
		<view class="wishlist_foot" v-show="isDelete">
			<view class="wishlist_foot_left">
				<image src="../../static/img/select2.png" mode=""></image>
				<text>All</text>
			</view>
			<view class="wishlist_foot_right">
				<button>DELETE</button>
			</view>
		</view>
		<uni-popup ref="alertDialog" type="dialog">
			<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>
</template>

<script>
	import {defaultRequest2} from '../../api/index.js'
	export default {
		data() {
			return {
				isDelete:false,
				goodsList:[]
			}
		},
		methods:{
			// 清空浏览记录
			clearFavorites(){
				let data={_action:'cleargoodsbrowsehistory'}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						uni.showToast({
							title:res.message,
							icon:'none'
						})
						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'
						})
						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'
						})
						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
						}
					}
				})
			},
			back(){
				uni.navigateBack()
			},
			getList(){
				let data={_action:'getgoodsbrowsehistory'}
				defaultRequest2(data).then(res=>{
					console.info(res)
					if(res.error==0){
						this.goodsList=res.data
					}
				})
			}
		},
		onLoad() {
			this.getList()
		}
	}
</script>

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