<template>
  <view class="container">
    <view class="dream-bg dream-bg-one"></view>
    <view class="dream-bg dream-bg-two"></view>

    <scroll-view class="content-wrapper" scroll-y>
      <profile-header
        :avatar="PROFILE_INFO.avatar"
        :username="PROFILE_INFO.username"
        :user-id="PROFILE_INFO.userId"
        @copy-id="copyUserId"
      />

      <view class="stats-card">
        <view
          v-for="item in PROFILE_STATS"
          :key="item.label"
          class="stat-item"
          hover-class="button-hover"
          @tap="navigateTo(item.route)"
        >
          <text class="stat-value">{{ item.value }}</text>
          <text class="stat-label">{{ item.label }}</text>
        </view>
      </view>

      <view class="section-block">
        <view class="section-header">
          <text class="section-title">常用操作</text>
          <text class="section-subtitle">更快开始一次表达</text>
        </view>
        <view class="quick-grid">
          <view
            v-for="item in PROFILE_QUICK_ACTIONS"
            :key="item.title"
            class="quick-card"
            hover-class="card-hover"
            @tap="navigateTo(item.route)"
          >
            <view class="quick-icon">{{ item.icon }}</view>
            <text class="quick-title">{{ item.title }}</text>
            <text class="quick-desc">{{ item.desc }}</text>
          </view>
        </view>
      </view>

      <profile-menu-section
        v-for="(menuGroup, index) in profileMenuGroups"
        :key="index"
        :items="menuGroup"
        @select="handleMenuSelect"
      />

      <view class="button-container">
        <button class="logout-button" hover-class="button-hover">退出登录</button>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import ProfileHeader from "@/components/profile/ProfileHeader.vue";
import ProfileMenuSection, { type ProfileMenuItem } from "@/components/profile/ProfileMenuSection.vue";
import { APP_ROUTES } from "@/config/routes";
import {
  CONTACT_CONFIG,
  PROFILE_INFO,
  PROFILE_MENU_GROUPS,
  PROFILE_NOTICE_TEXT,
  PROFILE_PRIVACY_TEXT,
  PROFILE_QUICK_ACTIONS,
  PROFILE_STATS,
} from "@/config/profile";

const profileMenuGroups = PROFILE_MENU_GROUPS.map((group) => [...group]) as ProfileMenuItem[][];

const navigateTo = (route?: string): void => {
  if (!route) return;

  if (route === APP_ROUTES.home || route === APP_ROUTES.mailbox || route === APP_ROUTES.mine) {
    uni.switchTab({
      url: route,
    });
    return;
  }

  uni.navigateTo({
    url: route,
  });
};

const handleMenuSelect = (item: ProfileMenuItem): void => {
  if (item.action === "copyWechat") {
    copyWechatContact();
    return;
  }

  if (item.action === "privacy") {
    showInfo(PROFILE_PRIVACY_TEXT);
    return;
  }

  if (item.action === "notice") {
    showInfo(PROFILE_NOTICE_TEXT);
    return;
  }

  navigateTo(item.route);
};

const copyUserId = (): void => {
  uni.setClipboardData({
    data: PROFILE_INFO.userId,
    success: () => {
      uni.showToast({
        title: "ID已复制",
        icon: "success",
        duration: 2000,
      });
    },
  });
};

const copyWechatContact = (): void => {
  uni.setClipboardData({
    data: CONTACT_CONFIG.wechatId,
    success: () => {
      uni.showToast({
        title: CONTACT_CONFIG.copySuccessText,
        icon: "success",
        duration: 2000,
      });
    },
    fail: () => {
      uni.showToast({
        title: CONTACT_CONFIG.copyFailText,
        icon: "none",
        duration: 2000,
      });
    },
  });
};

const showInfo = (content: string): void => {
  uni.showModal({
    title: "提示",
    content,
    showCancel: false,
    confirmText: "知道了",
  });
};
</script>

<style scoped lang="scss">
page {
  overflow: hidden;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans",
    "Helvetica Neue", sans-serif;
}

.container {
  display: flex;
  flex-direction: column;
  padding: 0;
  background:
    linear-gradient(180deg, rgba(255, 244, 250, 0.98) 0%, rgba(239, 248, 255, 0.96) 48%, #f7f8fb 100%),
    linear-gradient(135deg, rgba(255, 204, 225, 0.22), rgba(199, 233, 255, 0.24));
  height: 100vh;
  overflow: hidden;
  box-sizing: border-box;
  position: relative;
}

.dream-bg {
  position: fixed;
  border-radius: 50%;
  pointer-events: none;
  z-index: 0;
}

.dream-bg-one {
  width: 360rpx;
  height: 360rpx;
  right: -150rpx;
  top: 90rpx;
  background: rgba(255, 193, 215, 0.23);
}

.dream-bg-two {
  width: 330rpx;
  height: 330rpx;
  left: -140rpx;
  top: 620rpx;
  background: rgba(171, 218, 255, 0.21);
}

.content-wrapper {
  position: relative;
  z-index: 1;
  flex: 1;
  height: 0;
  padding: 24rpx;
  box-sizing: border-box;
}

.stats-card {
  display: flex;
  margin-bottom: 22rpx;
  padding: 20rpx 10rpx;
  border-radius: 26rpx;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.84), rgba(247, 252, 255, 0.62)),
    rgba(255, 255, 255, 0.7);
  border: 1rpx solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 16rpx 36rpx rgba(93, 107, 139, 0.07);
}

.stat-item {
  flex: 1;
  text-align: center;
  border-right: 1rpx solid rgba(230, 236, 246, 0.82);
}

.stat-item:last-child {
  border-right: 0;
}

.stat-value,
.stat-label,
.section-title,
.section-subtitle,
.quick-title,
.quick-desc {
  display: block;
}

.stat-value {
  color: #20273a;
  font-size: 34rpx;
  font-weight: 800;
  line-height: 1.2;
}

.stat-label {
  margin-top: 8rpx;
  color: #8c96a8;
  font-size: 22rpx;
}

.section-block {
  margin-bottom: 22rpx;
}

.section-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 4rpx 14rpx;
}

.section-title {
  color: #20273a;
  font-size: 31rpx;
  font-weight: 800;
}

.section-subtitle {
  color: #8c96a8;
  font-size: 22rpx;
}

.quick-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 16rpx;
}

.quick-card {
  width: calc(50% - 8rpx);
  min-height: 150rpx;
  box-sizing: border-box;
  padding: 22rpx;
  border-radius: 24rpx;
  background:
    linear-gradient(145deg, rgba(255, 255, 255, 0.82), rgba(255, 248, 252, 0.62)),
    rgba(255, 255, 255, 0.64);
  border: 1rpx solid rgba(255, 255, 255, 0.88);
  box-shadow: 0 14rpx 32rpx rgba(93, 107, 139, 0.07);
}

.card-hover {
  transform: scale(0.98);
}

.quick-icon {
  width: 56rpx;
  height: 56rpx;
  line-height: 56rpx;
  margin-bottom: 16rpx;
  border-radius: 18rpx;
  text-align: center;
  color: #fff;
  font-size: 24rpx;
  font-weight: 800;
  background: linear-gradient(135deg, #ff78a2 0%, #83c6ff 100%);
}

.quick-title {
  color: #263044;
  font-size: 27rpx;
  font-weight: 800;
}

.quick-desc {
  margin-top: 8rpx;
  color: #8c96a8;
  font-size: 21rpx;
  line-height: 1.35;
}

.button-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 18rpx 0 40rpx;
}

.logout-button {
  width: 100%;
  height: 86rpx;
  line-height: 86rpx;
  border-radius: 999rpx;
  color: #d94f75;
  font-size: 28rpx;
  font-weight: 700;
  background: rgba(255, 255, 255, 0.74);
  border: 1rpx solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 14rpx 30rpx rgba(83, 96, 130, 0.07);
}

.logout-button::after {
  border: 0;
}

.button-hover {
  opacity: 0.76;
}
</style>
