<template>
	<view class="profile-page">
		<view v-if="loading" class="loading-bar">同步中…</view>
		<!-- 个人信息卡片（只读，数据来自 GET /users/me） -->
		<view class="card">
			<view class="row">
				<text class="label">头像</text>
				<view class="row-right">
					<view class="avatar-wrap">
						<UserAvatar
							:name="user.name"
							:id="user.id"
							:src="user.avatar"
							:size="80"
							unit="rpx"
						/>
					</view>
				</view>
			</view>
			<view class="row">
				<text class="label">姓名</text>
				<view class="row-right">
					<text class="value">{{ user.name || '—' }}</text>
				</view>
			</view>
			<view class="row">
				<text class="label">账号名</text>
				<view class="row-right">
					<text class="value" :class="{ placeholder: !user.alias }">{{ user.alias || '—' }}</text>
				</view>
			</view>
		</view>
		<view class="card">
			<view class="row no-arrow">
				<text class="label">企业</text>
				<text class="value">{{ displayOrg }}</text>
			</view>
		</view>
		<text class="hint">信息由服务端维护，暂不支持在应用内修改。</text>

		<view class="logout-section">
			<view class="logout-btn" @click="onLogout">退出登录</view>
		</view>
	</view>
</template>

<script>
	import UserAvatar from '../../components/UserAvatar.vue'
	import { getToken, getCurrentUserInfo, normalizeUserPayload, setToken } from '../../utils/api'

	const USER_KEY = 'current_user'
	const DEFAULT_ORG = '韫珠科技'

	export default {
		components: { UserAvatar },
		data() {
			return {
				loading: false,
				user: {
					name: '',
					id: '',
					avatar: '',
					alias: '',
					orgName: ''
				}
			}
		},
		computed: {
			displayOrg() {
				return this.user.orgName || DEFAULT_ORG
			}
		},
		onLoad() {
			this.applyFromStorage()
		},
		onShow() {
			this.applyFromStorage()
			this.refreshFromServer()
		},
		methods: {
			applyFromStorage() {
				try {
					const raw = uni.getStorageSync(USER_KEY)
					const u = normalizeUserPayload(raw)
					if (u) {
						this.user = {
							name: u.name,
							id: u.id,
							avatar: u.avatar,
							alias: u.alias,
							orgName: u.orgName || u.org_name || ''
						}
					}
				} catch (e) {}
			},
			async refreshFromServer() {
				const token = getToken()
				if (!token) return
				this.loading = true
				try {
					const me = await getCurrentUserInfo(token)
					const u = normalizeUserPayload(me)
					if (!u) return
					const orgName = u.orgName || u.org_name || DEFAULT_ORG
					this.user = {
						name: u.name,
						id: u.id,
						avatar: u.avatar,
						alias: u.alias,
						orgName
					}
					try {
						const prev = uni.getStorageSync(USER_KEY)
						const base = prev && typeof prev === 'object' ? prev : {}
						uni.setStorageSync(USER_KEY, {
							...base,
							...u,
							orgName,
							org_name: orgName
						})
					} catch (e) {}
				} catch (e) {
					// 网络失败时保留本地/缓存展示
				} finally {
					this.loading = false
				}
			},
			onLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (!res.confirm) return
						setToken('')
						try {
							uni.removeStorageSync(USER_KEY)
						} catch (e) {}
						this.user = {
							name: '',
							id: '',
							avatar: '',
							alias: '',
							orgName: ''
						}
						uni.reLaunch({ url: '/pages/login/index' })
					}
				})
			}
		}
	}
</script>

<style scoped>
	.profile-page {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 24rpx;
		padding-bottom: calc(120rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
		box-sizing: border-box;
	}
	.loading-bar {
		font-size: 24rpx;
		color: #999;
		margin-bottom: 16rpx;
		padding-left: 8rpx;
	}
	.card {
		background: #fff;
		border-radius: 16rpx;
		overflow: hidden;
		margin-bottom: 24rpx;
	}
	.row {
		display: flex;
		align-items: center;
		justify-content: space-between;
		min-height: 112rpx;
		padding: 0 32rpx;
		border-bottom: 1rpx solid #f0f0f0;
	}
	.row:last-child {
		border-bottom: none;
	}
	.row.no-arrow {
		border-bottom: none;
	}
	.label {
		font-size: 30rpx;
		color: #333;
		flex-shrink: 0;
	}
	.row-right {
		display: flex;
		align-items: center;
		gap: 16rpx;
		min-width: 0;
	}
	.value {
		font-size: 28rpx;
		color: #333;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
		max-width: 400rpx;
	}
	.value.placeholder {
		color: #999;
	}
	.avatar-wrap {
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.hint {
		display: block;
		font-size: 24rpx;
		color: #999;
		line-height: 1.5;
		padding: 0 16rpx;
	}
	.logout-section {
		margin-top: 48rpx;
		padding: 0 0 24rpx;
	}
	.logout-btn {
		background: #fff;
		border-radius: 16rpx;
		min-height: 96rpx;
		line-height: 96rpx;
		text-align: center;
		font-size: 32rpx;
		color: #e54d42;
	}
</style>
