<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>

		<!-- 客服中心弹窗 @Author: Antigravity -->
		<view class="service-popup-mask" v-if="showServicePopup" @click="closeServicePopup">
			<view class="service-popup" @click.stop>
				<view class="service-header">
					<text class="service-title">联系客服</text>
					<uni-icons type="closeempty" size="20" color="#999" @click="closeServicePopup"></uni-icons>
				</view>

				<view class="qr-section">
					<text class="qr-title">客服二维码</text>
					<image class="qr-img" :src="customerSetting.qrCodeUrl || '/static/images/logo.png'" mode="aspectFit" @click="previewQRCode">
					</image>
					<text class="qr-desc">点击查看大图</text>
				</view>

				<view class="service-list">
					<view class="service-row" @click="openOnlineService">
						<view class="service-row-icon-box green">
							<uni-icons type="chat-filled" size="24" color="#fff"></uni-icons>
						</view>
						<view class="service-info">
							<text class="service-name">在线客服</text>
							<text class="service-desc">{{ customerSetting.wechatAccount || '企业微信专属客服在线解答' }}</text>
						</view>
						<view class="call-btn-mini green-btn">
							<text>去咨询</text>
						</view>
					</view>

					<view class="service-row" @click="callServicePhone">
						<view class="service-row-icon-box orange">
							<uni-icons type="phone-filled" size="24" color="#fff"></uni-icons>
						</view>
						<view class="service-info">
							<text class="service-name">客服电话</text>
							<text class="service-desc">{{ customerSetting.phoneNumber || '暂无电话' }}</text>
						</view>
						<view class="call-btn-mini orange-btn">
							<text>拨打</text>
						</view>
					</view>
				</view>
			</view>
		</view>

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

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

const userInfo = ref(null)
const showServicePopup = ref(false)
const customerSetting = reactive({
	wechatAccount: '',
	phoneNumber: '',
	qrCode: '',
	qrCodeUrl: '',
	enterpriseWechatLink: ''
})

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
	}
}

// 获取客服配置 (ID=2)
const fetchCustomerSetting = async () => {
	try {
		const res = await getCustomerServiceSetting(2)
		if (res) {
			Object.assign(customerSetting, res)
		}
	} catch (error) {
		console.error('获取客服配置失败', error)
	}
}

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

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

const goToOrder = (statusValue) => {
	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.title === '客服中心') {
		showServicePopup.value = true
		return
	}
	if (item.path) {
		uni.navigateTo({ url: item.path })
	}
}

const closeServicePopup = () => {
	showServicePopup.value = false
}

const previewQRCode = () => {
	if (!customerSetting.qrCodeUrl) return
	uni.previewImage({
		urls: [customerSetting.qrCodeUrl]
	})
}

const openOnlineService = () => {
	if (customerSetting.enterpriseWechatLink) {
		// #ifdef H5
		window.location.href = customerSetting.enterpriseWechatLink
		// #endif
		// #ifndef H5
		uni.setClipboardData({
			data: customerSetting.wechatAccount || customerSetting.enterpriseWechatLink,
			success: () => {
				uni.showToast({ title: '客服账号已复制，请在微信中添加', icon: 'none' })
			}
		})
		// #endif
	} else {
		uni.showToast({ title: '在线客服暂未配置', icon: 'none' })
	}
}

const callServicePhone = () => {
	if (!customerSetting.phoneNumber) {
		uni.showToast({ title: '暂无客服电话', icon: 'none' })
		return
	}
	uni.makePhoneCall({
		phoneNumber: customerSetting.phoneNumber
	})
}

// 订单图标 - 从 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>
/* 保持原有样式并添加弹窗样式 @Author: Antigravity */
.my-page {
	min-height: 100vh;
	background-color: #fcfaf5;
	padding-bottom: 140rpx;
}

/* ... 之前的样式保留，下文补充新样式 ... */

.service-popup-mask {
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background-color: rgba(0, 0, 0, 0.6);
	z-index: 999;
	display: flex;
	justify-content: center;
	align-items: center;
	backdrop-filter: blur(4px);
}

.service-popup {
	width: 620rpx;
	background-color: #fff;
	border-radius: 48rpx;
	padding: 0;
	overflow: hidden;
	box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
	animation: slide-in 0.3s cubic-bezier(0.18, 0.89, 0.32, 1.28);
}

@keyframes slide-in {
	from { transform: scale(0.85); opacity: 0; }
	to { transform: scale(1); opacity: 1; }
}

.service-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 40rpx;
	border-bottom: 2rpx solid #faf8f5;
}

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

.qr-section {
	padding: 48rpx 0;
	display: flex;
	flex-direction: column;
	align-items: center;
	background: linear-gradient(180deg, #fff 0%, #fffbf2 100%);
}

.qr-title {
	font-size: 28rpx;
	font-weight: 700;
	color: #6d5b45;
	margin-bottom: 30rpx;
}

.qr-img {
	width: 320rpx;
	height: 320rpx;
	border-radius: 32rpx;
	padding: 16rpx;
	background: #fff;
	box-shadow: 0 8rpx 32rpx rgba(247, 202, 62, 0.15);
	margin-bottom: 20rpx;
}

.qr-desc {
	font-size: 24rpx;
	color: #a39686;
	font-weight: 600;
}

.service-list {
	padding: 20rpx 40rpx 40rpx;
}

.service-row {
	display: flex;
	align-items: center;
	padding: 32rpx 0;
	border-bottom: 2rpx solid #fcfaf5;
	&:last-child { border-bottom: none; }
}

.service-row-icon-box {
	width: 88rpx;
	height: 88rpx;
	border-radius: 28rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 24rpx;
	&.green { background: #5ec686; box-shadow: 0 8rpx 16rpx rgba(94, 198, 134, 0.2); }
	&.orange { background: #f7ca3e; box-shadow: 0 8rpx 16rpx rgba(247, 202, 62, 0.2); }
}

.service-info {
	flex: 1;
}

.service-name {
	display: block;
	font-size: 28rpx;
	font-weight: 800;
	color: #4a3e2e;
	margin-bottom: 4rpx;
}

.service-desc {
	display: block;
	font-size: 22rpx;
	color: #a39686;
	font-weight: 600;
}

.call-btn-mini {
	padding: 12rpx 28rpx;
	border-radius: 20rpx;
	font-size: 24rpx;
	font-weight: 700;
	transition: transform 0.2s;
	&:active { transform: scale(0.95); }
	&.green-btn { background: #eef9f2; color: #5ec686; }
	&.orange-btn { background: #fef6df; color: #f7ca3e; }
}


.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>
