<template>
	<view class="store">
		<u-empty mode="list" v-if="!list.length" color="#000" img-width="200" font-size="30" margin-top="300"></u-empty>
		<u-swipe-action
			v-for="(item, index) in list" :key="index" 
			:index='item.store_id'
			:show="item.show"
			:options="options"
			@click="removeFavorite"
			@open="open"
		>
			<view class="item u-border-bottom" @click="toDetailsPage(item.store_id)">
				<image :src="item.store_avatar"></image>
				<!-- 此层wrap在此为必写的，否则可能会出现标题定位错误 -->
				<view class="title-wrap">
					<view class="item-name u-line-1">{{ item.store_name }}</view>
					<view class="item-date">
						<image src="@/pageE/static/mine/26.png"></image>
						<view>{{ item.fav_time | date }}</view>
					</view>
				</view>
			</view>
		</u-swipe-action>
	</view>
</template>
<script>
export default {
	data() {
		return {
			list: [],
			show: false,
			options: [
				{
					text: '删除',
					style: {
						backgroundColor: '#FF3131'
					}
				}
			]
		}
	},
	mounted() {
		this.getStoreFavoritesList();
	},
	methods: {
		getStoreFavoritesList() {
			this.$u.api.getFavoritesList({
				type: 2 // 店铺 type: 2
			}).then(res => {
				this.list = res.data;
			})
		},
		removeFavorite(id) {
			this.$u.api.removeFavorite({
				id: id,
				type: 'store'
			}).then(res => {
				this.$u.toast(res.message);
				if(res.errCode == 0) {
					this.getStoreFavoritesList();
				}
			})
		},
		toDetailsPage(id) {
			this.$u.route('pageC/merchant/index', {
				id: id
			});
		},
		open(index) {
			// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
			// 原本为'false'，再次设置为'false'会无效
			this.list[index].show = true;
			this.list.map((val, idx) => {
				if(index != idx) this.list[idx].show = false;
			})
		}
	},
};
</script>
<style lang="scss" scoped>
.store {
	min-height: calc(100vh - var(--window-top));
	background-color: #ECECEC;
	padding-top: 1rpx;
	.item {
		padding: 30rpx;
		display: flex;
		align-items: center;
		> image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			flex-shrink: 0;
			margin-right: 21rpx;
		}
		.title-wrap {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.item-name {
				width: 260rpx;
				font-size: 30rpx;
				color: rgba(51,51,51,1);
			}
			.item-date {
				display: flex;
				align-items: center;
				> image {
					margin-right: 25rpx;
					width: 24rpx;
					height: 24rpx;
				}
			}
		}
	}
}
</style>