<template>
	<view class="page">
		<view class="profile">
			<view class="bg">
				<view class="bg1"></view>
				<view class="bg2"></view>
			</view>
			<view class="box">
				<view class="title" @click="setting">
					<view class="left">
						<view class="avatar">
							<image :src="user.avatar">
						</view>
						<view class="name">
							<view class="name1">{{ user.name }}</view>
							<view class="name2">{{ user.cate_name }}</view>
						</view>
					</view>
					<view class="icon flex">
						<image src="../../static/images/arrow-right.png" style="width: 20rpx; height: 20rpx;" />
					</view>
				</view>
				<view class="list">
					<view class="item" @click="rank">
						<view class="number">{{ user.current_score }}</view>
						<view class="text">当前得星</view>
					</view>
					<view class="border"></view>
					<view class="item" @click="rank">
						<view class="number">{{ user.rank }}</view>
						<view class="text">总排名</view>
					</view>
					<view class="border"></view>
					<view class="item" @click="rank">
						<view class="number">{{ user.score }}</view>
						<view class="text">累计得星</view>
					</view>
				</view>
			</view>
		</view>
		<view class="redar">
			<view id="echart" style="height: 250px;width: 330px;"></view>
		</view>
		<view class="options">
			<u-cell-group>
				<u-cell title="我的填报" isLink @click="scoreList" />
			</u-cell-group>
		</view>
		<view class="logout">
			<u-button text="退出" type="error" @click="logout" />
		</view>
	</view>
</template>

<script>
	import * as echarts from 'echarts';
	import { setToken } from '../../utils/index'
	export default {
		data() {
			return {
				user: {
					name: '',
					cate_name: '',
					avatar: '',
					current_score: 0,
					score: 0,
					rank: '-'
				}
			}
		},
		onLoad() {
			this.init()
		},
		methods: {
			async init() {
				uni.showLoading()
				let res = await this.$ajax.get('/api/user/profile')
				let radarValue = {}
				if (res.status == 0) {
					this.user.name = res.data.name
					this.user.cate_name = res.data.cate_name
					this.user.avatar = res.data.avatar ?? '../../static/images/default-avatar.png'
					this.user.current_score = res.data.current_score
					this.user.score = res.data.score
					radarValue = res.data.scores
				}
				res = await this.$ajax.get('/api/rank/user')
				if (res.status == 0) {
					if (res.data.current) {
						this.user.rank = res.data.current.rank
					}
				}
				res = await this.$ajax.get('/api/keyword', { params: { key: 'score_cate_' } })
				let radarIndicator = [
					{ name: '政治忠诚' },
					{ name: '政治定力' },
					{ name: '政治担当' },
					{ name: '政治能力' },
					{ name: '政治自律' },
				]
				let radarValues = []
				if (res.status == 0) {
					radarIndicator = res.data.map(item => {
						radarValues.push(radarValue[item.key])
						return {key: item.key, name: item.name}
					})
				}
				uni.hideLoading()
				var option = {
					radar: {
						indicator: radarIndicator
					},
					series: [
						{
							type: 'radar',
							data: [
								{
									value: radarValues,
								}
							]
						}
					]
				};
				this.$nextTick(() => {
					this.chart(option)
				})
			},
			chart(option) {
				if (document.getElementById('echart')) {
					var chartElement = echarts.init(document.getElementById('echart'));
					chartElement.setOption(option)
				}
			},
			logout() {
				uni.showModal({
					title: '是否确定?',
					content: '退出登录',
					success: (res) => {
						if (res.confirm) {
							this.$ajax.get('/api/logout').then(res => {
								if (res.status == 0) {
									setToken('')
									uni.reLaunch({
										url: '/pages/auth/login'
									})
								}
							})
						}
					}
				})
			},
			setting() {
				uni.navigateTo({
					url: '/pages/user/setting'
				})
			},
			scoreList() {
				uni.navigateTo({
					url: '/pages/score/list'
				})
			},
			rank() {
				uni.navigateTo({
					url: '/pages/score/user-rank?type=user'
				})
			}
		}
	}
</script>

<style>
	.profile {
		position: relative;
	}
	.profile .avatar {
		display: inline-block;
	}
	.profile .left {
		display: flex;
		align-items: center;
	}
	.profile .name {
		margin-left: 20rpx;
	}
	.profile .avatar ::v-deep uni-image {
		border-radius: 100%;
		width: 120rpx;
		height: 120rpx;
	}
	.profile .bg {
		height: 400rpx;
	}
	.profile .bg1 {
		width: 100%;
		height: 200rpx;
		background-color: #0da0f5;
	}
	.profile .bg2 {
		width: 100%;
		height: 200rpx;
		background-color: white;
	}
	.profile .box {
		background-color: white;
		border: 1px solid #dddddd;
		border-radius: 20rpx;
		position: absolute;
		top: 10%;
		left: 3%;
		width: 700rpx;
		
		padding: 30rpx;
		box-sizing: border-box;
	}
	.profile .title {
		display: flex;
		justify-content: space-between;
	}
	.profile .name1 {
		font-weight: bold;
		font-size: 38rpx;
	}
	.profile .name2 {
		margin-top: 10rpx;
		font-size: 25rpx;
		color: gray;
	}
	.profile .list {
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		padding: 0 30rpx;
		text-align: center;
	}
	.list .border {
		width: 1px;
		border-left: 1px solid #cbcbcb;
	}
	.list .number {
		font-weight: bold;
		font-size: 38rpx;
	}
	.list .text {
		margin-top: 10rpx;
		font-size: 25rpx;
		color: gray;
	}
	.redar {
		background-color: white;
		margin: 10rpx auto;
		padding: 10rpx 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.options {
		margin-top: 10rpx;
		background-color: white;
	}

	.logout {
		margin-top: 20rpx;
		padding: 0 20rpx;
	}
</style>
