<template>
	<view class="ct-page">
		<view class="ct-nav" :style="{ paddingTop: statusBar + 'px' }">
			<view class="ct-nav-row">
				<view class="ct-back" @tap="goBack">
					<text class="ct-back-icon">←</text>
				</view>
				<text class="ct-nav-title">{{ copy.title }}</text>
				<view class="ct-nav-placeholder" />
			</view>
		</view>

		<scroll-view
			scroll-y
			class="ct-scroll"
			:style="{ height: scrollHeight + 'px' }"
		>
			<view class="ct-body" :style="{ paddingBottom: safeBottom + 'px' }">
				<view class="ct-hero">
					<view class="ct-hero-icon-wrap">
						<text class="ct-hero-icon">✉</text>
					</view>
					<text class="ct-hero-title">{{ copy.title }}</text>
					<text class="ct-hero-desc">{{ copy.desc }}</text>
				</view>

				<view v-if="loading" class="ct-state-card">
					<text class="ct-state-text">{{ copy.loading }}</text>
				</view>

				<view v-else-if="!email" class="ct-state-card">
					<text class="ct-state-icon">—</text>
					<text class="ct-state-text">{{ copy.empty }}</text>
				</view>

				<view
					v-else
					class="ct-email-card"
					hover-class="ct-email-card--press"
					@tap="copyEmail"
				>
					<view class="ct-email-head">
						<text class="ct-email-label">{{ copy.emailLabel }}</text>
						<view class="ct-copy-pill" @tap.stop="copyEmail">
							<text class="ct-copy-pill-text">{{ copy.copyBtn }}</text>
						</view>
					</view>
					<text class="ct-email-value">{{ email }}</text>
					<text class="ct-email-hint">{{ copy.tapCopy }}</text>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import { fetchAppBrand } from '@/features/shell/app-config-gateway'
import { copyText } from '@/core/storage/clipboard-copy'

export default {
	data() {
		return {
			statusBar: 0,
			scrollHeight: 600,
			safeBottom: 0,
			loading: false,
			email: ''
		}
	},
	computed: {
		copy() {
			const t = this.$t.bind(this)
			return {
				title: t('about.contactTitle'),
				desc: t('about.contactHeroDesc'),
				emailLabel: t('about.contactEmailLabel'),
				copyBtn: t('about.contactCopy'),
				tapCopy: t('about.contactTapCopy'),
				empty: t('about.contactEmpty'),
				loading: t('about.contactLoading'),
				network: t('user.realnameNetworkError')
			}
		}
	},
	onLoad() {
		const sys = uni.getSystemInfoSync()
		this.statusBar = sys.statusBarHeight || 0
		this.safeBottom = sys.safeAreaInsets ? sys.safeAreaInsets.bottom : 0
		const navH = this.statusBar + uni.upx2px(88)
		this.scrollHeight = (sys.windowHeight || 667) - navH
		this.loadContact()
	},
	methods: {
		goBack() {
			if (getCurrentPages().length > 1) {
				uni.navigateBack({ delta: 1 })
			} else {
				uni.navigateTo({
					url: '/pages/account/about',
					fail: () => uni.reLaunch({ url: '/pages/mine/index' })
				})
			}
		},
		async loadContact() {
			if (this.loading) return
			this.loading = true
			try {
				const brand = await fetchAppBrand()
				this.email = brand.email || ''
			} catch (_) {
				this.email = ''
				this.$appKit && this.$appKit.msg(this.copy.network)
			} finally {
				this.loading = false
			}
		},
		copyEmail() {
			if (!this.email) return
			copyText(this.email, true, this.$i18n)
		}
	}
}
</script>

<style scoped>
.ct-page {
	min-height: 100vh;
	background-color: #f4f6fb;
}

.ct-nav {
	background-color: #ffffff;
	border-bottom: 1rpx solid #eef1f6;
}

.ct-nav-row {
	display: flex;
	flex-direction: row;
	align-items: center;
	height: 88rpx;
	padding: 0 24rpx;
	box-sizing: border-box;
}

.ct-back {
	width: 64rpx;
	height: 64rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.ct-back-icon {
	font-size: 40rpx;
	color: #1a1d26;
	line-height: 1;
}

.ct-nav-title {
	flex: 1;
	text-align: center;
	font-size: 32rpx;
	font-weight: 600;
	color: #1a1d26;
}

.ct-nav-placeholder {
	width: 64rpx;
}

.ct-body {
	padding: 24rpx 28rpx 40rpx;
	box-sizing: border-box;
}

.ct-hero {
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 32rpx 24rpx 40rpx;
}

.ct-hero-icon-wrap {
	width: 120rpx;
	height: 120rpx;
	border-radius: 32rpx;
	background: linear-gradient(145deg, #eef4ff 0%, #e8f0fe 100%);
	box-shadow: 0 12rpx 36rpx rgba(74, 140, 255, 0.14);
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 24rpx;
}

.ct-hero-icon {
	font-size: 52rpx;
	line-height: 1;
}

.ct-hero-title {
	font-size: 36rpx;
	font-weight: 700;
	color: #1a1d26;
	margin-bottom: 12rpx;
}

.ct-hero-desc {
	font-size: 26rpx;
	color: #8a8d9e;
	line-height: 1.6;
	text-align: center;
	max-width: 560rpx;
}

.ct-state-card,
.ct-email-card {
	background-color: #ffffff;
	border-radius: 24rpx;
	box-shadow: 0 6rpx 24rpx rgba(74, 140, 255, 0.06);
	padding: 32rpx 28rpx;
	box-sizing: border-box;
}

.ct-state-card {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	min-height: 200rpx;
}

.ct-state-icon {
	font-size: 40rpx;
	color: #c5cad6;
	margin-bottom: 16rpx;
	line-height: 1;
}

.ct-state-text {
	font-size: 28rpx;
	color: #8a8d9e;
	text-align: center;
	line-height: 1.5;
}

.ct-email-card--press {
	background-color: #f7f9fc;
}

.ct-email-head {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	margin-bottom: 20rpx;
}

.ct-email-label {
	font-size: 26rpx;
	color: #8a8d9e;
}

.ct-copy-pill {
	padding: 10rpx 24rpx;
	border-radius: 999rpx;
	background-color: rgba(74, 140, 255, 0.1);
	border: 1rpx solid rgba(74, 140, 255, 0.22);
}

.ct-copy-pill-text {
	font-size: 24rpx;
	color: #4a8cff;
	font-weight: 600;
	line-height: 1.2;
}

.ct-email-value {
	display: block;
	font-size: 32rpx;
	font-weight: 600;
	color: #4a8cff;
	line-height: 1.5;
	word-break: break-all;
}

.ct-email-hint {
	display: block;
	margin-top: 16rpx;
	font-size: 24rpx;
	color: #b0b4c0;
	line-height: 1.4;
}
</style>
