<template>
	<view class="pcard" @tap="$emit('open', post)">
		<view class="pcard-hd">
			<image class="pcard-avatar" :src="avatarUrl" mode="aspectFill" />
			<view class="pcard-meta">
				<text class="pcard-nick">{{ nickname }}</text>
				<text class="pcard-time">{{ timeText }}</text>
			</view>
			<view
				class="pcard-follow"
				:class="{ 'pcard-follow--on': isFollowed }"
				@tap.stop="$emit('follow', post)"
			>
				<text class="pcard-follow-text">{{ isFollowed ? copy.followed : copy.followPlus }}</text>
			</view>
		</view>

		<post-content-block
			:post="post"
			@open-video="$emit('open-video', $event)"
			@open-detail="$emit('open-detail', $event)"
		/>

		<view class="pcard-ft">
			<view class="pcard-action" @tap.stop="$emit('like', post)">
				<text class="pcard-like" :class="{ 'pcard-like--on': post.isLike === 1 }">♥</text>
				<text class="pcard-num">{{ post.likes || 0 }}</text>
			</view>
			<view class="pcard-action" @tap.stop="$emit('comment', post)">
				<text class="pcard-comment">💬</text>
				<text class="pcard-num">{{ post.comments || 0 }}</text>
			</view>
			<view class="pcard-action">
				<text class="pcard-view">👁</text>
				<text class="pcard-num">{{ post.views || 0 }}</text>
			</view>
		</view>
	</view>
</template>

<script>
import PostContentBlock from '@/components/dynamics/post-content-block.vue'

export default {
	name: 'PostCard',
	components: { PostContentBlock },
	props: {
		post: { type: Object, default: () => ({}) }
	},
	computed: {
		copy() {
			const t = this.$t.bind(this)
			return {
				followPlus: t('dynamics.followPlus'),
				followed: t('dynamics.followed'),
				guest: t('dynamics.guest')
			}
		},
		nickname() {
			if (this.post.shop && this.post.shop.shopname) return this.post.shop.shopname
			if (this.post.user && this.post.user.nickname) return this.post.user.nickname
			return this.copy.guest
		},
		avatarUrl() {
			const path = (this.post.shop && this.post.shop.avatar)
				|| (this.post.user && this.post.user.avatar)
				|| ''
			if (this.$appKit && typeof this.$appKit.oss === 'function') {
				return this.$appKit.oss(path, 72, 72)
			}
			return path
		},
		timeText() {
			if (this.$appKit && typeof this.$appKit.timeFormat === 'function') {
				return this.$appKit.timeFormat(this.post.createtime, 'yyyy-mm-dd') || ''
			}
			return ''
		},
		isFollowed() {
			const flag = this.post && this.post.isFollow
			return flag === 1 || flag === '1' || flag === true
		}
	}
}
</script>

<style scoped>
.pcard {
	margin: 0 32rpx 28rpx;
	padding: 28rpx;
	background-color: #ffffff;
	border-radius: 28rpx;
	box-shadow: 0 8rpx 32rpx rgba(74, 140, 255, 0.08);
}
.pcard-hd {
	display: flex;
	flex-direction: row;
	align-items: center;
	margin-bottom: 24rpx;
}
.pcard-avatar {
	width: 72rpx;
	height: 72rpx;
	border-radius: 36rpx;
	background-color: #eef2f8;
	flex-shrink: 0;
}
.pcard-meta {
	flex: 1;
	min-width: 0;
	margin-left: 20rpx;
}
.pcard-nick {
	font-size: 30rpx;
	font-weight: 700;
	color: #1a1d26;
	display: block;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.pcard-time {
	font-size: 24rpx;
	color: #8a8d9e;
	margin-top: 4rpx;
}
.pcard-follow {
	padding: 8rpx 20rpx;
	border-radius: 999rpx;
	background-color: #4a8cff;
	flex-shrink: 0;
}
.pcard-follow--on {
	background-color: #eef2f8;
}
.pcard-follow-text {
	font-size: 22rpx;
	color: #ffffff;
}
.pcard-follow--on .pcard-follow-text {
	color: #8a8d9e;
}
.pcard-ft {
	display: flex;
	flex-direction: row;
	align-items: center;
	margin-top: 24rpx;
	padding-top: 20rpx;
	border-top: 1rpx solid rgba(0, 0, 0, 0.05);
}
.pcard-action {
	display: flex;
	flex-direction: row;
	align-items: center;
	margin-right: 36rpx;
}
.pcard-like,
.pcard-comment,
.pcard-view {
	font-size: 30rpx;
	margin-right: 8rpx;
	color: #b0b4c0;
}
.pcard-like--on {
	color: #ff4d4f;
}
.pcard-num {
	font-size: 26rpx;
	color: #5c6070;
}
</style>
