<template>
	<view class="comment-item-components">
		<view class="comment-type-tab">
			<view class="item" :class="{'active':activeIndex==0}" @click="handleChange(0)">评论列表</view>
			<view class="item" v-if="form.isSelf==1" :class="{'active':activeIndex==1}" @click="handleChange(1)">管理全部评论
			</view>
		</view>
		<view class="empty-box" v-if="commentList.length == 0">
			<image src="/static/images/empty.png" class="icon"></image>
			<view class="text">期待您的经常评论~</view>
		</view>

		<view class="comment-display-box" v-else>
			<view class="comment-item-new" v-for="(item,index) in commentList">
				<block v-if="activeIndex==0">
					<!-- PK评论展示红蓝方 -->
					<block v-if="form.isEnableVote == 1">
						<!-- 红方 -->
						<view class="user-info" v-if="item.voteOptionCode=='1'">
							<view class="flex-layout">
								<!-- 是否精选 -->
								<view class="tag rmark-icon mr-10" v-if="item.isFeatured">
									<image src="/static/images/is-featured.png" class="icon"></image>
								</view>
								<!-- 是否置顶 -->
								<view class="tag rmark-icon mr-10" v-if="item.isTop">
									<image src="/static/images/is-top.png" class="icon"></image>
								</view>
								<view class="tag red-tag">红</view>
								<view class="username ml-10">{{item.nickName}}</view>
							</view>
							<view class="flex-layout">
								<view class="datetime">{{item.createTime}}</view>
								<image src="/static/images/more-icon.png" class="more ml-10"
									@click.stop="(e)=>handleOperationDynamics(e,item)"></image>
							</view>
						</view>
						<view class="user-info" v-else>
							<view class="flex-layout">
								<image src="/static/images/more-icon.png" class="more mr-10"
									@click.stop="(e)=>handleOperationDynamics(e,item)"></image>
								<view class="datetime">{{item.createTime}}</view>
							</view>
							<view class="flex-layout">
								<view class="username mr-10">{{item.nickName}}</view>
								<view class="tag blue-tag">蓝</view>
								<!-- 是否精选 -->
								<view class="tag rmark-icon ml-10" v-if="item.isFeatured">
									<image src="/static/images/is-featured.png" class="icon"></image>
								</view>
								<!-- 是否置顶 -->
								<view class="tag rmark-icon ml-10" v-if="item.isTop">
									<image src="/static/images/is-top.png" class="icon"></image>
								</view>
							</view>
						</view>
						<view class="content" v-if="item.voteOptionCode=='1'">
							<view class="text " @click.stop="handleReplayComment(item)">
								{{item.content}}
							</view>
							<view class="replay-content" v-if="item.replyNickName"
								@click.stop="handleReplayComment(item)">
								<span>{{item.replyNickName}}：</span>{{item.replyContent}}
							</view>
						</view>
						<view class="content" v-if="item.voteOptionCode=='2'">
							<view class="text text-blue" @click.stop="handleReplayComment(item)">
								{{item.content}}
							</view>
							<view class="blue-content">
								<view class="replay-content" v-if="item.replyNickName"
									@click.stop="handleReplayComment(item)">
									<span>{{item.replyNickName}}：</span>{{item.replyContent}}
								</view>
							</view>

						</view>
					</block>
					<!-- 非PK评论正常展示 -->
					<block v-else>
						<view class="user-info">
							<view class="flex-layout">
								<!-- 是否精选 -->
								<view class="tag rmark-icon mr-10" v-if="item.isFeatured">
									<image src="/static/images/is-featured.png" class="icon"></image>
								</view>
								<!-- 是否置顶 -->
								<view class="tag rmark-icon mr-10" v-if="item.isTop">
									<image src="/static/images/is-top.png" class="icon"></image>
								</view>
								<view class="username ml-10">{{item.nickName}}</view>
							</view>
							<view class="flex-layout">
								<view class="datetime">{{item.createTime}}</view>
								<image src="/static/images/more-icon.png" class="more ml-10"
									@click.stop="(e)=>handleOperationDynamics(e,item)"> </image>
							</view>
						</view>
						<view class="content">
							<view class="text" @click.stop="handleReplayComment(item)">
								{{item.content}}
							</view>
							<view class="replay-content" v-if="item.replyNickName"
								@click.stop="handleReplayComment(item)">
								<span>{{item.replyNickName}}：</span>{{item.replyContent}}
							</view>
						</view>
					</block>
				</block>
				<block v-if="activeIndex==1">
					<view class="user-info">
						<view class="flex-layout">
							<!-- 是否精选 -->
							<view class="tag rmark-icon mr-10" v-if="item.isFeatured">
								<image src="/static/images/is-featured.png" class="icon"></image>
							</view>
							<!-- 是否置顶 -->
							<view class="tag rmark-icon mr-10" v-if="item.isTop">
								<image src="/static/images/is-top.png" class="icon"></image>
							</view>
							<view class="tag red-tag" v-if="item.voteOptionCode=='1'">红</view>
							<view class="tag blue-tag" v-if="item.voteOptionCode=='2'">蓝</view>
							<view class="username ml-10">{{item.nickName}}</view>
						</view>
						<view class="flex-layout">
							<view class="datetime">{{item.createTime}}</view>
							<image src="/static/images/more-icon.png" class="more ml-10"
								@click.stop="(e)=>handleOperationDynamics(e,item)"></image>
						</view>
					</view>
					<view class="content">
						<view class="text" @click.stop="handleReplayComment(item)">
							{{item.content}}
						</view>
						<view class="replay-content" v-if="item.replyNickName" @click.stop="handleReplayComment(item)">
							<span>{{item.replyNickName}}：</span>{{item.replyContent}}
						</view>
					</view>
				</block>
				<view class="show-more-box" v-if="commentList.length < total" @click="loadMore">
					加载更多
					<image src="/static/images/down.png" class="icon"></image>
				</view>

			</view>
			<!-- 操作按钮 -->
			<view class="operation-box" v-if="showOperationBtn"
				:style="'left:' + clientX + 'rpx;' +'top:' + clientY + 'rpx'">
				<view class="item del-item" v-if="operationItem.isSelf==1 || userInfo.bbsAdmin"
					@click.stop="handleDelete(operationItem)">删除</view>
				<view class="item" v-if="activeIndex==1 && operationItem.isTop==0"
					@click="handleIsTop(operationItem,1)">置顶</view>
				<view class="item" v-if="activeIndex==1 && operationItem.isTop==1"
					@click="handleIsTop(operationItem,0)">取消置顶</view>
				<view class="item" v-if="activeIndex==1 &&operationItem.isFeatured==0 &&form.isEnableFeaturedComment==1"
					@click="handleFeatured(operationItem)">加入精选</view>
				<view class="item" v-if="activeIndex==1 &&operationItem.isFeatured==1&&form.isEnableFeaturedComment==1"
					@click="handleFeatured(operationItem)">取消精选</view>
			</view>
		</view>
		<!-- 打开评论 -->
		<publish-comment :form="form" ref="publishCommentRef" @refresh="handleRefresh"></publish-comment>
	</view>
</template>

<script>
	import PublishComment from './PublishComment.vue';
	import {
		commentFeatured,
		delComment,
		dynamicDetailComments,
		commentIsTop
	} from '@/api/api.js'
	import {
		getUserInfo
	} from '@/utils/auth';
	export default {
		name: "Comment-Item",
		data() {
			return {
				commentList: [],
				activeIndex: 0,
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					momentId: ''
				},
				total: 0,
				userInfo: {},
				showOperationBtn: false,
				clientX: 0,
				clientY: 0,
				operationItem: {}
			};
		},
		components: {
			PublishComment
		},
		props: {
			form: {
				type: Object,
				default: () => {
					return {}
				}
			}
		},
		watch: {
			form(newVal, oldVal) {
				console.log(newVal, oldVal)
				if (newVal.id) {
					this.queryParams.momentId = newVal.id;
					this.commentList = [];
					this.userInfo = JSON.parse(JSON.parse(getUserInfo()))
					this.getCommentList()
				}
			}
		},
		onLoad() {

		},
		methods: {
			handleChange(index) {
				this.activeIndex = index
				this.showOperationBtn = false
			},
			handleRefresh() {
				this.queryParams.pageNum = 1;
				this.commentList = [];
				this.getCommentList()
			},
			getCommentList() {
				dynamicDetailComments(this.queryParams).then(res => {
					this.commentList = [...this.commentList, ...res.rows]
					this.total = res.total
				})
			},
			loadMore() {
				this.queryParams.pageNum += 1;
				this.getCommentList()
			},
			// 回复评论
			handleReplayComment(item) {
				this.$refs.publishCommentRef.openModal(item)
			},
			// 加入精选，取消精选
			handleFeatured(item) {
				let _this = this;
				let text = item.isFeatured == 1 ? '取消精选' : '加入精选'
				let data = {
					momentId: this.form.id,
					commentId: item.id,
					featuredStatus: item.isFeatured == 1 ? 0 : 1
				}
				uni.showModal({
					title: '提示',
					content: `是否将评论：${item.content}${text}`,
					success: function(res) {
						if (res.confirm) {
							commentFeatured(data).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: `${text}成功`
									})
									_this.showOperationBtn = false
									_this.handleRefresh()
								}
							})
						} else if (res.cancel) {
							_this.showOperationBtn = false
							console.log('用户点击取消');
						}
					}
				});
			},
			handleDelete(item) {
				let _this = this;
				uni.showModal({
					title: '提示',
					content: `是否确认删除评论：${item.content}？`,
					success: function(res) {
						if (res.confirm) {
							delComment(item.id).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: `删除成功`
									})
									_this.showOperationBtn = false
									_this.handleRefresh()
								}
							})
						} else if (res.cancel) {
							_this.showOperationBtn = false
							console.log('用户点击取消');
						}
					}
				});
			},
			handleIsTop(item, isTop) {
				let _this = this;
				let text = isTop == '0' ? '取消置顶' : '置顶'
				let data = {
					isTop,
					commentId: item.id,
					momentId: this.form.id,
				}
				let msg = `确认要${text}评论内容为：${item.content}的数据吗？`
				uni.showModal({
					title: '提示',
					content: msg,
					success: function(res) {
						if (res.confirm) {
							commentIsTop(data).then(res => {
								if (res.code == 200) {
									uni.showToast({
										title: `${text}成功`
									})
									_this.showOperationBtn = false
									_this.handleRefresh()
								}
							})
						} else if (res.cancel) {
							_this.showOperationBtn = false
							console.log('用户点击取消');
						}
					}
				});
			},
			// 展示删除动态按钮
			handleOperationDynamics(e, item) {
				this.showOperationBtn = !this.showOperationBtn
				this.operationItem = {
					...item
				}
				if (e.target.offsetLeft < 100) {
					this.clientX = (e.target.offsetLeft * 2);
					this.clientY = ((e.target.offsetTop * 2) + 14);
				} else {
					this.clientX = (e.target.offsetLeft * 2 - 148);
					this.clientY = ((e.target.offsetTop * 2) + 34);
				}
			},

		}
	}
</script>

<style lang="scss" scoped>
	.comment-item-components {
		background-color: white;
		min-height: 392rpx;
		padding: 34rpx;
		box-sizing: border-box;
		margin-top: 20rpx;

		.comment-type-tab {
			display: flex;
			height: 62rpx;
			font-size: 28rpx;

			.item {
				height: 100%;
				border-bottom: 4rpx solid transparent;
				color: #C3C8CE;
				line-height: 62rpx;
				margin-right: 40rpx;
			}

			.active {
				border-bottom: 4rpx solid #007BFF;
				color: rgba(16, 16, 16, 1);
			}
		}

		.empty-box {
			height: 280rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.icon {
				width: 160rpx;
				height: 160rpx;
			}

			.text {
				color: rgba(195, 200, 206, 1);
				font-size: 28rpx;
			}
		}

		.comment-display-box {
			background-color: rgba(245, 245, 247, 1);
			min-height: 80rpx;
			margin-top: 32rpx;
			padding: 24rpx 20rpx;
			box-sizing: border-box;
			position: relative;

			.comment-item-new {
				font-size: 28rpx;

				.user-info {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.flex-layout {
						display: flex;
						align-items: center;
						height: 100%;
					}

					.flex-end {
						display: flex;
						align-items: center;
						justify-content: flex-end;
					}

					.tag {
						width: 40rpx;
						height: 40rpx;
						border-radius: 6rpx;
						font-size: 24rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-weight: bold;
					}

					.red-tag {
						background-color: rgba(255, 100, 95, 0.1);
						color: rgba(255, 100, 95, 1);
					}

					.blue-tag {
						background-color: rgba(52, 132, 253, 0.1);
						color: rgba(52, 132, 253, 1);
					}

					.rmark-icon {
						background-color: rgba(242, 172, 57, 0.1);
						color: rgba(255, 100, 95, 1);

						.icon {
							width: 24rpx;
							height: 24rpx;
						}
					}

					.username {
						color: black;
						color: #0058B6;
					}

					.ml-10 {
						margin-left: 10rpx;
					}

					.mr-10 {
						margin-right: 10rpx;
					}

					.datetime {
						margin-left: 16rpx;
						color: #999999;
						font-size: 24rpx;
					}

					.more {
						width: 32rpx;
						height: 32rpx;
					}
				}

				.blue-content {
					display: flex;
					align-items: center;
					justify-content: flex-end;
				}


				.content {
					color: black;
					margin: 10rpx 0;


					.text {
						width: 100%;
						word-wrap: break-word;
						word-break: break-all;
						padding: 0 10rpx;
						box-sizing: border-box;
					}

					.text-blue {
						text-align: right;
					}


					.mr10 {
						margin-right: 12rpx;
					}

					.add-text {
						color: #0058B6;
					}
				}

				.replay-content {
					padding: 6rpx 12rpx;
					box-sizing: border-box;
					background-color: rgba(238, 238, 238, 1);
					width: fit-content;
					margin-top: 8rpx;
				}
			}

			.comment-item {
				.user-info {
					display: flex;
					justify-content: space-between;

					.username {
						color: black;
						color: #0058B6;
					}

					.datetime {
						margin-left: 16rpx;
						color: #999999;
					}

					.operation-text {
						width: 260rpx;
						text-align: right;

						display: flex;
						align-items: center;
						justify-content: flex-end;
					}
				}

				.content {
					color: black;
					margin: 10rpx 0;
					display: flex;

					.text {
						width: 100%;
						word-wrap: break-word;
						word-break: break-all;
					}


					.mr10 {
						margin-right: 12rpx;
					}

					.add-text {
						color: #0058B6;
					}
				}
			}
		}
	}

	.show-more-box {
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #BBBBBB;
		font-size: 24rpx;

		.icon {
			width: 32rpx;
			height: 32rpx;
		}
	}



	.operation-box {
		position: absolute;
		width: 152rpx;
		border-radius: 6rpx;
		background-color: rgba(255, 255, 255, 1);
		color: rgba(102, 102, 102, 1);
		font-size: 26rpx;
		text-align: center;
		box-shadow: 0px 2px 12rpx 0px rgba(0, 0, 0, 0.2);
		padding: 10rpx 0;
		color: rgba(16, 16, 16, 1);

		.item {
			height: 56rpx;
			line-height: 56rpx;
		}

		.del-item {
			color: #ff645f;
		}
	}
</style>