<template>
	<view class="my-box">
		<view class="my-content">
			<view class="my-top"></view>
			<image class="my-avatar" @tap="$navigateTo('/subPages/myPage/myDetail/myDetail')" :src="user.portrait"
				mode="aspectFill"></image>
			<view class="name" @tap="$navigateTo('/subPages/myPage/myDetail/myDetail')">{{user.userName || '登录/注册'}}
			</view>
			<view class="organization" v-if="user.organizedName"
				@tap="$navigateTo('/subPages/myPage/changeOrganization/changeOrganization')">
				{{user.organizedName}}
			</view>
			<view class="my-list">
				<view class="my-list-box">
					<view class="my-item" @tap="$navigateTo('/subPages/myPage/organization/organization')">
						<view class="my-icon bg3">
							<uv-icon name="home-fill" color="#fff" size="56"></uv-icon>
						</view>
						<view class="item-content">
							我的公司
						</view>
						<view class="my-forward">
							<uv-icon name="arrow-right" color="#c1c0c8" size="32"></uv-icon>
						</view>
					</view>
					<view class="my-item" @tap="$navigateTo('/subPages/myPage/download/download')">
						<view class="my-icon bg5">
							<uv-icon name="grid-fill" color="#fff" size="56"></uv-icon>
						</view>
						<view class="item-content">
							我的下载
						</view>
						<view class="my-forward">
							<uv-icon name="arrow-right" color="#c1c0c8" size="32"></uv-icon>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="fab-box" v-if="isCustomer" @click="$navigateTo('/subPages/myPage/chatList/chatList')">
			<view class="fab-data">
				<uv-badge type="error" max="99" numberType="overflow" :value="badge" :absolute="true" :offset="[0,0]"
					:customStyle="{
						zIndex:99
					}">
				</uv-badge>
				<uv-icon name="server-fill" color="#fff" size="60" labelPos="bottom" labelSize="20">
				</uv-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserInfoById,
		getCustomerServeListByQuery
	} from '@/request/api/my.js'
	export default {
		data() {
			return {
				user: {},
				isCustomer: false,
				badge: 0
			}
		},
		onReady() {
			uni.$on('changeChatList', this.initChat);
		},
		onShow() {
			this.initBadge();
			if (!uni.getStorageSync('token')) {
				this.user = {};
				this.isCustomer = false;
			} else {
				this.user = this.$store.getters.user;
				this.user.organizedName = this.$store.getters.organization && this.$store.getters.organization.name;
				this.init();
				this.initCustomerServe();
			}
		},
		methods: {
			async init() {
				let userInfo = await getUserInfoById(this.user.userId);
				if (userInfo.state) {
					this.user['userName'] = userInfo.data.name;
					this.user['userId'] = userInfo.data.id;
					this.user['portrait'] = userInfo.data.portrait;
					this.$store.dispatch('app/changeUser', this.user);
				}
			},
			async initCustomerServe() {
				let customerServe = await getCustomerServeListByQuery({
					organizationId: this.$store.getters.organization.id,
					userId: this.$store.getters.user.userId
				})
				if (customerServe.state && customerServe.data && customerServe.data.length > 0) {
					let data = customerServe.data[0];
					this.isCustomer = true;
					uni.setStorageSync('serveChatId', data.customerId);
					this.$customerServe.connect(data.customerId);
					this.initChat();
				}
			},
			initChat() {
				this.$customerServe.getConversationList(data => {
					let chatList = data.data.filter(node => node.conversationId !== 'system');
					let badge = 0;
					for (let i = 0; i < chatList.length; i++) {
						badge += chatList[i].unread
					}
					this.badge = badge;
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.my-box {
		height: 100vh;
		background: #fff;
		overflow-y: auto;

		.my-content {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-bottom: 80rpx;
		}

		.my-top {
			height: 260rpx;
			background-image: url('https://images.unsplash.com/photo-1558591710-4b4a1ae0f04d?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=MnwyMDUzMDJ8MHwxfHNlYXJjaHw1fHxhYnN0cmFjdHxlbnwxfHx8fDE2NTI4OTYzNTU&ixlib=rb-1.2.1&q=80&w=1080');
			background-position: center center;
			background-size: cover;
			background-repeat: no-repeat;
			width: 100%;
		}

		.my-avatar {
			width: 200rpx;
			height: 200rpx;
			border-radius: 50%;
			margin-top: -100rpx;
			border: 6rpx solid #fff;
			background: #eee;
		}

		.name {
			font-size: 20px;
			font-weight: 700;
			margin: 10rpx 0;
		}

		.organization {
			font-weight: 300;
		}

		.my-list {
			width: 100%;
			padding: 60rpx 30rpx 30rpx 30rpx;
			box-sizing: border-box;

			.my-list-box {
				border-radius: 24rpx;
				box-shadow: 1px 1px 12px rgba(164, 164, 164, 0.25);
				overflow: hidden;
			}

			.my-item {
				display: flex;
				align-items: center;
				padding: 20rpx 30rpx;
			}

			.my-icon {
				width: 80rpx;
				height: 80rpx;
				border-radius: 16rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				margin-right: 20rpx;
				background: #c1c0c8;

				&.bg1 {
					background: #88d498;
				}

				&.bg2 {
					background: #ffb997;
				}

				&.bg3 {
					background: #7ad9ff;
				}

				&.bg4 {
					background: #9ac0cd;
				}

				&.bg5 {
					background: #40e0d0;
				}
			}

			.item-content {
				flex: 1;
				width: 0;
				overflow: hidden;
				font-weight: 700;
			}
		}

		.fab-box {
			position: fixed;
			bottom: 60rpx;
			right: 30rpx;
			width: 100rpx;
			height: 100rpx;
			border-radius: 50%;
			background: $uv-primary;

			.fab-data {
				width: 100%;
				height: 100%;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>