<template>
	<view class="container">
		<view class="head">
			<image :src="$picUrl+'/static/dynamic-banner.png'" mode="aspectFill"></image>
		</view>
		<view class="box">
			<view class="tab-list">
				<view v-for="(item, index) in tabList" :key="index" class="tab-item" :class="{ 'active': activeTab === item }" @click="activeTab = item">{{ item }}</view>
			</view>
			<view class="list">
				<view v-for="(item, index) in dynamicList" :key="index" class="dynamic-item">
					<view class="user">
						<image v-if="item.avatar" :src="item.avatar" mode="aspectFill"></image>
						<image v-else src="/static/touxiang.png" mode="aspectFill"></image>
						<view class="user-info">
							<view class="name">{{ item.name }}</view>
							<view class="time">{{ item.time }}</view>
						</view>
					</view>
					<view class="cont">
						{{ showFullCont.includes(index) ? item.content :  calcContentWidth(item.content).splitStr }}
						<view v-if="!showFullCont.includes(index) && calcContentWidth(item.content).isSplit" class="show-more" @click="showFullCont.push(index)">全文</view>
					</view>
					<view v-if="item.pic && item.pic.length > 0" class="pic-list">
						<image v-for="(pic, k) in item.pic" :key="k" :src="pic" mode="aspectFill"></image>
					</view>
					<view class="btns">
						<view class="btn" v-if="item.content" @click="clipCont(item.content)">
							<image src="/static/copy.png" mode="aspectFill"></image>
							<view>复制文案</view>
						</view>
						<view class="btn" v-if="item.pic && item.pic.length > 0" @click="downloadMultipleImages(item.pic)">
							<image src="/static/download.png" mode="aspectFill"></image>
							<view>下载图片</view>
						</view>
						<view class="btn" @click="togggleLike(item.id)">
							<image v-if="!item.isLike" src="/static/nolike.png" mode="aspectFill"></image>
							<image v-else src="/static/like.png" mode="aspectFill"></image>
							<view>{{ item.likeNum }}</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="dynamicList.length && !loading" class="finished">{{ loadTitle }}</view>
			<view v-if="finished && !dynamicList.length" class="empty">
			  <image mode="aspectFill" src="@/static/empty.png" alt="暂无动态" />
			  <view>暂无动态</view>
			</view>
			<view class="publish" @click="publish">
				<image src="/static/publish.png" mode="aspectFill"></image>
				<text>发布</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabList: ['推荐', '精选', '晒单', '日常', '文章'],
				activeTab: '推荐',
				dynamicList: [
					{
						avatar: '',
						name: '幽默的先生',
						time: '11:23',
						content: '今天天气真好啊！碧蓝的天空，绿油油的油菜花，空 中飘香，远远的山脉看着忽远忽近。',
						pic: ['', '', '', '', ''],
						likeNum: 1000,
						isLike: true,
					},
					{
						avatar: '',
						name: '幽默的先生',
						time: '4月12日  11:21',
						content: '今天天气真好啊！碧蓝的天空，绿油油的油菜花，空 中飘香，远远的山脉看着忽远忽近。远远的山脉看着忽远忽近。远远的山脉看着忽远忽近。远远的山脉看着忽远忽近。远远的山脉看着忽远忽近。',
						pic: ['', '', '', '', ''],
						likeNum: 1000,
						isLike: false,
					},
				],
				page: 1,
				limit: 10,
				loadTitle: '',
				loading: false,
				finished: false,
				showFullCont: [],
			};
		},
		onLoad() {
			uni.setNavigationBarColor({
				frontColor: '#000000',
				backgroundColor: '#E7F1FC',
			})
		},
		methods: {
			calcContentWidth(cont) {
				let len = 0;
				let spliti = 0;
				for (var i = 0; i < cont.length; i++) {
						if (cont.charCodeAt(i) > 255 || cont.charCodeAt(i<0)) {
							len += 2;
						} else {
							len ++;
						}
						if (len > 84 && spliti === 0) {
							spliti = i;
						}
				}
				console.log(cont, spliti, len);
				return {
					isSplit: len > 88,
					splitStr: len > 88 ? `${cont.split('').splice(0, spliti).join('')}...` : cont,
				};
			},
			clipCont(cont) {
				uni.setClipboardData({
				  data: cont
				});
			},
			downloadSingleImage(url) {
				console.log(url);
			  return new Promise((resolve, reject) => {
			    uni.downloadFile({
			      url: url,
			      success: (res) => {
			        if (res.statusCode === 200) {
			          resolve(res.tempFilePath)
			        } else {
			          reject(new Error('下载失败'))
			        }
			      },
			      fail: (err) => {
			        reject(new Error('下载失败'))
			      }
			    })
			  })
			},
			downloadMultipleImages(urls) {
			  let tasks = []
			  urls.forEach((url) => {
			    tasks.push(this.downloadSingleImage(url).then(e => console.log(e)))
			  })
			  return Promise.all(tasks)
			},
			togggleLike() {
				
			},
			publish() {
				uni.navigateTo({
					url: '/pages/squareDynamic/publish',
				})
			},
		},
	};
</script>

<style lang="scss" scoped>
	.container {
		background: #F7F8FA;
		padding-bottom: 55rpx;
		padding-top: 292rpx;
		.head {
			height: 450rpx;
			width: 100%;
			position: absolute;
			top: -88rpx;
			background: linear-gradient(0deg, #F7F8FA, #E0EFFD);
			z-index: 0;
			>image {
				position: absolute;
				top: 138rpx;
				left: 50%;
				transform: translateX(-50%);
				width: 669rpx;
				height: 221rpx;
			}
		}
		.box {
			position: relative;
			width: 100%;
			// top: 292rpx;
			padding: 24rpx;
			.tab-list {
				display: flex;
				justify-content: space-between;
				.tab-item {
					width: 130rpx;
					height: 80rpx;
					background: #FFFFFF;
					border-radius: 10rpx;
					opacity: 0.7;
					font-size: 30rpx;
					color: #999999;
					line-height: 80rpx;
					text-align: center;
					&.active {
						box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(32,39,76,0.1);
						opacity: 1;
						color: #333333;
					}
				}
			}
			.list {
				margin-top: 30rpx;
				.dynamic-item {
					width: 100%;
					background: #FFFFFF;
					border-radius: 20rpx;
					padding: 30rpx;
					margin-bottom: 20rpx;
					&:last-child {
						margin-bottom: 0;
					}
					.user {
						display: flex;
						>image {
							width: 80rpx;
							height: 80rpx;
							margin-right: 24rpx;
						}
						.user-info {
							.name {
								font-size: 30rpx;
								color: #222222;
								line-height: 30rpx;
								margin-bottom: 26rpx;
							}
							.time {
								font-size: 24rpx;
								color: #999999;
								line-height: 24rpx;
							}
						}
					}
					.cont {
						margin-top: 30rpx;
						font-size: 28rpx;
						line-height: 40rpx;
						position: relative;
						color: #222222;
						.show-more {
							position: absolute;
							right: 10rpx;
							bottom: 0;
							color: #F34A40;
						}
					}
					.pic-list {
						margin-top: 24rpx;
						>image {
							width: 210rpx;
							height: 210rpx;
							margin-right: 6rpx;
							margin-bottom: 6rpx;
							border-radius: 10rpx;
							&:nth-child(3n) {
								margin-right: 0;
							}
						}
					}
					.btns {
						margin-top: 20rpx;
						display: flex;
						justify-content: flex-end;
						.btn {
							display: flex;
							flex-direction: column;
							align-items: center;
							font-size: 24rpx;
							line-height: 24rpx;
							color: #999999;
							margin-left: 40rpx;
							image {
								width: 40rpx;
								height: 40rpx;
								margin-bottom: 10rpx;
							}
						}
					}
				}
			}
			.finished,
			.loading {
			  font-size: 28rpx;
			  line-height: 100rpx;
			  text-align: center;
			  color: #bbb;
			}
			.empty {
			  margin-top: 100rpx;
			  font-size: 28rpx;
			  text-align: center;
			  color: #bbb;
			}
			
			.empty image {
			  display: block;
			  width: 414rpx;
			  height: 305rpx;
			  margin: 0 auto;
			  pointer-events: none;
			}
			.publish {
				position: fixed;
				right: 24rpx;
				bottom: 200rpx;
				width: 100rpx;
				height: 100rpx;
				background: #F34A40;
				border-radius: 50%;
				color: #fff;
				font-size: 24rpx;
				line-height: 24rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				>image {
					width: 35rpx;
					height: 28rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>