<template>
	<view class="content">
		<view class="row1">
			<text class="tit">头像</text>
			<view class="background-img" @click.stop="imgsub()">
				<image class="background-img" v-model="userInfo.avatar" :src="userInfo.avatar" mode="aspectFill">
				</image>
			</view>
		</view>
		<view class="row">
			<text class="tit">昵称</text>
			<input class="input" type="text" v-model="userInfo.nickname" placeholder-class="placeholder" />
		</view>
		<view class="row">
			<text class="tit">ID</text>
			<input class="input" type="text" disabled="true" v-model="userInfo.uid" placeholder-class="placeholder" />
		</view>
		<view class="row">
			<text class="tit">账户</text>
			<input class="input" type="text" disabled="true" v-model="userInfo.phone" placeholder-class="placeholder" />
		</view>
		<view class="add-btn" @click="confirm">提交</view>
		<canvas canvas-id="canvas" v-if="canvasStatus"
			:style="{width: canvasWidth + 'px', height: canvasHeight + 'px',position: 'absolute',left:'-100000px',top:'-100000px'}"></canvas>
		<uni-popup ref="popup" type="center">
			<view class="alertprogress">
			{{progress}}%
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		upload
	} from '@/api/order.js';
	import {
		userEdit,
		logout,
		test
	} from '@/api/set.js';
	export default {
		data() {
			return {
				show: false,
				password: '',
				upFileLoding: false,
				canvasStatus: false,
				canvasWidth:0,
				canvasHeight:0,
				progress:0,
			}
		},
		onLoad() {
			console.log(this.userInfo)
			test();
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		methods: {
			...mapMutations('user', ['logout']),
			imgsub() {
				const that = this;
					if (that.upFileLoding) {
						return
					}
					that.upFileLoding = true;
					that.canvasStatus = true;
					setTimeout(() => {
						that.upFileLoding = false;
					}, 1000);
					upload({
						filename: ''
					}, (res) => {
						that.userInfo.avatar = res[0].url;
						that.canvasStatus = false
					}, (res) => {
						that.canvasStatus = false
					}, (res) => {
						that.canvasWidth = res.w
						that.canvasHeight = res.h
					},(res)=>{
						// console.log(res,'res');
						if(res.progress==100){
							this.$refs.popup.close()
							uni.showToast({
								title: '上传成功'
							});
						}else if(res.progress==0){
							this.$refs.popup.open()
						}
						that.progress = res.progress;
						// console.log(res,'进度条');
					})
			},
			confirm() {
				userEdit({
						avatar: this.userInfo.avatar,
						nickname: this.userInfo.nickname
					})
					.then(e => {
						this.$api.msg('修改成功');
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/user/user'
							});
						}, 1000);
						console.log(e);
					})
					.catch(e => {
						console.log(e);
					});
			},
			toLogout() {
				let obj = this;
				uni.showModal({
					content: '确定要退出登录么',
					success: (e) => {
						if (e.confirm) {
							logout({}).then((e) => {
								uni.navigateBack();
							}).catch((e) => {
								console.log(e);
							})
							obj.logout();
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.row1 {
		display: flex;
		align-items: center;
		justify-content: space-between;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;
		margin-bottom: 20upx;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: $font-lg;
			color: $font-color-dark;
		}

		.background-img {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background: #f2f2f2;
		}
	}

	.row {
		display: flex;
		align-items: center;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: $font-lg;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: $font-base;
			color: $color-gray;
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 158rpx auto 30rpx;
		width: 560rpx;
		height: 80rpx;
		background: #DC262B;
		border-radius: 40px;
		color: #FFFFFF;
	}

	.out {
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 auto 30rpx;
		width: 560rpx;
		height: 80rpx;
		border: 1px solid #32C6FF;
		background: #FFFFFF;
		border-radius: 40px;
		color: #32C6FF;
	}

	.psw-wrapper {
		width: 548rpx;
		height: 344rpx;
		background-color: #ffffff;

		.psw-title {
			width: 100%;
			font-size: 35rpx;
			padding: 43rpx 0 49rpx;
			text-align: center;
			font-weight: 800;
		}

		.psw-ipt {
			display: block;
			background-color: #dce3ed;
			height: 90rpx;
			width: 464rpx;
			padding-left: 30rpx;
			margin: 0 auto;
			font-size: 80rpx;
		}

		.psw-btn text {
			display: inline-block;
			text-align: center;
			width: 50%;
			padding-top: 29rpx;
			font-size: 35rpx;
		}

		.psw-qd {
			color: #32C6FF;
		}
	}
</style>