<template>
	<view class="container">
		<!-- 顶部标题 -->
		<!-- <view class="header">
      <text class="title">我的</text>
    </view> -->

		<!-- 用户信息区域 -->
		<view class="user-info">
			<view class="avatar-wrapper">
				<image class="avatar" @click="avatarClick()" :src="headPortraitUrl" mode="aspectFill" @error="avatarError"></image>
			</view>
			<view class="user-detail">
				<text class="username">{{custName}}</text>
			</view>
			<view class="star-icon" @click="goCollectPage">
				<uni-icons type="star" size="24" color="#ff9900"></uni-icons>
			</view>
		</view>

		<!-- 我的服务区域 -->
		<view class="services">
			<text class="services-title">我的服务</text>
			<view class="grid-container">
				<!-- 个人信息 -->
				<view class="grid-item" @click="navigateTo('/pages/mine/me/index')">
					<view class="icon-wrapper">
						<image style="width: 28px;height: 28px;" src="/static/images/my/personalInfo.png"></image>
					</view>
					<text class="icon-text">个人信息</text>
				</view>

				<!-- 通知公告 -->
				<view class="grid-item" @click="navigateTo('/pages/mine/announcement/index')">
					<view class="icon-wrapper">
						<image style="width: 28px;height: 28px;" src="/static/images/my/notice.png"></image>
					</view>
					<text class="icon-text">通知公告</text>
				</view>

				<!-- 投诉建议 -->
				<view class="grid-item" @click="navigateTo('/pages/feedback/index')">
					<view class="icon-wrapper">
						<image style="width: 28px;height: 28px;" src="/static/images/my/complaints.png"></image>
					</view>
					<text class="icon-text">投诉建议</text>
				</view>

				<!-- 食堂评价 -->
				<view class="grid-item" @click="navigateTo('/pages/review/index')">
					<view class="icon-wrapper"> 
						<image style="width: 28px;height: 28px;" src="/static/images/my/evaluation.png"></image>
					</view>
					<text class="icon-text">食堂评价</text>
				</view>

				<!-- 体检报告 -->
				<!-- <view class="grid-item" @click="navigateTo('/pages/survey/index')">
					<view class="icon-wrapper"> 
						<image style="width: 28px;height: 28px;" src="/static/images/my/report.png"></image>
					</view>
					<text class="icon-text">体检报告</text>
				</view> -->
			</view>
		</view>

		<u-action-sheet :actions="list" :show="avatarShow" safeAreaInsetBottom closeOnClickOverlay cancelText="取消"
			round="10" @close="avatarShow = false" @select="avatarSelect"></u-action-sheet>
	</view>
</template>

<script>
	import {
		getUserHeaderPhoto,saveUserHeaderPhoto,
		uploadAvatar,
	} from "@/api/system/user"
	import config from '@/config'
	import { getToken } from '@/utils/auth'
	import { showConfirm } from '@/utils/common'
	import { uploadBase64 } from "@/api/upload"
	import { pathToBase64, base64ToPath } from 'image-tools';
	export default {
		data() {
			return {
				custName: "",
				headPortraitUrl:"",
				defaultImg: '/static/images/my/face.png',
				avatarShow: false,
				list: [{
					name: '上传头像',
					val: 1
				}, {
					name: '查看头像',
					val: 2
				}]
			}
		},
		onLoad() {
			this.custName = uni.getStorageSync('custName') ? uni.getStorageSync('custName') : "",
			this.getUserHeaderImg()
		},
		methods: {
			avatarClick() {
				this.avatarShow = true
			},
			
			avatarSelect(e) {
				if (e.val == 1) {
					//上传头像 
					uni.chooseImage({//选择图片
						count: 1,
						success: resImage => {
							console.log(resImage)
							// this.headPortraitUrl = resImage.tempFilePaths[0]
							//转base64
							this.imgToBase64(resImage.tempFilePaths[0]).then(base64 => {
								console.log(base64)
								//base64上传
								this.uploadHeadImg(base64)
							}) 
						}
					});
				}else if(e.val == 2) {
					//查看头像
					uni.previewImage({
						urls: [this.headPortraitUrl]
					});
				}
			},
			//base64上传
			uploadHeadImg(base64){
				let param = {
					"MERCHANT-ID":"378915229716713472",
					"uploadKey":'cust',
					"base64File":base64
				}
				uploadBase64(param).then(res => {
					console.log(res)
					if(res.code==200){
						this.saveHeadImg(res.data) 
					}else{
						uni.showToast({
						  title: res.msg,
						  icon: 'none'
						}) 
					}
				}) 
			},
			//头像保存接口
			async saveHeadImg(data){
				console.log(data)
				let param = {
					"headPortraitUrl": data.fileNameUrl,
					"custId": uni.getStorageSync('custId')
				}
				console.log(param) 
				const res = await saveUserHeaderPhoto(param)
				console.log(res)
				if(res.code==200){
					uni.showToast({
					  title: "上传成功",
					  icon: 'none'
					})
					this.getUserHeaderImg()
				}else{
					uni.showToast({
					  title: res.msg,
					  icon: 'none'
					}) 
				}
				
			},
			imgToBase64(data) {
			  return new Promise((resolve, reject) => {
						pathToBase64(data).then(base64 => {
							resolve(base64)
						}).catch(error => {
							console.error(error)
							reject(error)
						})
			  })
			},
			getUserHeaderImg() {
				getUserHeaderPhoto({"custId":uni.getStorageSync('custId'),"sourceType": "7"}).then(res => {
					console.log(res)
					this.headPortraitUrl=res.headPortraitUrl;
					if(!this.headPortraitUrl) {
						this.headPortraitUrl = this.defaultImg
					}  
				})
			},
			
			avatarError(e) {
				console.log(e)
				if (e.type == 'error') {
					this.headPortraitUrl = this.defaultImg
				}
			},
			
			goCollectPage(){ 
				uni.navigateTo({url: "/pages/mine/collect/index"})
			},
			navigateTo(url) {
				uni.navigateTo({
					url
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		min-height: 10vh;
		background-color: #F9FBFF;
	}

	.header {
		padding: 14px 16px 16px;
		background: #F9FBFF;

		.title {
			font-size: 18px;
			font-weight: 500;
			color: #333;
		}
	}

	.user-info {
		display: flex;
		align-items: center;
		padding: 16px;
		background: #fff;
		margin-bottom: 10px;

		.avatar-wrapper {
			margin-right: 12px;

			.avatar {
				width: 60px;
				height: 60px;
				border-radius: 30px;
			}
		}

		.user-detail {
			flex: 1;

			.username {
				margin-left: 12px;
				font-size: 18px;
				font-weight: 500;
				color: #333;
			}
		}

		.star-icon {
			padding: 8px;
		}
	}

	.services {
		background: #fff;
		padding: 16px;

		.services-title {
			font-size: 16px;
			color: #333;
			margin-bottom: 26px;

			font-weight: 400;
		}

		.grid-container {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 20px;

			.grid-item {
				display: flex;
				flex-direction: column;
				align-items: center;

				.icon-wrapper {
					width: 48px;
					height: 48px;
					//border-radius: 24px;
					//background: #f5f5f5;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 3px;
				}

				.icon-text {
					font-size: 14px;
					color: #666;
				}
			}
		}
	}
</style>