<template>
	<view class="container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<view class="back-btn" @click="goBack">
				<text class="back-icon">←</text>
			</view>
			<text class="header-title">设置</text>
			<view class="header-right"></view>
		</view>
		
		<scroll-view class="scroll-content" scroll-y>
			<!-- 开关设置项 -->
			<view class="settings-section">
				<view class="setting-item" v-for="(setting, index) in toggleSettings" :key="index">
					<text class="setting-label">{{ setting.label }}</text>
					<switch 
						class="setting-switch" 
						:checked="setting.value" 
						@change="handleToggleChange(index, $event)"
						color="#4FC3F7"
					/>
				</view>
			</view>
			
			<!-- 分隔线 -->
			<view class="divider"></view>
			
			<!-- 信息项 -->
			<view class="info-section">
				<view class="info-item" @click="handleInfoClick('clearCache')">
					<text class="info-label">清理缓存</text>
					<view class="info-right">
						<text class="info-value">{{ cacheSize }}</text>
						<text class="info-arrow">></text>
					</view>
				</view>
				<view class="info-item" @click="handleInfoClick('about')">
					<text class="info-label">关于我们</text>
					<text class="info-arrow">></text>
				</view>
				<view class="info-item" @click="handleInfoClick('contact')">
					<text class="info-label">联系我们</text>
					<text class="info-arrow">></text>
				</view>
			</view>
			
			<!-- 分隔线 -->
			<view class="divider"></view>
			
			<!-- 退出登录按钮 -->
			<view class="logout-section">
				<button class="logout-btn" @click="handleLogout">退出登录</button>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				toggleSettings: [
					{
						label: '阅读时不自动锁屏',
						value: false,
						key: 'noAutoLock'
					},
					{
						label: '阅读时隐藏别人的笔记',
						value: false,
						key: 'hideOthersNotes'
					},
					{
						label: '阅读时显示时间和电量',
						value: true,
						key: 'showTimeBattery'
					},
					{
						label: '阅读时允许横屏',
						value: false,
						key: 'allowLandscape'
					},
					{
						label: '点赞提醒',
						value: true,
						key: 'likeReminder'
					}
				],
				cacheSize: '20M'
			}
		},
		onLoad() {
			// 加载保存的设置
			this.loadSettings()
			// 加载缓存大小
			this.loadCacheSize()
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			handleToggleChange(index, event) {
				this.toggleSettings[index].value = event.detail.value
				// 保存设置到本地存储
				this.saveSettings()
			},
			handleInfoClick(type) {
				switch(type) {
					case 'clearCache':
						this.clearCache()
						break
					case 'about':
						this.showAbout()
						break
					case 'contact':
						this.showContact()
						break
				}
			},
			clearCache() {
				uni.showModal({
					title: '提示',
					content: `确定要清理 ${this.cacheSize} 缓存吗？`,
					success: (res) => {
						if (res.confirm) {
							// 清理缓存
							uni.clearStorageSync()
							this.cacheSize = '0M'
							uni.showToast({
								title: '缓存已清理',
								icon: 'success'
							})
						}
					}
				})
			},
			showAbout() {
				uni.navigateTo({
					url: '/pages/about/about'
				})
			},
			showContact() {
				uni.showModal({
					title: '联系我们',
					content: '邮箱: support@yunread.com\n电话: 400-123-4567',
					showCancel: false
				})
			},
			handleLogout() {
				uni.showModal({
					title: '提示',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 清除登录信息
							uni.removeStorageSync('token')
							uni.removeStorageSync('userInfo')
							
							uni.showToast({
								title: '已退出登录',
								icon: 'success'
							})
							
							// 跳转到登录页
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/login/login'
								})
							}, 1500)
						}
					}
				})
			},
			loadSettings() {
				// 从本地存储加载设置
				const savedSettings = uni.getStorageSync('appSettings')
				if (savedSettings) {
					this.toggleSettings.forEach((setting, index) => {
						if (savedSettings[setting.key] !== undefined) {
							this.toggleSettings[index].value = savedSettings[setting.key]
						}
					})
				}
			},
			saveSettings() {
				// 保存设置到本地存储
				const settings = {}
				this.toggleSettings.forEach(setting => {
					settings[setting.key] = setting.value
				})
				uni.setStorageSync('appSettings', settings)
			},
			loadCacheSize() {
				// 计算缓存大小
				try {
					const info = uni.getStorageInfoSync()
					const size = (info.keys.length * 2).toFixed(0) // 简单估算
					this.cacheSize = size + 'M'
				} catch (e) {
					this.cacheSize = '20M'
				}
			}
		}
	}
</script>

<style scoped>
	.container {
		width: 100%;
		height: 100vh;
		min-height: 100vh;
		background-color: #FFFFFF;
		display: flex;
		padding-top: 30px;
		box-sizing: border-box;
		flex-direction: column;
		box-sizing: border-box;
		overflow: hidden;
	}
	
	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 30rpx;
		padding-top: calc(20rpx + env(safe-area-inset-top));
		background-color: #FFFFFF;
		border-bottom: 1rpx solid #F0F0F0;
		position: relative;
		flex-shrink: 0;
		box-sizing: border-box;
	}
	
	.back-btn {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
	}
	
	.back-icon {
		font-size: 40rpx;
		color: #333333;
		font-weight: bold;
	}
	
	.header-title {
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		font-size: 36rpx;
		font-weight: bold;
		color: #333333;
	}
	
	.header-right {
		width: 60rpx;
	}
	
	.scroll-content {
		flex: 1;
		width: 100%;
		height: 0;
		overflow: hidden;
		padding-bottom: calc(env(safe-area-inset-bottom));
		background-color: #FFFFFF;
		box-sizing: border-box;
	}
	
	.settings-section {
		background-color: #FFFFFF;
		padding: 0 30rpx;
	}
	
	.setting-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.setting-item:last-child {
		border-bottom: none;
	}
	
	.setting-label {
		font-size: 30rpx;
		color: #333333;
		flex: 1;
	}
	
	.setting-switch {
		transform: scale(0.9);
	}
	
	.divider {
		height: 20rpx;
		background-color: #F5F5F5;
	}
	
	.info-section {
		background-color: #FFFFFF;
		padding: 0 30rpx;
	}
	
	.info-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 30rpx 0;
		border-bottom: 1rpx solid #F0F0F0;
	}
	
	.info-item:last-child {
		border-bottom: none;
	}
	
	.info-label {
		font-size: 30rpx;
		color: #333333;
		flex: 1;
	}
	
	.info-right {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}
	
	.info-value {
		font-size: 28rpx;
		color: #999999;
	}
	
	.info-arrow {
		font-size: 32rpx;
		color: #CCCCCC;
	}
	
	.logout-section {
		padding: 40rpx 30rpx;
		background-color: #FFFFFF;
	}
	
	.logout-btn {
		width: 100%;
		height: 88rpx;
		background-color: transparent;
		color: #FF1744;
		font-size: 32rpx;
		border: none;
		border-radius: 44rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.logout-btn::after {
		border: none;
	}
</style>

