<template>
	<view class="honour" v-if="isAjaxed">
		<custom-nav title="排行版"></custom-nav>
		<custom-toast ref="toast"></custom-toast>
		<view class="container">
			<view class="certificate"><canvas id="canvas" canvas-id="canvas" :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"></canvas></view>
			<view class="share">
				<view class="item" @tap="saveImg"><view>保存图片</view></view>
				<text>保存图片去朋友圈晒晒吧～</text>
			</view>
		</view>
	</view>
</template>

<script>
import { api_getHonourInfo } from '../../api.js';
import * as filters from '../../common/js/filters.js';
export default {
	data() {
		return {
			pageTitle: '荣耀殿堂', //页面名
			canvasWidth: 0, //canvas 宽
			canvasHeight: 0, //canvas 高
			name: '', //名字
			season: '',
			grade: '', //学位
			year: '', //年
			month: '', //月
			day: '', //日
			fromIndex: true, //是否非分享页面,
			header: '',
			headerImg: '',
			deg: '',
			isAjaxed: false
		};
	},
	computed: {
		userServerInfo() {
			return this.$store.state.userServerInfo;
		},
		userWeixinInfo() {
			return this.$store.state.userWeixinInfo;
		}
	},
	methods: {
		saveImg() {
			//保存证书到本地
			uni.canvasToTempFilePath({
				//首先将 canvas 转化为一个图片
				canvasId: 'canvas'
			})
				.then(([, res]) => {
					//获得图片的临时路径
					return uni.saveImageToPhotosAlbum({
						//将图片保存到系统相册
						filePath: res.tempFilePath
					});
				})
				.then(([err, res]) => {
					if (err && (err.errMsg === 'saveImageToPhotosAlbum:fail:auth denied' || err.errMsg === 'saveImageToPhotosAlbum:fail auth deny')) {
						//如果用户拒绝授权保存图片到相册
						const that = this;
						uni.showModal({
							title: '"大卫博士学位争霸赛"需要保存图片或视频到你的相册',
							content: '',
							success(res) {
								if (res.confirm) {
									wx.openSetting({
										success(res) {
											that.saveImg();
											return;
										}
									});
								}
							}
						});
					} else if (err && err.errMsg === 'saveImageToPhotosAlbum:fail authorize no response') {
						uni.showModal({
							content: '保存图片失败,请手动截图保存',
							showCancel: false
						});
					}
					if (res && res.errMsg === 'saveImageToPhotosAlbum:ok') {
						uni.showModal({
							content: '图片已经保存成功',
							showCancel: false
						});
					}
				});
		}
	},
	async onLoad(opt) {
		//当进入页面之后判断是否带带有参数
		uni.hideShareMenu();
		this.header = this.userServerInfo.avatar
		uni.showLoading({
			title: '加载中',
			mask: true
		});
		let _this = this;
		uni.downloadFile({
			url: this.header,
			success: ({ statusCode, tempFilePath }) => {
				if (statusCode === 200) {
					this.headerImg = tempFilePath;
				}
			},
			fail: e => {
				console.log(e);
				uni.showToast({
					icon: 'none',
					title: '获取用户头像失败',
					duration: 3000
				});
			},
			complete: () => {
				uni.getImageInfo({
					src: 'https://api.jiuweiyun.cn/public/uploads/weapp/icon/phb_bg.png',
					success: res => {
						this.isAjaxed = true;
						uni.hideLoading(); //停止 loading
						_this.canvasWidth = uni.upx2px(750);
						_this.canvasHeight = uni.upx2px(1385);
						_this.name = '刘德华' || _this.userServerInfo.name; //姓名
						_this.name = _this.name.slice(0, 6);
						_this.season = 23 || _this.userServerInfo.season; // 赛季季数
						const ctx = uni.createCanvasContext('canvas');
						ctx.drawImage(res.path, 0, 0, _this.canvasWidth, _this.canvasHeight);
						ctx.save();
						ctx.beginPath();
						ctx.arc(uni.upx2px(375), uni.upx2px(844), uni.upx2px(64), 0, 2 * Math.PI, false);
						ctx.clip();
						ctx.drawImage(_this.headerImg, uni.upx2px(311), uni.upx2px(780), uni.upx2px(128), uni.upx2px(128));
						ctx.restore();
						ctx.setTextAlign('center');
						ctx.setFontSize(uni.upx2px(30));
						ctx.setFillStyle('#F85820');
						ctx.fillText(`${_this.userServerInfo.score}分`, uni.upx2px(470), uni.upx2px(1080));
						ctx.fillText(
							`${_this.userServerInfo.rank}名（超过${_this.userServerInfo.score === 0 ? 0 : _this.userServerInfo.count - _this.userServerInfo.rank}人）`,
							uni.upx2px(480),
							uni.upx2px(1204)
						);
						ctx.fillText(_this.userServerInfo.name, uni.upx2px(380), uni.upx2px(960));
						ctx.setFillStyle('#FFFFFF');
						//隐藏等级
						// ctx.fillText(`${_this.userServerInfo.level_name}排行榜`, uni.upx2px(375), uni.upx2px(405));
						ctx.fillText(`排行榜`, uni.upx2px(375), uni.upx2px(405));
						ctx.setFontSize(uni.upx2px(28));
						ctx.fillText(
							`大卫博士学位争霸赛第${_this.userServerInfo.season}季 ${filters.dateFormatter(Date.now(), 'yyyy年MM月dd日 hh:mm')}`,
							uni.upx2px(370),
							uni.upx2px(1350)
						);
						ctx.draw();
						_this.$hideLoading();
					},
					fail: err => {
						uni.showModal({
							content: '获取背景图失败',
							showCancel: false,
							success: res => {
								if (res.confirm) {
									uni.navigateBack({});
								}
							}
						});
					}
				});
			}
		});
	}
};
</script>

<style lang="scss">
.container {
	.certificate {
		.bg {
			width: 100%;
			height: 100%;
		}
	}
	.share {
		padding: 22rpx 90rpx;
		background-color: #ffbf2a;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		color: #fa6b3d;
		font-size: 32rpx;
		.item {
			width: 100%;
			height: 110rpx;
			line-height: 110rpx;
			text-align: center;
			background-color: #f85820;
			border-radius: 110rpx;
			padding: 0;
			margin: 0;
			box-shadow: 0 0 5px rgba(233, 87, 55, 0.45);
			&::after,
			&::before {
				border: none;
			}
			color: #ffd252;
			font-size: 50rpx;
			margin-bottom: 16rpx;
		}
	}
}
.honour {
	@include page();
	height: auto;
	min-height: 100%;
	overflow: inherit;
	&.share {
		border-top-color: #493225;
	}
	.content {
		display: flex;
		flex-direction: column;
		.top {
			width: 100%;
			height: 680rpx;
			background: #493225;
			border-bottom-right-radius: 375rpx 123rpx;
			border-bottom-left-radius: 375rpx 123rpx;
			position: relative;
			&.share {
				height: 890rpx;
			}
			#canvas {
				position: absolute;
				top: 56rpx;
				left: 50%;
				transform: translateX(-50%);
				&.share {
					top: 0;
				}
			}
			.bg {
				position: absolute;
				width: 566rpx;
				height: 464rpx;
				top: 35rpx;
				left: 50%;
				z-index: 1;
				transform: translateX(-50%);
			}
			.all {
				position: absolute;
				right: 32rpx;
				bottom: -13rpx;
				font-size: 26rpx;
				color: #fa6342;
			}
		}
		.bot {
			flex: 1;
			box-sizing: border-box;
			padding: 100rpx;
			.title {
				text-align: center;
				height: 45rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 36rpx;
				color: #fa6342;
				text {
					margin: 0 40rpx;
				}
				image {
					width: 48rpx;
					height: 45rpx;
				}
			}
			.text {
				margin-top: 80rpx;
				font-size: 30rpx;
				color: #666666;
			}
			.share-save {
				height: 60rpx;
				margin-top: 80rpx;
				display: flex;
				justify-content: space-between;
				.button {
					height: 100%;
					width: 240rpx;
					box-sizing: border-box;
					border: 2rpx solid #fa6342;
					border-radius: 30rpx;
					line-height: 56rpx;
					text-align: center;
					color: #fa6342;
					background: #ffffff;
					padding: 0;
					font-size: 24rpx;
					text {
						margin: 0 6rpx;
						vertical-align: middle;
						&.cuIcon {
							font-size: 36rpx;
						}
					}
				}
			}
		}
	}
}
</style>
