<template>
	<view class="page-container">
		<view class="mine-header">
			<view class="user-info-box">
				<view class="info-box" v-if="userInfo">
					<image v-if="!userInfo.avatarUrl" src="/static/images/default-avatar.png" class="avatar"></image>
					<image v-else :src="userInfo.avatarUrl" class="avatar"></image>
					<view class="name-class">
						<view class="name">{{userInfo.nickName}}</view>
						<view class="class-name">{{userInfo.dept.deptName}}</view>
					</view>
					<image src="/static/images/mine/detail-icon-white.png" class="more"></image>
				</view>
			</view>
			<view class="stastics-data-box" v-if="analysisData">
				<view class="item">
					<view class="num">{{analysisData.momentCount || 0}}</view>
					<view class="label">动态</view>
				</view>
				<view class="item">
					<view class="num">{{analysisData.commentCount || 0}}</view>
					<view class="label">评论</view>
				</view>
				<view class="item">
					<view class="num">{{analysisData.likeCount || 0}}</view>
					<view class="label">点赞数</view>
				</view>
			</view>
		</view>

		<!-- 我发的动态 -->
		<scroll-view class="my-dynamics" scroll-y :show-scrollbar="false" v-if="list.length >0" @scrolltolower="loadMore">
			<template v-for="(item,index) in list">
				<dynamic-item :form="item" @refresh="handleRefresh" :deleteable="true"></dynamic-item>
			</template>
		</scroll-view>
		<scroll-view class="my-dynamics" scroll-y :show-scrollbar="false" v-if="list.length==0">
			<list-empty></list-empty>
		</scroll-view>
	</view>
</template>

<script>
	import DynamicItem from '@/components/Dynamic-Item.vue';
	import ListEmpty from '@/components/ListEmpty.vue';
	import {
		getInfo
	} from '@/api/login.js';
	import {
		getDynamicStastics,
		userDynamics
	} from '@/api/api.js';
	export default {
		data() {
			return {
				userInfo: undefined,
				analysisData: undefined,
				list: [],
				queryParams: {
					pageNum: 1,
					pageSize: 10
				},
				total:0
			}
		},
		components: {
			DynamicItem,
			ListEmpty
		},
		onLoad() {
			this.getUserInfo();
		},
		onShow() {
			this.getStatistics();
			this.list = [];
			this.queryParams.pageNum = 1;
			this.getList()
		},
		methods: {
			// 当前用户信息
			getUserInfo() {
				getInfo().then(res => {
					this.userInfo = res.data
				})
			},
			// 统计数据
			getStatistics() {
				getDynamicStastics().then(res => {
					console.log(res)
					this.analysisData = res.data
				})
			},
			handleRefresh(){
				this.getStatistics();
				this.list = [];
				this.queryParams.pageNum = 1;
				this.getList()
			},
			// 获取当前用户动态列表
			getList() {
				uni.showLoading({
					title:'加载中...'
				})
				userDynamics(this.queryParams).then(res => {
					this.list = [...this.list,...res.rows];
					this.total = res.total;
				}).finally(()=>{
					uni.hideLoading()
				})
			},
			loadMore() {
				if(this.list.length < this.total){
					this.queryParams.pageNum += 1;
					this.getList()
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/styles/common.scss';

	.mine-header {
		height: 410rpx;

		.user-info-box {
			height: 300rpx;
			background-color: #4873C0;
			background-image: url('/static/images/mine/bg-dec.png');
			background-size: contain;
			background-repeat: no-repeat;
			background-position: bottom;

			.info-box {
				height: 226rpx;
				display: flex;
				padding: 0 36rpx;
				box-sizing: border-box;
				align-items: center;
				justify-content: space-between;

				.avatar {
					width: 112rpx;
					height: 112rpx;
					border-radius: 50%;
				}

				.name-class {
					width: calc(100% - 184rpx);
					color: white;

					.name {
						font-size: 32rpx;
					}

					.class-name {
						font-size: 24rpx;
						margin-top: 10rpx;
					}
				}

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

		.stastics-data-box {
			height: 184rpx;
			width: 92vw;
			margin-top: -74rpx;
			background-color: white;
			border-radius: 12rpx;
			margin-left: 4vw;
			display: flex;
			justify-content: space-evenly;
			align-items: center;

			.item {
				.num {
					color: rgba(0, 0, 0, 1);
					font-size: 32rpx;
					text-align: center;
				}

				.label {
					color: rgba(148, 148, 150, 1);
					font-size: 28rpx;
				}
			}
		}
	}

	.my-dynamics {
		margin-top: 40rpx;
		height: calc(100vh - 570rpx);
	}
</style>
