<template>
    <view class="container">
        <!-- 自定义头部 -->
        <view class="custom-header">
            <view class="header-left" @click="navBack">
                <image class="back-icon" src="/static/icons/chevron_right_dark.svg" style="transform: rotate(180deg);"></image>
            </view>
            <text class="header-title">设置</text>
            <view class="header-right"></view>
        </view>
        <view class="header-placeholder"></view>

        <!-- 个人信息组 -->
        <view class="group-card">
            <view class="list-item" @click="navTo('profile')">
                <text class="item-title">个人资料</text>
                <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
            </view>
            <view class="list-item" @click="navTo('auth')">
                <text class="item-title">认证信息</text>
                <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
            </view>
            <!-- <view class="list-item" @click="navTo('bank')">
                <text class="item-title">银行卡信息</text>
                <view class="item-right">
                    <view class="tag-status">已完善</view>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view> -->
            <view class="list-item no-border" @click="navTo('security')">
                <text class="item-title">账号与安全</text>
                <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
            </view>
        </view>

        <!-- 应用设置组 -->
        <view class="group-card">
            <!-- <view class="list-item" @click="navTo('push')">
                <text class="item-title">推送通知</text>
                <view class="item-right">
                    <text class="item-value">部分开启</text>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view> -->
            <view class="list-item">
                <view class="item-row-left">
                    <text class="item-title">位置上报</text>
                    <text class="item-subtitle">每隔20分钟自动上报位置</text>
                </view>
                <switch :checked="isGpsEnabled" color="#FF5722" style="transform:scale(0.8)" @change="onGpsSwitchChange"/>
            </view>
            <view class="list-item" @click="clearCache">
                <text class="item-title">清理缓存</text>
                <view class="item-right">
                    <text class="item-value">{{ cacheSize }}</text>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view>
            <view class="list-item no-border" @click="navTo('about')">
                <text class="item-title">关于我们</text>
                 <view class="item-right">
                    <text class="item-value">v{{ version }}</text>
                    <image class="arrow-icon" src="/static/icons/chevron_right.svg"></image>
                </view>
            </view>
        </view>

    </view>
</template>

<script>
import { startGpsTimer, stopGpsTimer } from '@/utils/gps'
export default {
    data() {
        return {
            version: '2.0.6',
            cacheSize: '0B',
            // 明确默认状态为开启：获取不到缓存即为 true，以此确保默认值为开启。
            isGpsEnabled: uni.getStorageSync('GPS_REPORT_ENABLED') !== false
        }
    },
    onLoad() {
        this.getAppVersion();
        this.getCacheSize();
    },
    methods: {
        navBack() {
             uni.navigateBack({
                delta: 1
            });
        },
        navTo(type) {
            let url = '';
            switch(type) {
                case 'profile':
                    url = '/pages/mine/settings/profile/index';
                    break;
                case 'auth':
                    url = '/pages/mine/settings/auth/index';
                    break;
                case 'bank':
                    url = '/pages/mine/settings/bank/index';
                    break;
                case 'security':
                    url = '/pages/mine/settings/security/index';
                    break;
                case 'push':
                    url = '/pages/mine/settings/notification/index';
                    break;
                case 'about':
                    url = '/pages/mine/settings/about/index';
                    break;
                default:
                    console.log('Navigate to:', type);
                    return;
            }
            uni.navigateTo({ url });
        },
        clearCache() {
            uni.showModal({
                title: '清理缓存',
                content: '确定要清理应用缓存吗？',
                success: (res) => {
                    if (res.confirm) {
                        try {
                            // 排除 token 等关键数据的清理方案，取决于实际需求。通常清除所有 Storage
                            // 为了安全起见只同步清理所有，或者根据业务决定
                            const token = uni.getStorageSync('Authorization_token') // 假设 token 名
                            const gpsToggle = uni.getStorageSync('GPS_REPORT_ENABLED')

                            uni.clearStorageSync();

                            // 恢复关键数据以便不用退出重新登录
                            if (token) uni.setStorageSync('Authorization_token', token)
                            uni.setStorageSync('GPS_REPORT_ENABLED', gpsToggle)

                            uni.showToast({ title: '清理完成', icon: 'success' });
                            this.getCacheSize();
                        } catch (err) {
                            console.error('清理缓存失败:', err);
                            uni.showToast({ title: err.message || err.msg || '请求失败', icon: 'none' });
                        }
                    }
                }
            });
        },
        getCacheSize() {
            try {
                const res = uni.getStorageInfoSync();
                const kb = res.currentSize;
                if (kb < 1024) {
                    this.cacheSize = kb + 'KB';
                } else {
                    this.cacheSize = (kb / 1024).toFixed(2) + 'MB';
                }
            } catch (err) {
                this.cacheSize = '0B';
            }
        },
        getAppVersion() {
            // #ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
                this.version = widgetInfo.version || '2.0.6';
            });
            // #endif
            // #ifndef APP-PLUS
            const systemInfo = uni.getSystemInfoSync();
            this.version = systemInfo.appVersion || '2.0.6';
            // #endif
        },
        onGpsSwitchChange(e) {
            const val = e.detail.value;
            this.isGpsEnabled = val;
            uni.setStorageSync('GPS_REPORT_ENABLED', val);
            if (val) {
                startGpsTimer();
            } else {
                stopGpsTimer();
            }
        }
    }
}
</script>

<style>
page {
    background-color: #F8F8F8;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}
.custom-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 88rpx;
    padding-top: var(--status-bar-height);
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 30rpx;
    padding-right: 30rpx;
    box-sizing: content-box; /* Exclude padding from height calculation if needed, or adjust */
    z-index: 100;
}
.header-placeholder {
    height: calc(88rpx + var(--status-bar-height));
}
.back-icon {
    width: 40rpx;
    height: 40rpx;
}
.header-title {
    font-size: 28rpx; /* 14pt */
    font-weight: bold;
    color: #333;
}
.header-right {
    width: 40rpx;
}

.container {
    padding: 20rpx 30rpx;
}
.group-card {
    background-color: #fff;
    border-radius: 20rpx;
    padding: 0 30rpx;
    margin-bottom: 30rpx;
}
.list-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100rpx;
    border-bottom: 1px solid #F5F5F5;
}
.list-item.no-border {
    border-bottom: none;
}
.item-title {
    font-size: 28rpx; /* 14pt */
    color: #333;
}
.item-right {
    display: flex;
    align-items: center;
}
.arrow-icon {
    width: 28rpx;
    height: 28rpx;
    opacity: 0.5;
    margin-left: 10rpx;
}
.tag-status {
    font-size: 22rpx;
    color: #4CAF50;
    background-color: #F1F8E9;
    padding: 4rpx 10rpx;
    border-radius: 6rpx;
    margin-right: 6rpx;
}
.item-value {
    font-size: 28rpx; /* Changed to 14pt (28rpx) */
    color: #999;
}
.item-column {
    display: flex;
    flex-direction: column;
}
.item-row-left {
    display: flex;
    align-items: center;
}
.item-subtitle {
    font-size: 24rpx; /* Changed to 12pt (24rpx) for better legibility */
    color: #999;
    margin-left: 20rpx; /* Moved to right of title */
    margin-top: 0;
}
</style>
