<template>
	<view style="background: #F1F2F8;min-height: 100vh;" v-cloak>
		<tn-nav-bar :isBack="false" backTitle="" :bottomShadow="true" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view style="padding-left: 15rpx;" @click="goBack()">
					<text class="tn-icon-left" style="font-size: 40rpx;"></text>
				</view>
				<view style="width: 83%;font-weight: bold;">
					<text>动态详情</text>
				</view>
			</view>
		</tn-nav-bar>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}" style="padding-bottom: 200rpx;">
			<view style="background-color: #FFFFFF;margin: 20rpx;border-radius: 20rpx;">
				<view style="padding:30rpx;">
					<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between" style="width: 100%;">
						<view class="tn-flex tn-flex-center tn-flex-col-center" style="width: 100%;">
							<view @click="tn('/pages/packageA/user/my_card?id='+info.member.id)">
								<image v-if="info.member.photo_image==''||info.member.photo_image==null"
									src="/static/def.png" style="width: 100rpx;height: 100rpx;border-radius: 50%;">
								</image>
								<image v-if="info.member.photo_image!=''&&info.member.photo_image!=null"
									:src="apiImgUrl+info.member.photo_image"
									style="width: 100rpx;height: 100rpx;border-radius: 50%;"></image>
							</view>
							<view style="margin-left: 10rpx;width: 100%;">
								<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between">
									<view style="font-size: 30rpx;color: #181818;font-weight: bold;">
										<text
											style="vertical-align: middle;margin-right: 10rpx;">{{info.member.nikename}}</text>
										<!-- <tn-tag width="80rpx" shape="radius" size="sm" backgroundColor="#66B5FF"
											fontColor="#FFFFFF">供给
										</tn-tag> -->
									</view>
									<view>
										<tn-button @click="tn('/pages/packageA/user/my_card?id='+info.member.id)"
											padding="20rpx 20rpx" backgroundColor="rgba(48,86,211,0.1)" size="sm"
											fontColor="#3056D3" shape="round">
											<text style="font-size: 24rpx;">联系ta</text>
										</tn-button>
									</view>
								</view>
								<view class="tn-text-ellipsis"
									style="width: 380rpx;font-size: 26rpx;color: #666666;font-weight: 500;margin-top: 5rpx;">
									{{info.member.enterprise_name}}
								</view>
							</view>
						</view>

					</view>
					<view class="tn-text-ellipsis-2" style="font-size: 32rpx;font-weight: bold;margin-top: 20rpx;">
						{{info.title}}
					</view>
					<view class="tn-text-ellipsis-2" style="font-size: 28rpx;color: #666666;margin-top: 20rpx;">
						{{info.content}}
					</view>
					<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between"
						style="margin-top: 20rpx;">
						<view style="color: #AAAAAA;font-size: 24rpx;">
							发布于:{{info.createtime}}
						</view>
						<!-- <view style="color: #3056D3;font-size: 28rpx;font-weight: 500;">
							全文
						</view> -->
					</view>
					<view v-if="info.files.length>0" style="margin-top: 30rpx;">
						<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-wrap">
							<view class="tn-flex-basic-xs" v-for="(img,img_i) in info.files"
								style="text-align: center;">
								<view style="padding-right: 10rpx;">
									<image :src="apiImgUrl+img" @click="openImg(info,img_i)" mode="aspectFill"
										style="width: 200rpx;height: 200rpx;border-radius:20rpx;">
									</image>
								</view>
							</view>
						</view>
					</view>
					<view style="padding:30rpx;width: 80%;margin: 0 auto;padding-top: 40rpx;">
						<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between">
							<view style="text-align: center;font-size: 28rpx;">
								<button hover-class="none" open-type="share">
									<text class="tn-icon-send" style="font-size: 32rpx;"></text>
									<view>分享</view>
								</button>
							</view>
							<view style="text-align: center;font-size: 28rpx;" @click="like(index)">
								<text v-if="info.is_like==1" class="tn-icon-praise-fill tn-color-red"
									style="font-size: 32rpx;"></text>
								<text v-if="info.is_like==0" class="tn-icon-praise" style="font-size: 32rpx;"></text>
								<view>{{info.like_num>0?info.like_num:'点赞'}}</view>
							</view>
							<view style="text-align: center;font-size: 28rpx;" @click="favs(index)">
								<text v-if="info.is_fav==1" class="tn-icon-star-fill tn-color-orangeyellow"
									style="font-size: 32rpx;"></text>
								<text v-if="info.is_fav==0" class="tn-icon-star" style="font-size: 32rpx;"></text>
								<view>{{info.fav_num>0?info.fav_num:'收藏'}}</view>
							</view>
						</view>
					</view>
					<view style="margin-top: 40rpx;">
						<view style="color: #181818;font-size: 34rpx;font-weight: bold;">评论（{{com_info.total}})</view>
						<view style="padding-top: 30rpx;">
							<view v-for="(item,index) in com_list" style="width: 100%;margin: 50rpx 0rpx;">
								<view class="tn-flex tn-flex-center tn-flex-col-center" style="width: 100%;">
									<view>
										<image src="/static/def.png"
											style="width: 100rpx;height: 100rpx;border-radius: 50%;">
										</image>
									</view>
									<view style="margin-left: 10rpx;width: 100%;">
										<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between">
											<view style="font-size: 30rpx;color: #181818;font-weight: bold;">
												<text
													style="vertical-align: middle;margin-right: 10rpx;">{{item.member.nikename}}</text>
												<!-- <tn-tag width="80rpx" shape="radius" size="sm" backgroundColor="#66B5FF"
													fontColor="#FFFFFF">供给
												</tn-tag> -->
											</view>
											<view v-if="item.member_id==uid || uid==info.member_id"
												style="text-align: center;font-size: 28rpx;" @click="del(item)">
												<!-- <text class="tn-icon-praise" style="font-size: 32rpx;"></text>
												<view>21</view> -->
												<view style="color: #AAAAAA;font-size: 24rpx;">删除</view>
											</view>
										</view>
										<view class="tn-text-ellipsis"
											style="width: 380rpx;font-size: 26rpx;color: #666666;font-weight: 500;margin-top: 5rpx;">
											{{item.createtime}}
										</view>
									</view>
								</view>
								<view style="color: #666666;padding-top: 30rpx;">
									{{item.content}}
								</view>

								<view v-if="index<com_list.length-1"
									style="height: 1rpx;border-top: #BABDC7 1rpx dashed;margin: 50rpx 0rpx;"></view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view
			style="position: fixed;bottom:0px;width: 100%;background-color: #FFFFFF;box-shadow: 0rpx -2rpx 4rpx 0rpx rgba(186,189,199,0.2);padding:30rpx 30rpx 50rpx 30rpx;">
			<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-between">
				<view style="background: #F1F2F8;border-radius: 60rpx;padding:0rpx 20rpx;height: 80rpx;width: 75%;">
					<tn-input :maxLength="100" :height="80" v-model="content" placeholder="输入发表评论"
						:customStyle="{width:'450rpx'}" :clearable="false" />
				</view>
				<view @click="submitDo"
					style="line-height: 80rpx;height: 80rpx;font-size: 28rpx;font-weight: bold;text-align: center;width: 150rpx;color: #ffffff;;background: #3056D3;border: 1rpx solid #3056D3;border-radius: 90rpx;">
					<text>发布</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		tweetsInfo,
		likeDo,
		favsDo,
		likeRevoke,
		favsRevoke,
		postComment,
		commentList,
		addComment,
		removeComment
	} from '@/util/api.js';
	import store from '@/store/index.js'
	export default {
		data() {
			return {
				id: 0,
				info: {},
				apiImgUrl: this.$store.state.imgUrl,
				content: '',
				com_list: [],
				com_info: {},
				page: 1,
				uid: 0,
			}
		},
		onLoad(d) {
			var uid = uni.getStorageSync('uid');
			this.uid = uid;
			this.id = d.id;
			this.getTweets();
			this.getCommentList();
		},
		onReachBottom() {
			this.page += 1;
			this.getCommentList();
		},
		methods: {
			openImg(item, index) {
				console.log(index);
				console.log(item.files);
				var img = item.files;
				var apiImgUrl = this.apiImgUrl;
				const updatedImagePaths = img.map(path => `${apiImgUrl}${path}`);
				console.log(updatedImagePaths);
				uni.previewImage({
					current: index,
					urls: updatedImagePaths,
				});
			},
			submitDo() {
				addComment({
						tweet_id: this.id,
						content: this.content,
						member_b_id: this.info.member_id
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showToast({
								title: '评论成功！',
								icon: 'none',
								duration: 2000
							});
						} else {
							uni.showToast({
								title: '评论失败！',
								icon: 'none',
								duration: 2000
							});
						}
						this.content = '';
						this.page = 1;
						this.com_list = [];
						this.getCommentList();
						//this.com_list = res.data;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getCommentList() {
				commentList({
						tweet_id: this.id,
						page: this.page,
						size: 15,
					})
					.then(res => {
						console.log(res);
						this.com_list.push(...res.data.data);
						this.com_info = res.data;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			getTweets() {
				tweetsInfo({
						id: this.id
					})
					.then(res => {
						console.log(res);
						this.info = res.data;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},

			like() {
				var uid = uni.getStorageSync('uid');
				if (!uid) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.info.is_like == 0) {
					likeDo({
							member_id: uid,
							related_id: this.id,
							related_type: 'tweets',
						})
						.then(res => {
							console.log(res);
							uni.showToast({
								title: '赞成功！',
								icon: 'none',
								duration: 2000
							});
							this.info.is_like = 1;
						})
						.catch(error => {
							uni.showToast({
								title: error,
								icon: 'none',
								duration: 2000
							});
						})
				} else {
					likeRevoke({
							member_id: uid,
							related_id: this.id,
							related_type: 'tweets',
						})
						.then(res => {
							console.log(res);
							uni.showToast({
								title: '取消成功！',
								icon: 'none',
								duration: 2000
							});
							this.info.is_like = 0;
						})
						.catch(error => {
							uni.showToast({
								title: error,
								icon: 'none',
								duration: 2000
							});
						})
				}

			},
			favs() {
				var uid = uni.getStorageSync('uid');
				if (!uid) {
					uni.showToast({
						title: '请先登录',
						icon: 'none',
						duration: 2000
					});
					return;
				}
				if (this.info.is_fav == 0) {
					favsDo({
							member_id: uid,
							related_id: this.id,
							related_type: 'tweets',
						})
						.then(res => {
							console.log(res);
							uni.showToast({
								title: '收藏成功！',
								icon: 'none',
								duration: 2000
							});
							this.info.is_fav = 1;
						})
						.catch(error => {
							uni.showToast({
								title: error,
								icon: 'none',
								duration: 2000
							});
						})
				} else {
					favsRevoke({
							member_id: uid,
							related_id: this.id,
							related_type: 'tweets',
						})
						.then(res => {
							console.log(res);
							uni.showToast({
								title: '取消收藏成功！',
								icon: 'none',
								duration: 2000
							});
							this.info.is_fav = 0;
						})
						.catch(error => {
							uni.showToast({
								title: error,
								icon: 'none',
								duration: 2000
							});
						})
				}

			},
			del(item) {
				removeComment({
						id: item.id,
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showToast({
								title: '删除成功！',
								icon: 'none',
								duration: 2000
							});
							this.page = 1;
							this.com_list = [];
							this.getCommentList();
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
								duration: 2000
							});
						}


					})
					.catch(error => {
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
					})
			},
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/index/index'
					})

				}
			}
		}
	}
</script>

<style>
	button::after {
		border: none;
	}

	button {
		position: relative;
		display: block;
		margin-left: auto;
		margin-right: auto;
		padding-left: 0px;
		padding-right: 0px;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		line-height: 1.35;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		background-color: transparent;
		font-size: 28rpx;
		width: 100%;
		height: 100%;
	}
</style>