<template>
	<view class="my-page">
		<!-- 顶部背景墙 -->
		<view class="header-curve">
			<view class="user-block" @click="goToLogin">
				<image class="user-avatar" :src="userInfo?.avatarUrl || '/static/images/default-avatar.png'"
					mode="aspectFill"></image>
				<view class="user-info">
					<text class="user-name">{{ userInfo ? userInfo.nickName : '点击登录' }}</text>
					<text class="user-desc-text">{{ userInfo ? (userInfo.remark || '这位用户很懒,什么都没写 🐾') : '登录后享受更多权益 🐾'
					}}</text>
				</view>
				<uni-icons v-if="!userInfo" type="right" size="16" color="rgba(100, 70, 20, 0.4)"></uni-icons>
			</view>
			<view class="wave-shape"></view>
		</view>

		<!-- 我的服务订单区 -->
		<view class="cute-card order-wrap">
			<view class="card-head">
				<text class="card-title">我的服务订单</text>
				<view class="card-more" @click="goToOrder('all')">
					<text>查看全部</text>
					<uni-icons type="right" size="12" color="#a39686"></uni-icons>
				</view>
			</view>
			<view class="order-nav">
				<view class="nav-item" v-for="item in orderItems" :key="item.key" @click="goToOrder(item.key)">
					<view class="icon-bulb">
						<image class="custom-icon" :src="item.icon" mode="aspectFit"></image>
					</view>
					<text class="nav-label">{{ item.label }}</text>
				</view>
			</view>
		</view>

		<!-- 主功能网格 -->
		<view class="menu-grid">
			<view class="menu-box" v-for="item in menuItems" :key="item.path" @click="goToMenu(item)">
				<view class="menu-icon-wrap">
					<image class="custom-icon" :src="item.icon" mode="aspectFit"></image>
				</view>
				<text class="menu-text">{{ item.title }}</text>
			</view>
		</view>

		<view class="footer-msg">
			<text>~ 感谢您的陪伴 ~</text>
		</view>

		<custom-tabbar></custom-tabbar>
	</view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { getInfo } from '@/api/system/user'
import customTabbar from '@/components/custom-tabbar/index.vue'
import orderStatusData from '@/json/orderStatus.json'

const userInfo = ref(null)

const fetchUserInfo = async () => {
	const token = uni.getStorageSync('token')
	if (token) {
		try {
			const res = await getInfo()
			if (res && res.user) {
				userInfo.value = res.user
			}
		} catch (error) {
			console.error('获取用户信息失败', error)
		}
	} else {
		userInfo.value = null
	}
}

onShow(() => {
	fetchUserInfo()
})

const goToLogin = () => {
	if (!userInfo.value) {
		uni.navigateTo({ url: '/pages/login/index' })
	}
}

const goToOrder = (statusValue) => {
	// statusValue 可以是 'all' 或枚举值
	if (statusValue === 'all') {
		uni.reLaunch({ url: '/pages/order/list/index' })
	} else {
		uni.reLaunch({ url: `/pages/order/list/index?status=${statusValue}` })
	}
}

const goToMenu = (item) => {
	if (item.path) {
		uni.navigateTo({ url: item.path })
	}
}

// 订单图标 - 从 orderStatus.json 生成
const iconMap = {
	0: '/static/icon/order-wait.svg',
	1: '/static/icon/order-accept.svg',
	2: '/static/icon/order-service.svg',
	3: '/static/icon/order-service.svg',
	4: '/static/icon/order-done.svg',
	5: '/static/icon/order-cancel.svg'
}

const orderItems = orderStatusData.map(item => ({
	key: item.value,
	label: item.label,
	icon: iconMap[item.value] || '/static/icon/order-service.svg'
}))

// 功能菜单
const menuItems = [
	{ title: '宠物档案', icon: '/static/icon/pet-archive.svg', path: '/pages/my/pet/list/index' },
	{ title: '用户管理', icon: '/static/icon/user-manage.svg', path: '/pages/my/user/list/index' },
	{ title: '投诉管理', icon: '/static/icon/review.svg', path: '/pages/my/complaint/list/index' },
	{ title: '服务费统计', icon: '/static/icon/service-fee.svg', path: '/pages/my/fee/statistics/index' },
	{ title: '客服中心', icon: '/static/icon/service-center.svg', path: '' },
	{ title: '协议中心', icon: '/static/icon/agreement.svg', path: '/pages/my/agreement/list/index' },
	{ title: '系统设置', icon: '/static/icon/settings.svg', path: '/pages/my/settings/index' }
]
</script>

<style lang="scss" scoped>
.my-page {
	min-height: 100vh;
	background-color: #fcfaf5;
	padding-bottom: 140rpx;
}

.header-curve {
	position: relative;
	background: #f7ca3e;
	padding: 120rpx 40rpx 160rpx;
	overflow: hidden;
}

.user-block {
	display: flex;
	align-items: center;
	position: relative;
	z-index: 2;
}

.user-avatar {
	width: 140rpx;
	height: 140rpx;
	border: 8rpx solid #fff;
	border-radius: 50%;
	background-color: #fff;
}

.user-info {
	flex: 1;
	margin-left: 32rpx;
}

.user-name {
	display: block;
	font-size: 40rpx;
	font-weight: 800;
	color: #5c4314;
	margin-bottom: 12rpx;
	letter-spacing: 2rpx;
}

.user-desc-text {
	display: block;
	font-size: 24rpx;
	color: #5c4314;
	opacity: 0.85;
	font-weight: 600;
}

.wave-shape {
	position: absolute;
	bottom: -80rpx;
	left: 0;
	width: 100%;
	height: 160rpx;
	background: #fcfaf5;
	border-radius: 50%;
	transform: scaleX(1.5);
}

.cute-card {
	background: #fff;
	border-radius: 40rpx;
	padding: 40rpx;
	margin: 0 32rpx;
	box-shadow: 0 12rpx 40rpx rgba(220, 212, 196, 0.4);
	position: relative;
	z-index: 3;
}

.order-wrap {
	margin-top: -60rpx;
	border: 4rpx solid #fffdfa;
}

.card-head {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 32rpx;
}

.card-title {
	font-size: 30rpx;
	font-weight: 800;
	color: #4a3e2e;
}

.card-more {
	display: flex;
	align-items: center;
	gap: 4rpx;
	font-size: 24rpx;
	color: #a39686;
	font-weight: 600;
}

.order-nav {
	display: flex;
	justify-content: space-between;
}

.nav-item {
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 16rpx;
}

.icon-bulb {
	width: 92rpx;
	height: 92rpx;
	background: #fef6df;
	border-radius: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.menu-grid {
	display: grid;
	grid-template-columns: repeat(4, 1fr);
	gap: 24rpx;
	padding: 0 32rpx;
	margin-top: 40rpx;
}

.menu-box {
	background: #fff;
	border-radius: 36rpx;
	padding: 36rpx 12rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	gap: 24rpx;
	box-shadow: 0 8rpx 24rpx rgba(220, 212, 196, 0.3);
}

.menu-icon-wrap {
	width: 80rpx;
	height: 80rpx;
	border-radius: 50%;
	background: #fef6df;
	display: flex;
	align-items: center;
	justify-content: center;
}

.custom-icon {
	width: 44rpx;
	height: 44rpx;
}

.nav-label,
.menu-text {
	font-size: 22rpx;
	font-weight: 700;
	color: #6d5b45;
	text-align: center;
}

.nav-label {
	font-weight: 600;
}

.footer-msg {
	text-align: center;
	margin-top: 60rpx;
	margin-bottom: 40rpx;
	font-size: 24rpx;
	font-weight: 600;
	color: #d1c5b4;
	letter-spacing: 4rpx;
}
</style>
