<template>
  <view class="settings-page">
    <view class="custom-nav safe-area-top">
      <view class="nav-content">
        <view class="nav-back" @click="goBack">
          <uni-icons type="left" size="20" color="#1E293B"></uni-icons>
        </view>
        <text class="nav-title">账号设置</text>
      </view>
    </view>

    <scroll-view scroll-y class="page-content">
      <view class="settings-section">
        <view class="section-title">个人信息</view>
        <view class="settings-card">
          <view class="settings-item" @click="editAvatar">
            <text class="item-label">头像</text>
            <view class="item-value">
              <image v-if="userInfo.avatar" :src="userInfo.avatar" class="avatar-img"></image>
              <view v-else class="avatar-placeholder">
                <text>{{getAvatarText(userInfo.nickname)}}</text>
              </view>
              <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
            </view>
          </view>
          <view class="settings-item" @click="editNickname">
            <text class="item-label">昵称</text>
            <view class="item-value">
              <text>{{userInfo.nickname || '未设置'}}</text>
              <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
            </view>
          </view>
          <view class="settings-item">
            <text class="item-label">手机号</text>
            <view class="item-value">
              <text>{{maskPhone(userInfo.phone)}}</text>
            </view>
          </view>
        </view>
      </view>

      <view class="settings-section">
        <view class="section-title">安全设置</view>
        <view class="settings-card">
          <view class="settings-item" @click="changePassword">
            <text class="item-label">修改密码</text>
            <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
          </view>
          <view class="settings-item" @click="bindPhone">
            <text class="item-label">绑定手机</text>
            <view class="item-value">
              <text :class="{ 'text-muted': !userInfo.phone }">{{userInfo.phone ? '已绑定' : '未绑定'}}</text>
              <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
            </view>
          </view>
        </view>
      </view>

      <view class="settings-section">
        <view class="section-title">其他</view>
        <view class="settings-card">
          <view class="settings-item" @click="clearCache">
            <text class="item-label">清除缓存</text>
            <view class="item-value">
              <text>{{cacheSize}}</text>
              <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
            </view>
          </view>
          <view class="settings-item" @click="checkUpdate">
            <text class="item-label">检查更新</text>
            <view class="item-value">
              <text class="version">v{{version}}</text>
              <uni-icons type="right" size="16" color="#94A3B8"></uni-icons>
            </view>
          </view>
        </view>
      </view>

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

      <view class="safe-area-bottom" style="height: 40rpx;"></view>
    </scroll-view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        userInfo: {},
        cacheSize: '0KB',
        version: '1.0.0'
      }
    },
    onLoad() {
      this.loadUserInfo()
      this.calculateCacheSize()
    },
    onShow() {
      this.loadUserInfo()
    },
    methods: {
      loadUserInfo() {
        this.userInfo = uni.getStorageSync('userInfo') || {}
      },
      calculateCacheSize() {
        // 模拟计算缓存大小
        this.cacheSize = '2.5MB'
      },
      editAvatar() {
        uni.chooseImage({
          count: 1,
          sourceType: ['album', 'camera'],
          success: (res) => {
            this.userInfo.avatar = res.tempFilePaths[0]
            uni.setStorageSync('userInfo', this.userInfo)
            uni.showToast({ title: '头像更新成功', icon: 'success' })
          }
        })
      },
      editNickname() {
        uni.showModal({
          title: '修改昵称',
          editable: true,
          placeholderText: '请输入昵称',
          success: (res) => {
            if (res.confirm && res.content) {
              this.userInfo.nickname = res.content
              uni.setStorageSync('userInfo', this.userInfo)
              uni.showToast({ title: '修改成功', icon: 'success' })
            }
          }
        })
      },
      changePassword() {
        uni.navigateTo({ url: '/pages/change-password/change-password' })
      },
      bindPhone() {
        if (!this.userInfo.phone) {
          uni.navigateTo({ url: '/pages/bind-phone/bind-phone' })
        }
      },
      clearCache() {
        uni.showModal({
          title: '清除缓存',
          content: '确定要清除所有缓存数据吗？',
          success: (res) => {
            if (res.confirm) {
              uni.clearStorage()
              this.cacheSize = '0KB'
              uni.showToast({ title: '清除成功', icon: 'success' })
            }
          }
        })
      },
      checkUpdate() {
        uni.showToast({ title: '已是最新版本', icon: 'none' })
      },
      logout() {
        uni.showModal({
          title: '确认退出',
          content: '确定要退出登录吗？',
          success: (res) => {
            if (res.confirm) {
              uni.removeStorageSync('token')
              uni.removeStorageSync('userInfo')
              uni.reLaunch({ url: '/pages/login/login' })
            }
          }
        })
      },
      goBack() { uni.navigateBack() },
      getAvatarText(nickname) {
        return nickname ? nickname.charAt(0).toUpperCase() : '用'
      },
      maskPhone(phone) {
        return phone ? phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '未绑定'
      }
    }
  }
</script>

<style scoped>
  .settings-page { min-height: 100vh; background: #F8FAFC; display: flex; flex-direction: column; }
  .custom-nav { background: #FFFFFF; border-bottom: 2rpx solid #F1F5F9; }
  .nav-content { display: flex; align-items: center; padding: 20rpx 32rpx; }
  .nav-back { width: 60rpx; height: 60rpx; display: flex; align-items: center; justify-content: center; margin-right: 24rpx; }
  .nav-title { font-size: 36rpx; font-weight: 700; color: #1E293B; }
  .page-content { flex: 1; padding: 24rpx 32rpx; }
  .settings-section { margin-bottom: 32rpx; }
  .section-title { font-size: 28rpx; font-weight: 600; color: #94A3B8; margin-bottom: 16rpx; padding-left: 16rpx; }
  .settings-card { background: #FFFFFF; border-radius: 24rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.04); }
  .settings-item { display: flex; align-items: center; justify-content: space-between; padding: 28rpx 32rpx; border-bottom: 2rpx solid #F8FAFC; }
  .settings-item:last-child { border-bottom: none; }
  .item-label { font-size: 30rpx; color: #1E293B; }
  .item-value { display: flex; align-items: center; gap: 16rpx; font-size: 30rpx; color: #64748B; }
  .avatar-img { width: 80rpx; height: 80rpx; border-radius: 50%; }
  .avatar-placeholder { width: 80rpx; height: 80rpx; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: #FFFFFF; font-size: 32rpx; font-weight: 700; }
  .text-muted { color: #94A3B8; }
  .version { font-size: 26rpx; color: #94A3B8; }
  .logout-section { margin-top: 48rpx; }
  .logout-btn { width: 100%; height: 96rpx; background: #FEE2E2; color: #EF4444; font-size: 32rpx; font-weight: 600; border-radius: 16rpx; border: none; display: flex; align-items: center; justify-content: center; }
</style>
