<template>
	<view class="container">
		<!-- 用户信息头部 -->
		<view class="user-header">
			<view class="user-info">
				<view class="avatar-container">
					<view class="avatar" v-if="userInfo.avatar">
						<image class="avatar-image" :src="getFullImageUrl(userInfo.avatar)" mode="aspectFill"></image>
					</view>
					<view class="avatar default-avatar" v-else>
						<uni-icons type="person-filled" size="60" color="#fff"></uni-icons>
					</view>
				</view>
				<view class="user-details" @click="goToEditProfile">
					<text class="user-name">{{ userInfo.customer_name || '未设置姓名' }}</text>
					<text class="user-phone">{{ userInfo.phone }}</text>
					<view class="user-points" @click.stop="goToScores">
						<text class="points-text">积分：{{ userInfo.current_points || 0 }}</text>
						<uni-icons type="right" size="16" color="#fff"></uni-icons>
					</view>
				</view>
				<view class="edit-btn" @click="goToEditProfile">
					<uni-icons type="compose" size="24" color="#fff"></uni-icons>
				</view>
			</view>
		</view>

		<!-- 我的买单 -->
		<view class="order-section">
			<view class="section-header">
				<text class="section-title">我的买单</text>
				<text class="view-all" @click="viewAllOrders('purchase')">查看全部></text>
			</view>
			<view class="order-status-bar">
				<view class="status-item" @click="goToOrders('purchase', 'pending')">
					<uni-icons type="wallet" size="32" color="#FF8800"></uni-icons>
					<text class="status-text">待付款</text>
					<view class="status-badge" v-if="pendingCount > 0">{{ pendingCount }}</view>
				</view>
				<view class="status-item" @click="goToOrders('purchase', 'confirming')">
					<uni-icons type="checkmarkempty" size="32" color="#1890FF"></uni-icons>
					<text class="status-text">待确认</text>
					<view class="status-badge" v-if="confirmingCount > 0">{{ confirmingCount }}</view>
				</view>
				<view class="status-item" @click="goToOrders('purchase', 'completed')">
					<uni-icons type="checkbox" size="32" color="#52C41A"></uni-icons>
					<text class="status-text">已完成</text>
				</view>
			</view>
		</view>

		<!-- 我的卖单 -->
		<view class="order-section">
			<view class="section-header">
				<text class="section-title">我的卖单</text>
				<text class="view-all" @click="viewAllOrders('sales')">查看全部></text>
			</view>
			<view class="order-status-bar">
				<view class="status-item" @click="goToOrders('sales', 'pending')">
					<uni-icons type="shop" size="32" color="#FF8800"></uni-icons>
					<text class="status-text">待付款</text>
					<view class="status-badge" v-if="salesPendingCount > 0">{{ salesPendingCount }}</view>
				</view>
				<view class="status-item" @click="goToOrders('sales', 'confirming')">
					<uni-icons type="eye" size="32" color="#1890FF"></uni-icons>
					<text class="status-text">待确认</text>
					<view class="status-badge" v-if="salesConfirmingCount > 0">{{ salesConfirmingCount }}</view>
				</view>
				<view class="status-item" @click="goToOrders('sales', 'completed')">
					<uni-icons type="checkmarkempty" size="32" color="#52C41A"></uni-icons>
					<text class="status-text">已完成</text>
				</view>
			</view>
		</view>

		<!-- 其他服务 -->
		<view class="service-section">
			<view class="section-title">其他服务</view>
			<view class="service-grid">
				<view class="service-item" @click="goToWarehouse">
					<uni-icons type="home" size="32" color="#666"></uni-icons>
					<text class="service-text">我的仓库</text>
				</view>
				<view class="service-item" @click="goToScores">
					<uni-icons type="medal" size="32" color="#666"></uni-icons>
					<text class="service-text">积分明细</text>
				</view>
				<view class="service-item" @click="goToReconciliation">
					<uni-icons type="bars" size="32" color="#666"></uni-icons>
					<text class="service-text">对账管理</text>
				</view>
				<view class="service-item" @click="goToPayment">
					<uni-icons type="wallet" size="32" color="#666"></uni-icons>
					<text class="service-text">收款方式</text>
				</view>
				<view class="service-item" @click="goToService">
					<uni-icons type="chatbubble" size="32" color="#666"></uni-icons>
					<text class="service-text">联系客服</text>
				</view>
			</view>
		</view>

		<!-- 底部功能 -->
		<view class="bottom-section">
			<view class="bottom-item" @click="goToAddress">
				<uni-icons type="location" size="32" color="#666"></uni-icons>
				<text class="bottom-text">收货地址</text>
			</view>
			<view class="bottom-item" @click="logout">
				<uni-icons type="loop" size="32" color="#666"></uni-icons>
				<text class="bottom-text">退出登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	import request from '@/utils/request.js'

	export default {
		data() {
			return {
				userInfo: {},
				confirmingCount: 0,
				salesConfirmingCount: 0,
				pendingCount: 0,
				salesPendingCount: 0,
				phone:''
			}
		},
		onReady() {
			// 从本地存储获取用户信息
			this.userInfo = uni.getStorageSync('userInfo') || {}
		},
		onLoad() {
			this.loadUserInfo()
			this.loadOrderStats()
			this.loadPhone()
			
		},
		onShow() {
			this.loadUserInfo()
			this.loadOrderStats()
			this.loadPhone()
		},
		methods: {
			// 获取完整的图片URL
			getFullImageUrl(url) {
				return request.getImageUrl(url)
			},
			async loadPhone(){
				const res= await request.get("/back/config",{key:"phone"})
				if(res.success) this.phone=res.data
			},
			// 加载用户信息
			async loadUserInfo() {
				try {
					const res = await request.get('/back/current-user')
					if (res.success) {
						this.userInfo = res.data
						// 更新本地存储中的用户信息
						uni.setStorageSync('userInfo', res.data)
					}
				} catch (error) {
					console.error('获取用户信息失败：', error)
				}
			},

			// 加载订单统计
			async loadOrderStats() {
				try {
					const res = await request.get('/back/user/orders/stats')
					if (res.success) {
						this.pendingCount = res.data.purchase_pending_count || 0
						this.confirmingCount = res.data.purchase_confirming_count || 0
						this.salesPendingCount = res.data.sales_pending_count || 0
						this.salesConfirmingCount = res.data.sales_confirming_count || 0
					}
				} catch (error) {
					console.error('获取订单统计失败：', error)
				}
			},

			// 跳转到个人信息编辑页面
			goToEditProfile() {
				uni.navigateTo({
					url: '/pages/profile/edit'
				})
			},

			// 跳转到订单页面
			goToOrders(type, status) {
				const url = type === 'purchase' ? '/pages/orders/orders' : '/pages/sales/sales'
				uni.navigateTo({
					url: `${url}?status=${status}`
				})
			},

			// 查看全部订单
			viewAllOrders(type) {
				const url = type === 'purchase' ? '/pages/orders/orders' : '/pages/sales/sales'
				uni.navigateTo({
					url: url
				})
			},

			// 跳转到仓库
			goToWarehouse() {
				uni.navigateTo({
					url: '/pages/warehouse/warehouse'
				})
			},

			// 跳转到积分明细
			goToScores() {
				uni.navigateTo({
					url: '/pages/scores/scores'
				})
			},

			// 跳转到对账管理
			goToReconciliation() {
				uni.navigateTo({
					url: '/pages/reconciliation/reconciliation'
				})
			},

			// 跳转到收款方式
			goToPayment() {
				uni.navigateTo({
					url: '/pages/payment/payment'
				})
			},

			// 联系客服
			goToService() {
				
			uni.showModal({
				title: '联系客服',
				content: '请在服务群内联系客服',
				showCancel: false,
				
				
			
			})
				// const res = uni.getSystemInfoSync();
				// const phone=""+this.phone
				// // ios系统默认有个模态框
				// if (res.platform == 'ios') {
				// 	uni.makePhoneCall({
				// 		phoneNumber: phone,
				// 	})
				// } else {
				// 	//安卓手机手动设置一个showActionSheet
				// 	uni.showActionSheet({
				// 		itemList: ['phone', '呼叫'],
				// 		success: function(res) {
				// 			if (res.tapIndex == 1) {
				// 				uni.makePhoneCall({
				// 					phoneNumber: phone,
				// 				})
				// 			}
				// 		}
				// 	})
				// }
			},

			// 跳转到收货地址
			goToAddress() {
				uni.navigateTo({
					url: '/pages/address/address'
				})
			},

			// 退出登录
			logout() {
				uni.showModal({
					title: '确认退出',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 清除本地存储
							uni.removeStorageSync('token')
							uni.removeStorageSync('userInfo')
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.container {
		background-color: #f5f5f5;
		min-height: 100vh;
	}

	/* 用户信息头部 */
	.user-header {
		background: linear-gradient(135deg, #FF4444 0%, #FF6666 100%);
		padding: 40rpx 30rpx;
		color: #fff;
	}

	.user-info {
		display: flex;
		align-items: center;
	}

	.avatar-container {
		margin-right: 30rpx;
	}

	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		border: 4rpx solid rgba(255, 255, 255, 0.3);
		display: flex;
		align-items: center;
		justify-content: center;
		overflow: hidden;
	}

	.avatar-image {
		width: 100%;
		height: 100%;
	}

	.default-avatar {
		background-color: rgba(255, 255, 255, 0.2);
	}

	.user-details {
		flex: 1;
	}

	.user-name {
		display: block;
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 10rpx;
	}

	.user-phone {
		display: block;
		font-size: 28rpx;
		opacity: 0.9;
		margin-bottom: 10rpx;
	}

	.user-points {
		font-size: 28rpx;
		opacity: 0.9;
		display: flex;
		align-items: center;
		background-color: rgba(255, 255, 255, 0.1);
		border-radius: 20rpx;
		padding: 8rpx 15rpx;
		margin-top: 10rpx;
		max-width: 200rpx;
	}

	.points-text {
		margin-right: 8rpx;
	}

	.edit-btn {
		padding: 20rpx;
	}

	/* 订单区域 */
	.order-section {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 30rpx;
	}

	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.view-all {
		font-size: 24rpx;
		color: #999;
	}

	.order-status-bar {
		display: flex;
		justify-content: space-around;
	}

	.status-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
	}

	.status-item uni-icons {
		margin-bottom: 15rpx;
	}

	.status-text {
		font-size: 24rpx;
		color: #666;
	}

	.status-badge {
		position: absolute;
		top: -5rpx;
		right: -5rpx;
		background-color: #FF4444;
		color: #fff;
		font-size: 20rpx;
		padding: 4rpx 8rpx;
		border-radius: 20rpx;
		min-width: 32rpx;
		text-align: center;
	}

	/* 服务区域 */
	.service-section {
		background-color: #fff;
		margin: 20rpx;
		border-radius: 16rpx;
		padding: 30rpx;
	}

	.service-grid {
		display: flex;
		justify-content: space-around;
		margin-top: 30rpx;
	}

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

	.service-item uni-icons {
		margin-bottom: 15rpx;
	}

	.service-text {
		font-size: 24rpx;
		color: #666;
	}

	/* 底部区域 */
	.bottom-section {
		display: flex;
		margin: 20rpx;
		gap: 20rpx;
	}

	.bottom-item {
		flex: 1;
		background-color: #fff;
		border-radius: 16rpx;
		padding: 40rpx 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.bottom-item uni-icons {
		margin-bottom: 15rpx;
	}

	.bottom-text {
		font-size: 24rpx;
		color: #666;
	}
</style>