<template>
	<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">
					<u-avatar :src="info.headimgurl?info.headimgurl:headReplace" size="61"></u-avatar>
				</view>
				<view class="user_info">
					<view class="user_name">
						{{info.nickname||"暂未授权"}}
					</view>
					<view class="user_phone">
						{{info.mobile||"暂未设置"}}
					</view>
					<view class="user_phone">
						欢迎来到苏州革命博物馆~
					</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/index_icon_1.png" class="mine-box-navitemimg"></image>
						<text class="mine-box-navitemtxt">参观预约</text>
					</view>
					<view class="mine-box-navitem" @click="toPage(2)">
						<image src="../../static/img/index_icon_2.png" class="mine-box-navitemimg"></image>
						<text class="mine-box-navitemtxt">活动预约</text>
					</view>
					<view class="mine-box-navitem" @click="toPage(3)">
						<image src="../../static/img/index_icon_3.png" 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>
		<!--  -->
		<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;">
					请授权您的微信头像和昵称
				</view>
				<view class="box-content" style="padding: 20px;">
					<u-button type="primary" @click="getUserProfile">微信用户一键登录</u-button>
				</view>
			</view>
		</u-popup>

		<u-popup :show="showform" mode="bottom" @close="closeInfo" :round="10">
			<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">
							<htz-image-upload :max="1" v-model="avatarList" mediaType="image" :chooseNum="1"
								:sourceType="['album','camera']" :compress="false" :quality="80" :formData="otherData"
								:remove="true" :dataType="1" @uploadSuccess="uploadSuccess" @imgDelete="imgDelete"
								@uploadFail="uploadFail" :action="action"></htz-image-upload>
						</u-form-item>
					</u-form>
					<u-button type="primary" @click="tosubmit">确认提交</u-button>

				</view>
			</view>
		</u-popup>
		<tabbar :current-page="4"></tabbar>
	</view>
</template>

<script>
	import tabbar from "../../components/tabbar/tabbar.vue"
	import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue'
	export default {
		components: {
			tabbar,
			htzImageUpload
		},
		data() {
			return {
				topimg: require('@/static/img/top_bg.png'),
				headReplace: require('@/static/img/headreplace.png'),
				info: {},
				showAuthorization: false,
				showform: false,
				action: '',
				otherData: {
					token: ''
				},
				avatarList: [],
				form: {
					name: '',
					avatar: ''
				}
			}
		},
		onReady() {},
		onShow() {

			var that = this;
			this.action = this.util.HOST + "/api/mobile/upload-file"
			this.otherData.token = uni.getStorageSync("userInfo_token").token;
			this.loadInfo(function(res) {
				if (that.util.isNull(res.headimgurl) || that.util.isNull(res.nickname)) {
					that.showAuthorization = true;
				} else {
					that.showAuthorization = false;
				}
			});
		},
		methods: {
			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
					that.showform = true
				})
			},
			uploadSuccess(res) {
				console.log("succ", res)
				uni.showLoading({
					title: "上传成功"
				})
				let data = JSON.parse(res.data)
				this.form.avatar = data.url
				this.avatarList = []
				this.avatarList.push({
					type: 0,
					url: data.url
				})
			},
			uploadFail(res) {
				console.log("fail", res)
			},
			imgDelete(res) {
				this.avatarList = res.tempFilePaths
				this.form.avatar = ''
			},
			tosubmit() {
				if(this.util.isNull(this.form.name)){
					uni.showLoading({
						title: "请填写昵称"
					})
					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.loadInfo(function() {
							
						})
					}

				})
			},
			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: #fbf3ea;
		height: 100vh;
	}

	.mine-box-user {
		margin-bottom: 65rpx;
	}

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

	.mine-box {
		width: 100%;
		position: relative;
		z-index: 1024;
		box-sizing: border-box;
		margin-top: -260rpx;
		/* margin-top: -288rpx; */
		/* height: calc(100vh); */
	}

	.mine-box-user {
		display: flex;
		align-items: center;
		padding-left: 63rpx;
	}

	.user_info {
		margin-left: 33rpx;
	}

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

	.user_phone {
		font-size: 28rpx;
		color: #fff;
		line-height: 40rpx;
	}

	.mine-box-nav {
		margin: 39rpx;
		box-sizing: border-box;
		background: #FBF3EA;
		margin-top: -10rpx;
		border-radius: 20rpx 20rpx 0 0;
	}

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

	.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: 28rpx;
		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;
	}

	.mine-box-navitemimg {
		width: 102rpx;
		height: 102rpx;
	}

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