<template>
	<view>
		<view class="navBar fixed" style="background-image: none;">
			<!-- <uni-nav-bar class="flex_start" :fixed="true" :status-bar="true" color="#000"
				background-color="rgba(0,0,0,0)">
				<view style="width:95%;text-align: center;" class="map-top-text flex_center">个人中心</view>
			</uni-nav-bar> -->
		</view>
		<view class="mine-top">
			<image :src="topimg" class="mine-top-img"></image>
		</view>
		<view class="mine-box">
			<view class="mine-box-user">
				<view class="user_avator" @click="showEditForm">
					<u-avatar :src="info.headimgurl" size="61"></u-avatar>
				</view>
				<view class="user_info">
					<view class="user_name">
						{{info.nickname||"暂未授权"}}
					</view>
					<view class="user_phone">
						{{info.mobile||"暂未设置"}}
					</view>
				</view>
			</view>
			<view class="mine-box-nav">
				<view class="mine-box-navtitle">
					预约记录
				</view>
				<view class="mine-box-navlist">
					<view class="mine-box-navitem" @click="toPage(1)">
						<image src="../../static/img/icon_visit.png" class="mine-box-navitemimg"
							style="width: 60rpx;height: 54rpx;"></image>
						<text class="mine-box-navitemtxt">参观预约</text>
					</view>
					<view class="mine-box-navitem" @click="toPage(2)">
						<image src="../../static/img/icon_activity.png" class="mine-box-navitemimg"
							style="width: 51rpx;height: 56rpx;"></image>
						<text class="mine-box-navitemtxt">活动预约</text>
					</view>
					<view class="mine-box-navitem" @click="toPage(3)">
						<image src="../../static/img/icon_park.png" style="width: 51rpx;height: 52rpx;"
							class="mine-box-navitemimg"></image>
						<text class="mine-box-navitemtxt">停车场预约</text>
					</view>
				</view>
			</view>
			<view class="mine-box-list">
				<view class="list-item" style="position: relative;">
					<button open-type="feedback" class="hideBtn"
						style="width: 100%;height: 100%;top:0rpx;left: 0rpx;"></button>
					<view class="list-item-left">
						<text class="iconfont icon-yijianfankui"></text>
						<text class="list-item-lefttxt">意见反馈</text>
					</view>
					<view class="list-item-right">
						<u-icon name="arrow-right" color="#ccc"></u-icon>
					</view>
				</view>
				<view class="list-item" @click="toPage(4)">
					<view class="list-item-left">
						<text class="iconfont icon-fenxiang"></text>
						<text class="list-item-lefttxt">推荐预约管理系统</text>
					</view>
					<view class="list-item-right">
						<u-icon name="arrow-right" color="#ccc"></u-icon>
					</view>
				</view>
			</view>
		</view>

		<!-- H5和小程序环境判断的授权弹窗 -->
		<u-popup :show="showAuthorization" mode="bottom" @close="closeInfo" :round="10">
			<view class="box">
				<view class="box-title" style="text-align: center;padding: 20rpx 0;font-size: 32rpx;">
					{{isH5 ? '请完善您的个人信息' : '请授权您的微信头像和昵称'}}
				</view>
				<view class="box-content" style="padding: 20px;">
					<!-- H5环境：直接显示编辑表单 -->
					<view v-if="isH5">
						<u-form labelPosition="top" :model="form">
							<u-form-item label="昵称" prop="name" labelWidth="60px" required>
								<u-input v-model="form.name" placeholder="请输入昵称"></u-input>
							</u-form-item>
							<u-form-item label="头像" prop="avatar" labelWidth="60px">
								<view class="avatar-upload">
									<u-avatar :src="form.avatar" size="80" @click="chooseAvatar"></u-avatar>
									<text class="avatar-tip">点击上传头像</text>
								</view>
							</u-form-item>
						</u-form>
						<u-button type="primary" @click="tosubmit">确认提交</u-button>
					</view>
					<!-- 小程序环境：微信一键授权 -->
					<u-button v-else type="primary" @click="getUserProfile">微信用户一键登录</u-button>
				</view>
			</view>
		</u-popup>

		<!-- 小程序环境：确认编辑弹窗 -->
		<u-popup :show="showform" mode="bottom" @close="closeForm" :round="10" v-if="!isH5">
			<view class="box">
				<view class="box-title" style="text-align: center;padding: 20rpx 0;font-size: 32rpx;">
					请确认编辑您的微信头像和昵称
				</view>
				<view class="box-content" style="padding: 20px;">
					<u-form labelPosition="top" :model="form">
						<u-form-item label="昵称" prop="name" labelWidth="60px" required>
							<u-input v-model="form.name" placeholder="请输入昵称"></u-input>
						</u-form-item>
						<u-form-item label="头像" prop="avatar" labelWidth="60px">
							<view class="avatar-upload">
								<u-avatar :src="form.avatar" size="80" @click="chooseAvatar"></u-avatar>
								<text class="avatar-tip">点击上传头像</text>
							</view>
						</u-form-item>
					</u-form>
					<u-button type="primary" @click="tosubmit">确认提交</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				topimg: "",
				info: {},
				showAuthorization: false,
				showform: false,
				action: '',
				otherData: {
					token: ''
				},
				avatarList: [],
				form: {
					name: '',
					avatar: ''
				},
				isH5: false
			}
		},
		onReady() {
			this.topimg = this.util.HOST + "/top_bg.png";
		},
		onShow() {
			var that = this;
			// 检测运行环境
			this.isH5 = typeof window !== 'undefined' && window.location
			
			this.action = this.util.HOST + "/api/mobile/upload-file"
			this.otherData.token = uni.getStorageSync("userInfo_token").token;
			
			this.loadInfo(function(res) {
				if (res.headimgurl || res.nickname) {
					that.showAuthorization = false;
				} else {
					that.showAuthorization = true;
				}
			});
		},
		methods: {
			// 显示编辑表单
			showEditForm() {
				// 检测运行环境
				const isH5 = typeof window !== 'undefined' && window.location
				
				if (isH5) {
					// H5环境：直接显示编辑弹窗
					this.showAuthorization = true;
					// 预填充当前信息
					this.form.name = this.info.nickname || '';
					this.form.avatar = this.info.headimgurl || '';
				} else {
					// 小程序环境：显示确认编辑弹窗
					this.showform = true;
					// 预填充当前信息
					this.form.name = this.info.nickname || '';
					this.form.avatar = this.info.headimgurl || '';
				}
			},
			closeInfo() {
				this.showAuthorization = false;
			},
			closeForm() {
				this.showform = false
			},
			loadInfo(cb) {
				var that = this;
				this.util.getOpenidInfo(function(res) {
					that.util.getUserInfo(function(r) {
						that.info = r;
						cb(r);
					}, true)
				}, true);

			},
			getUserProfile() {
				var that = this;
				this.util.getUserProfile((res) => {
					console.log("res", res)
					that.form.name = res.nickName
					that.form.avatar = res.avatarUrl
					that.avatarList.push({
						type: 0,
						url: res.avatarUrl
					})
					that.showAuthorization = false
					// 小程序环境：显示确认编辑弹窗
					// H5环境：直接提交（因为已经在第一个弹窗中编辑了）
					if (that.isH5) {
						that.tosubmit()
					} else {
						that.showform = true
					}
				})
			},
			chooseAvatar() {
				var that = this;
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: function(res) {
						const tempFilePath = res.tempFilePaths[0];
						// 上传图片到服务器
						uni.uploadFile({
							url: that.action,
							filePath: tempFilePath,
							name: 'file',
							formData: that.otherData,
							success: function(uploadRes) {
								console.log("upload success", uploadRes)
								let data = JSON.parse(uploadRes.data)
								that.form.avatar = data.url
								uni.showToast({
									title: "上传成功",
									icon: "success"
								})
							},
							fail: function(err) {
								console.log("upload fail", err)
								uni.showToast({
									title: "上传失败",
									icon: "none"
								})
							}
						})
					}
				})
			},
			tosubmit() {
				if(this.util.isNull(this.form.name)){
					uni.showToast({
						title: "请填写昵称",
						icon: "none"
					})
					return
				}
				var that = this
				that.util.request({
					api: '/api/mobile/user/save',
					method: 'POST',
					data: {
						headimgurl: that.form.avatar,
						nickname: that.form.name
					},
					utilSuccess: function(r) {
						that.showform = false;
						that.showAuthorization = false;
						that.loadInfo(function() {
							uni.showToast({
								title: "保存成功",
								icon: "success"
							})
						})
					}
				})
			},
			toPage(type) {
				var url = "";
				switch (type) {
					case 1:
						url = "../order/visitorder"
						break;
					case 2:
						url = "../order/activityorder"
						break;
					case 3:
						url = "../order/parkorder"
						break;
					case 4:
						url = "recommend"
						break;
				}
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style>
	.uni-navbar--border {
		border-bottom-width: 0 !important;
	}

	page {
		background: #d8ac75;
	}
	.navBar{
		height:80rpx;
	}
	.mine-box-user {
		margin-bottom: 65rpx;
	}

	.mine-top-img {
		width: 100%;
	}

	.map-top-text {
		width: 124px;
		font-family: PingFangSC-Medium;
		font-size: 32rpx;
		color: #FFFFFF;
		letter-spacing: 0;
		text-align: center;

	}

	.mine-box {
		width: 100%;
		border-top-left-radius: 30rpx;
		border-top-right-radius: 30rpx;
		background-color: #fff;
		position: relative;
		z-index: 1024;
		padding: 55rpx 25rpx;
		box-sizing: border-box;
		margin-top: -288rpx;
		height: calc(100vh);
	}

	.mine-box-nav {
		background: url("../../static/img/nav_bg.png") no-repeat;
		background-size: cover;
		width: 100%;
		height: 273rpx;
	}

	.mine-box-user {
		display: flex;
		align-items: center;

	}

	.user_avator {
		cursor: pointer;
		position: relative;
		transition: transform 0.2s;
	}
	
	.user_avator:active {
		transform: scale(0.95);
	}
	
	.user_info {
		margin-left: 33rpx;
	}

	.user_name {
		font-size: 32rpx;
		font-family: PingFang SC;
		color: #4E4E4E;
	}

	.user_phone {
		font-size: 24rpx;
		color: #828282;
		line-height: 40rpx;
	}

	.mine-box-nav {
		padding: 39rpx;
		box-sizing: border-box;
	}

	.mine-box-navtitle {
		font-size: 32rpx;
		font-family: PingFang SC;
		color: #4E4E4E;
	}

	.hideBtn {
		width: 170rpx;
		height: 170rpx;
		opacity: 0;
		border-radius: 50%;
		position: absolute;
		top: 30rpx;
		left: 30rpx;
		z-index: 9999;
	}

	.mine-box-navlist {
		display: flex;
		justify-content: space-around;
		margin-top: 40rpx;
	}

	.mine-box-navitem {
		display: flex;
		flex-direction: column;
		justify-content: center;
		text-align: center;
		align-items: center;
	}

	.mine-box-navitemtxt {
		font-size: 24rpx;
		font-family: PingFang SC;
		color: #4E4E4E;
		margin-top: 30rpx;
	}

	.mine-box-list {
		display: flex;
		margin-top: 27rpx;
		font-size: 32rpx;
		color: #4E4E4E;
		flex-direction: column;
	}

	.list-item {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		padding-top: 32rpx;
		padding-bottom: 32rpx;
		border-bottom: 2rpx solid #F7F6F4;
	}

	.list-item-lefttxt {
		margin-left: 41rpx;
	}
	
	.avatar-upload {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 10rpx;
	}
	
	.avatar-tip {
		font-size: 24rpx;
		color: #999;
	}
</style>
