<template>
	<view class="content">
		<view class="row1">
			<text class="tit">头像</text>
			<!-- #ifdef APP -->
			<view class="background-img" @click="openAuth">
				<image class="background-img" v-model="userInfo.avatar" :src="userInfo.avatar" mode="aspectFill">
				</image>
			</view>
			<!-- #endif -->
			<!-- #ifndef APP -->
			<view class="background-img" @click="imgsub">
				<image class="background-img" v-model="userInfo.avatar" :src="userInfo.avatar" mode="aspectFill">
				</image>
			</view>
			<!-- #endif -->
		</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" v-if="!userInfo.spread_uid">
			<text class="tit">推荐人</text>
			<input class="input" type="text" placeholder="请输入推荐人UID" v-model="tjr" placeholder-class="placeholder" />
		</view>
		<view class="add-btn tj" @click="confirm">提交</view>
		<view class="add-btn" @click="cancel">退出</view>
		<!-- #ifdef APP -->
		<yk-authpup ref="WRITE_EXTERNAL_STORAGE" type="top" @changeAuth="changeAuth"
			permissionID="WRITE_EXTERNAL_STORAGE"></yk-authpup>
		<!-- #endif -->
	</view>
</template>

<script>
	import ykAuthpup from "@/components/yk-authpup/yk-authpup";
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import {
		upload
	} from '@/api/order.js';
	import {
		userEdit,
		logout,
		setSpread
	} from '@/api/set.js';
	export default {
		components: {
			ykAuthpup
		},
		data() {
			return {
				show: false,
				password: '',
				tjr: ''
			}
		},
		onLoad() {
			console.log(this.userInfo)
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		methods: {
			...mapMutations('user', ['logout']),
			//获取头像变化的值
			openAuth() {
				this.$refs['WRITE_EXTERNAL_STORAGE'].open()
			},
			changeAuth() {
				this.imgsub()
			},
			imgsub() {
				console.log('上传头像')
				upload({
					filename: ''
				}).then(data => {
					console.log("data", data);
					this.userInfo.avatar = data[0].url;
				})
			},
			confirm() {
				let that = this;
				userEdit({
						avatar: that.userInfo.avatar,
						nickname: that.userInfo.nickname
					})
					.then(e => {
						that.$api.msg('修改成功');
						if(!that.userInfo.spread_uid && that.tjr) {
							setSpread({
								spread_uid: that.tjr
							}).then(res => {
								setTimeout(() => {
									uni.switchTab({
										url: '/pages/user/user'
									});
								}, 1000);
							})
						}else {
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/user/user'
								});
							}, 1000);
						}
						
					})
					.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();
						}
					}
				});
			},
			cancel() {
				// this.$refs.popup.open();
				let obj = this;
				uni.showModal({
					content: '确定要退出登录么',
					success: e => {
						if (e.confirm) {
							logout({}).then(e => {
									obj.logout();
									uni.switchTab({
										url: '/pages/index/index'
									})
								})
								.catch(e => {
									console.log(e);
								});
						}
					}
				});
			},
			qx() {
				this.password = '';
				this.$refs.popup.close();
			},
			pswQd() {
				if (this.password != this.userInfo.phone) {
					this.$refs.popup.close();
					this.password = '';
					this.$api.msg("请输入自己的账户")
					return
				}
				this.$refs.popup.close();
				this.password = '';
				this.$api.msg("申请注销成功，请耐心等待审核")
			}
		}
	}
</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: 40rpx auto 30rpx;
		width: 560rpx;
		height: 80rpx;
		background: #fff;
		border-radius: 40px;
		color: $base-color;
		border: 1px solid $base-color;
	}

	.tj {
		margin-top: 160rpx;
		background: $base-color;
		color: #fff;
	}

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

	.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;
		}
	}

	.auth_wrap {
		position: fixed;
		width: 100%;
		bottom: -120%;
		transition: all 0.35s linear;

		&.show {
			bottom: 0;
			transition: all 0.35s linear;

			.mask {
				display: block;
			}
		}

		.mask {
			width: 100%;
			height: 100vh;
			position: fixed;
			background: rgba(0, 0, 0, 0.5);
			z-index: 98;
			top: 0;
			display: none;
		}

		.auth_content {
			padding: 32rpx 32rpx 72rpx 32rpx;
			position: relative;
			z-index: 99;
			background: #fff;
			border-radius: 16rpx 16rpx 0 0;

			.auth_top {
				position: relative;

				.ptitle {
					font-size: 30rpx;
					font-weight: bold;
					margin-bottom: 24rpx;
				}

				.txt {
					font-size: 26rpx;
					color: #999;
				}

				.close {
					width: 26rpx;
					height: 26rpx;
					position: absolute;
					right: 0;
					top: 0;

					image {
						width: 100%;
						height: 100%;
					}
				}
			}

			.auth_ul {
				margin-top: 16rpx;

				.auth_li {
					display: flex;
					align-items: center;
					border-top: solid 1px #f5f5f5;
					padding: 24rpx 0;

					&:last-child {
						border-bottom: solid 1px #f5f5f5;
					}

					.tit {
						width: 140rpx;
						font-size: 30rpx;
					}

					.rit {
						width: calc(100% - 140rpx);

						input {
							font-size: 28rpx;
							height: 72rpx;
							width: 100%;
						}

						image {
							width: 54rpx;
							height: 54rpx;
							border-radius: 50%;
						}

						button {
							width: 100%;
							height: 72rpx;
							background: #fff;
							text-align: left;
							padding: 0;

							&:after {
								border: solid 1px #fff;
							}

							// opacity: 0;
						}
					}
				}
			}

			.confirm_btn {
				width: 420rpx;
				margin: 46rpx auto 0 auto;
				background: #39b54a;
				color: #fff;
				border-radius: 8rpx;
				height: 94rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 30rpx;
			}
		}
	}
</style>