<template>
	<view class="container">
		<view class="body">
			<!-- 个人头像区域 -->
			<view class="body_box">
				个人头像
				<view class="box_right" @click="handleClickPhoto">
					<img mode="aspectFill" class="img" :src="imgUrl || '../../static/my/portrait.png'" />

					<img class="img2" src="../../static/my/right.png" />
				</view>
			</view>

			<!-- 账号名区域 -->
			<view class="body_box" @click="handleClickName">
				账号名
				<view class="box_right">
					<view class="msg">{{ name }}</view>
					<img class="img2" src="../../static/my/right.png" />
				</view>
			</view>

			<!-- ID区域 -->
			<view class="body_box">
				ID
				<view class="box_right">
					<view class="msg">{{ id }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 头像
			imgUrl: '',
			// 账号名
			name: '',
			//用户id
			id: ''
		}
	},
	onLoad() {
		this.getUserInfo()
	},
	methods: {
		getUserInfo() {
			let userInfo = uni.getStorageSync('userInfo')
			this.name = userInfo.user_name
			this.imgUrl = userInfo.headPhoto
			this.id = userInfo.id
		},
		// 点击头像图片回调
		handleClickPhoto() {
			uni.showActionSheet({
				itemList: ['更换头像'],
				success: (res) => {
					if (res.tapIndex === 0) {
						this.changeImg()
					}
				}
			})
		},
		// 更换头像回调
		changeImg() {
			uni.chooseImage({
				count: 1,
				sizeType: ['compressed'],
				sourceType: ['album'], //从相册选择
				success: (res) => {
					uni.showLoading({
						title: '上传中'
					})
					uni.uploadFile({
						url: `https://chtech.ncjti.edu.cn/homestay/file/cos/upload`,
						filePath: res.tempFilePaths[0],
						name: 'files',
						success: async (uploadFileRes) => {
							const res = JSON.parse(uploadFileRes.data)

							if (res.code === 200 || res.code === 1) {
								uni.hideLoading()
								this.imgUrl = res.data
								const result = await this.$myRequest({
									url: '/mhotel/ampupdateUserInfo.action',
									data: {
										userId: this.id,
										userName: this.name,
										headPhoto: this.imgUrl
									}
								})
								if (result.code === 200) {
									uni.showToast({
										title: '头像修改成功',
										icon: 'success',
										mask: true
									})
									setTimeout(() => {
										this.getUser()
									}, 1500)
								}
							}
						},
						fail: () => {
							uni.showToast({
								title: '上传失败',
								icon: 'error'
							})
						}
					})
				}
			})
		},
		// 点击账号名回调
		handleClickName() {
			uni.showActionSheet({
				itemList: ['修改账号名'],
				success: (res) => {
					if (res.tapIndex === 0) {
						this.changeName()
					}
				}
			})
		},
		// 修改账号名回调
		changeName() {
			uni.showModal({
				title: '请输入账号名',
				editable: true,
				success: async (res) => {
					if (res.confirm) {
						if (!res.content) {
							uni.showToast({
								title: '账号名不能为空',
								icon: 'none'
							})
							setTimeout(() => {
								this.changeName()
							}, 1500)
						} else if (res.content.length > 8) {
							uni.showToast({
								title: '账号名最多8位',
								icon: 'none'
							})
							setTimeout(() => {
								this.changeName()
							}, 1500)
						} else {
							const result = await this.$myRequest({
								url: '/mhotel/ampupdateUserInfo.action',
								data: {
									userId: this.id,
									userName: res.content,
									headPhoto: this.imgUrl
								}
							})
							if (result.code === 200) {
								uni.showToast({
									title: '账号修改成功',
									icon: 'success',
									mask: true
								})
								setTimeout(() => {
									this.getUser()
								}, 1500)
							}
						}
					}
				}
			})
		},
		//获取个人信息
		async getUser() {
			const res = await this.$myRequest({
				url: '/mhotel/ampqueryUsersById.action',
				data: {
					userId: uni.getStorageSync('userInfo').id
				}
			})
			if (res.code === 200) {
				this.name = res.data.user_name
				this.imgUrl = res.data.headPhoto
				uni.setStorageSync('userInfo', res.data)
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	display: flex;
	flex-direction: column;
	min-height: 100vh;
	background-color: #ebeced;

	.body {
		box-sizing: border-box;
		margin-top: 20rpx;
		padding: 0 30rpx;
		height: calc(100vh - 20rpx);
		background-color: #fff;

		.body_box {
			display: flex;
			justify-content: space-between;
			align-items: center;
			height: 100rpx;
			font-size: 28rpx;
			border-bottom: 1rpx solid #e6e6e6;

			.box_right {
				display: flex;
				justify-content: flex-end;
				align-items: center;
				height: 100rpx;

				.img {
					width: 64rpx;
					height: 64rpx;
					border-radius: 50%;
				}

				.img2 {
					margin-left: 7rpx;
					width: 47rpx;
					height: 47rpx;
				}

				.msg {
					color: #808080;
				}
			}
		}
	}
}
</style>
