<template>
  <ion-page>
    <Header 
      title="设置"
      :onBack="handleBackPress"
    />
    
    <ion-content class="settings-content">
      <div class="settings-section">
        <div class="section-header">
          账户
        </div>
        <div button @click="handleLogin" class="settings-item settings-item-clickable">
          <div class="item-text-primary">登录云同步</div>
          <div class="item-text-tertiary">未登录</div>
        </div>
      </div>
      
      <div class="settings-section">
        <div class="section-header">
          偏好设置
        </div>
        <div class="settings-item settings-item-border">
          <div class="item-text-primary">云同步</div>
          <ion-toggle 
            slot="end" 
            :checked="settings.cloudSync" 
            @ion-change="toggleCloudSync"
          ></ion-toggle>
        </div>
        <div class="settings-item">
          <div class="item-text-primary">深色模式</div>
          <ion-toggle 
            slot="end" 
            :checked="settings.darkMode" 
            @ion-change="toggleDarkMode"
          ></ion-toggle>
        </div>
      </div>
      
      <div class="settings-section">
        <div class="section-header">
          数据管理
        </div>
        <div button @click="handleBackup" class="settings-item settings-item-clickable settings-item-border">
          <img :src="'/assets/icons/drawable-xxhdpi/btn_save_pic.png'" class="item-icon" />
          <div class="item-text-primary">备份便签</div>
        </div>
        <div button @click="handleRestore" class="settings-item settings-item-clickable settings-item-border">
          <img :src="'/assets/icons/drawable-xxhdpi/btn_restore.png'" class="item-icon" />
          <div class="item-text-primary">恢复便签</div>
        </div>
        <div button @click="handleExport" class="settings-item settings-item-clickable settings-item-border">
          <img :src="'/assets/icons/drawable-xxhdpi/btn_share.png'" class="item-icon" />
          <div class="item-text-primary">导出便签</div>
        </div>
        <div button @click="handleImport" class="settings-item settings-item-clickable">
          <img :src="'/assets/icons/drawable-xxhdpi/btn_load_error.png'" class="item-icon" />
          <div class="item-text-primary">导入便签</div>
        </div>
      </div>
      
      <div class="settings-section">
        <div class="section-header">
          关于
        </div>
        <div class="settings-item settings-item-border">
          <div class="item-text-primary">版本</div>
          <div class="item-text-tertiary">1.0.0</div>
        </div>
        <div button @click="handlePrivacyPolicy" class="settings-item settings-item-clickable settings-item-border">
          <div class="item-text-primary">隐私政策</div>
        </div>
        <div button @click="handleTermsOfService" class="settings-item settings-item-clickable">
          <div class="item-text-primary">服务条款</div>
        </div>
      </div>
    </ion-content>
  </ion-page>
</template>

<script setup>
import { computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { useAppStore } from '../stores/useAppStore';
import Header from '../components/Header.vue';

const store = useAppStore();
const router = useRouter();

// 页面挂载时加载初始数据
// 从localStorage加载用户设置和便签数据
onMounted(() => {
  store.loadData();
});

// 切换云同步设置
// 调用store中的方法更新云同步状态
const toggleCloudSync = () => {
  store.toggleCloudSync();
};

// 切换深色模式设置
// 调用store中的方法更新深色模式状态
const toggleDarkMode = () => {
  store.toggleDarkMode();
};

// 处理登录云同步按钮点击事件
// 在完整实现中，这里会打开登录界面
const handleLogin = () => {
  console.log('Login to cloud');
};

// 处理隐私政策按钮点击事件
// 在完整实现中，这里会显示隐私政策内容
const handlePrivacyPolicy = () => {
  console.log('Privacy policy');
};

// 处理服务条款按钮点击事件
// 在完整实现中，这里会显示服务条款内容
const handleTermsOfService = () => {
  console.log('Terms of service');
};

// 处理备份便签按钮点击事件
// 在完整实现中，这里会执行便签备份操作
const handleBackup = () => {
  console.log('Backup notes');
};

// 处理恢复便签按钮点击事件
// 在完整实现中，这里会执行便签恢复操作
const handleRestore = () => {
  console.log('Restore notes');
};

// 处理导出便签按钮点击事件
// 在完整实现中，这里会执行便签导出操作
const handleExport = () => {
  console.log('Export notes');
};

// 处理导入便签按钮点击事件
// 在完整实现中，这里会执行便签导入操作
const handleImport = () => {
  console.log('Import notes');
};

const handleBackPress = () => {
  router.back();
};

const settings = computed(() => store.settings);
</script>

<style scoped>
.settings-content {
  background-color: var(--background);
}

.settings-section {
  margin-bottom: 12px;
  background-color: var(--background-card);
}

.section-header {
  background-color: var(--background-secondary);
  font-size: 13px;
  font-weight: 600;
  color: var(--text-tertiary);
  padding: 10px 16px;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: var(--background-card);
  padding: 14px 16px;
}

.settings-item-border {
  border-bottom: 1px solid var(--border);
}

.settings-item-clickable {
  cursor: pointer;
}

.item-text-primary {
  font-size: 16px;
  color: var(--text-primary);
}

.item-text-tertiary {
  font-size: 15px;
  color: var(--text-tertiary);
}

.item-icon {
  width: 20px;
  height: 20px;
  color: var(--text-primary);
  margin-right: 12px;
}

.settings-item-clickable .item-text-primary {
  flex: 1;
}

.settings-item-clickable {
  display: flex;
  align-items: center;
}
</style>