<template>
	<view class="container">
		<image class="bkg" mode="aspectFill" src="@/static/order-bg.png"></image>

		<view @click="showMyInfo = true" class="top" :style="{'padding-top':statusBarHeight+'px'}">
			<image class="avatar" mode="aspectFit" :src="avatar_img || vuex_default_icon"></image>

			<view class="info">
				<view class="info-name">{{ vuex_user.name || '微信用户' }}</view>
				<view class="info-mobile">手机号：{{ vuex_user.mobile || '' }}</view>
			</view>
		</view>
		<view class="panel panel2">
			<view class="row" @click="toUrl">
				<u-icon class="row__icon" name="heart" color="#d61b24"></u-icon>
				<view>我的订单</view>
			</view>
			<view class="row" @click="loginOut">
				<u-icon class="row__icon" name="account" color="#d61b24"></u-icon>
				<view>退出</view>
			</view>
		</view>

		<!-- 设置头像 -->
		<view class="myinfo">
			<u-popup v-model="showMyInfo" mode="bottom" :border-radius="30">
				<view class="form-wrapper">
					<u-form :model="form" ref="uForm" label-width="140rpx">
						<u-form-item label="头像">
							<button class="btn-normal" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
								<image :src="avatar_img || vuex_default_icon"></image>
							</button>
						</u-form-item>
					</u-form>
				</view>
				<!-- 操作按钮 -->
				<view class="footer">
					<view class="btn-wrapper">
						<view class="btn-item btn-item-main" @click="saveInfo">保存</view>
					</view>
				</view>
			</u-popup>
		</view>
	</view>
</template>

<script>
	import {
		ROOTPATH as baseUrl
	} from "@/common/config.js"
	export default {
		components: {},
		data() {
			return {
				statusBarHeight: 40,
				showMyInfo: false,
				form: {
					avatar: ''
				},
				avatar_img: ''
			};
		},
		onLoad() {

			this.statusBarHeight = uni.getMenuButtonBoundingClientRect().top;
			this.getUserInfo()
		},
		methods: {
			toUrl() {
				uni.navigateTo({
					url: '/package_sub/order/order'
				})
			},
			loginOut() {
				let that = this
				uni.showModal({
					content: "是否确认退出？",
					success(res) {
						if (res.confirm) {
							that.$u.api.loginOut().then(res => {
								that.base.toast("退出成功", 1500, function() {
									setTimeout(() => {
										uni.removeStorageSync('lifeData')
										uni.redirectTo({
											url: '/package_sub/login/login'
										})
									}, 1500)
								})
							})
						}
					}
				})

			},
			onChooseAvatar(e) {
				console.log(e)
				console.log(e);
				let a = uni.uploadFile({
					url: baseUrl + "/api/nurse/upload-file",
					filePath: e.detail.avatarUrl,
					name: 'file',
					header: {
						['Authorization']: `Bearer ${this.vuex_token}`
					},
					success: (res) => {
						uni.showToast({
							title: '上传成功',
							duration: 1000,
							icon: 'none'
						})
						console.log("res", res)
						let data = JSON.parse(res.data)
						this.form.avatar = data.id
					}
				})
			},
			async saveInfo() {
				let that = this
				if (this.base.isNull(this.form.avatar)) {
					this.base.toast("请上传头像")
					return
				}
				const res = await this.$u.api.saveUser({
					avatar: that.form.avatar
				})
				that.base.toast('更新成功', 1000, function() {
					setTimeout(function() {
						that.showMyInfo = false
						that.getUserInfo()
					}, 1000)
				})
			},
			async getUserInfo() {
				const user = await this.$u.api.getUser()
				this.form.avatar = user.avatar ? user.avatar : ''
				this.avatar_img = user.avatar_detail ? user.avatar_detail.url : ''
				this.$u.vuex('vuex_user', user)
			}
		},

	}
</script>

<style lang="scss">
	.container {
		min-height: 100vh;
		position: relative;
		background: #f4efef;
		font-size: 32rpx;

		.bkg {
			width: 100vw;
			z-index: 0;
			height: 686rpx;
			position: absolute;
			top: 0;
			left: 0;
		}

		.top {
			display: flex;
			align-items: center;
			position: relative;
			padding: 4vh 25rpx 0;

			.avatar {
				width: 100rpx;
				height: 100rpx;
				border-radius: 80rpx;
			}

			.info {
				padding-left: 40rpx;

				&-name {
					line-height: 2;
					font-weight: 600;
				}
			}
		}

		.panel {
			position: relative;
			background: #fff;
			border-radius: 10rpx;
			margin: 40rpx 25rpx 0;
			padding: 28rpx 25rpx;
		}

		.panel2 {
			.row {
				display: flex;
				padding: 20rpx 10rpx;
				border-bottom: 2rpx #dee2e6 solid;

				&__icon {
					margin-right: 20rpx;
					color: $uni-color-primary;
					font-size: 36rpx;
				}
			}
		}

		.form-wrapper {
			margin: 20rpx auto 20rpx auto;
			padding: 0 40rpx;
			width: 94%;
			box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
			border-radius: 16rpx;
			background: #fff;

			.btn-normal {
				display: block;
				margin: 0;
				padding: 0;
				line-height: normal;
				background: none;
				border-radius: 0;
				box-shadow: none;
				border: none;
				font-size: unset;
				text-align: unset;
				overflow: visible;
				color: inherit;

				image {
					width: 120rpx;
					height: 120rpx;
					border-radius: 100%;
					border: 2px solid #fff;
					margin-right: 30rpx;
				}
			}

			.btn-normal::after {
				border: none
			}
		}

		.footer {
			margin-top: 80rpx;
			margin-bottom: 40rpx;

			.btn-wrapper {
				height: 100%;
				// display: flex;
				// align-items: center;
				padding: 0 20rpx;
			}

			.btn-item {
				flex: 1;
				font-size: 28rpx;
				height: 86rpx;
				color: #fff;
				border-radius: 50rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}

			.btn-item-main {
				background: #d61b24;
				color: #fff;
			}
		}
	}
</style>