<script lang='ts' setup>
import type { MyApp } from "./";
import { alertController } from "@ionic/vue";
import { arrowDownOutline, arrowUpOutline, exitOutline, homeOutline, walletOutline } from "ionicons/icons";
import { authClient } from "@/auth";
import { myApps } from "./";

const router = useRouter();
const userStore = useUserStore();
const walletStore = useWalletStore();
const { userProfile } = storeToRefs(userStore);

const balanceWallet = await walletStore.getWalletByType("balance");
const profitWallet = await walletStore.getWalletByType("profit");

function formatAmount(amount: number | string) {
  const num = Number(amount);
  return num.toLocaleString("zh-CN", {
    minimumFractionDigits: 0,
    maximumFractionDigits: 2,
  });
}

function handleRecharge() {
  router.push("/recharge");
}

function handleWithdraw() {
  router.push("/withdraw");
}
function handleClickApp(app: MyApp) {
  if (app.path.startsWith("http")) {
    window.open(app.path, "_blank");
  }
  else {
    router.push(app.path);
  }
}
async function handleLogout() {
  const alert = await alertController.create({
    header: "退出登录",
    message: "确定要退出登录吗？",
    buttons: [
      {
        text: "取消",
        role: "cancel",
      },
      {
        text: "退出登录",
        role: "destructive",
        handler: async () => {
          userStore.signOut();
          authClient.signOut();
          router.replace("/layout/riwa");
        },
      },
    ],
  });

  await alert.present();
}
</script>

<template>
  <ion-page>
    <ion-header class="ion-no-border">
      <ion-toolbar class="ion-toolbar">
        <img slot="start" src="@/assets/images/icon-1.png" class="h-8 w-8 ml-2" alt="Logo">
        <ion-title>深化改革</ion-title>
      </ion-toolbar>
    </ion-header>
    <ion-content :fullscreen="true">
      <!-- 顶部横幅和用户信息 -->
      <div class="relative -z-1">
        <img src="@/assets/images/profile-banner.jpg" class="h-50 w-full object-cover" alt="我的横幅">

        <div class="absolute top-10 left-5 flex items-center gap-3">
          <ion-avatar class="size-18 border-3 border-white shadow-lg">
            <img alt="用户头像" src="@/assets/images/avatar.jpg">
          </ion-avatar>
          <div>
            <div class="text-primary text-xl font-bold">
              {{ userProfile?.user.name }}
            </div>
            <div class="text-primary text-sm font-semibold opacity-90">
              手机号：{{ userProfile?.user.username }}
            </div>
            <div class="text-primary text-sm font-semibold opacity-90">
              邀请码：{{ userProfile?.referralCode }}
            </div>
          </div>
        </div>
      </div>

      <div class="ion-padding">
        <!-- 钱包余额卡片 -->
        <section class="mb-4 -mt-12">
          <div class="card rounded-2xl shadow-lg p-5">
            <div class="flex items-center gap-2 mb-4">
              <ion-icon :icon="walletOutline" class="text-2xl text-[#c41e3a]" />
              <div class="text-lg font-bold text-[#1a1a1a] m-0">
                我的钱包
              </div>
            </div>

            <!-- 余额展示 -->
            <div class="grid grid-cols-2 gap-4 mb-4">
              <div class="bg-linear-to-br from-[#fff7f0] to-[#ffe8e8] rounded-xl p-4">
                <div class="text-sm mb-1 font-semibold">
                  收益钱包
                </div>
                <div class="text-lg font-bold text-[#c41e3a] mb-1 leading-tight">
                  ¥{{ formatAmount(profitWallet?.available || 0) }}
                </div>
              </div>
              <div class="bg-linear-to-br from-[#fff7f0] to-[#ffe8e8] rounded-xl p-4">
                <div class="text-sm mb-1 font-semibold">
                  账户余额
                </div>
                <div class="text-lg font-bold text-[#c41e3a] mb-1 leading-tight">
                  ¥{{ formatAmount(balanceWallet?.available || 0) }}
                </div>
              </div>
            </div>

            <ion-button
              expand="block"
              class="flex-1 recharge-btn mb-5"
              @click="router.push('/asset_center')"
            >
              <ion-icon slot="start" :icon="walletOutline" class="mr-2" />
              资产中心
            </ion-button>

            <!-- 快捷操作按钮 -->
            <div class="flex gap-3">
              <ion-button
                expand="block"
                fill="outline"
                class="flex-1 withdraw-btn"
                @click="handleRecharge"
              >
                <ion-icon slot="start" :icon="arrowDownOutline" />
                快捷充值
              </ion-button>
              <ion-button
                expand="block"
                class="flex-1 withdraw-btn"
                fill="outline"
                @click="handleWithdraw"
              >
                <ion-icon slot="start" :icon="arrowUpOutline" />
                快捷提现
              </ion-button>
            </div>
          </div>
        </section>

        <!-- 我的应用 -->
        <section class="my-5">
          <div class="card rounded-2xl shadow-lg p-5">
            <div class="flex items-center gap-2 mb-4">
              <ion-icon :icon="homeOutline" class="text-2xl text-[#c41e3a]" />
              <div class="text-lg font-bold text-[#1a1a1a] m-0">
                我的应用
              </div>
            </div>

            <div class="grid grid-cols-4 gap-y-5 gap-x-2">
              <div
                v-for="app in myApps"
                :key="app.id"
                class="flex flex-col items-center gap-2 cursor-pointer transition-transform active:scale-95"
                @click="handleClickApp(app)"
              >
                <div
                  class="app-item w-13 h-13 rounded-xl flex-center shadow-md"
                >
                  <ion-icon :icon="app.icon" class="text-2xl text-white" />
                </div>
                <span class="text-xs text-[#333] font-medium text-center leading-tight">{{ app.name }}</span>
              </div>
            </div>
          </div>
        </section>

        <!-- 退出登录按钮 -->
        <section class="mb-5">
          <ion-button expand="block" fill="outline" @click="handleLogout">
            <ion-icon slot="start" :icon="exitOutline" />
            安全退出登录
          </ion-button>
        </section>
      </div>
    </ion-content>
  </ion-page>
</template>

<style lang='css' scoped>
.card {
  background: linear-gradient(180deg, #ffeef1, #ffffff 15%);
}
.recharge-btn {
  --background: linear-gradient(135deg, #c41e3a 0%, #8b1a2e 100%);
  --background-activated: linear-gradient(135deg, #8b1a2e 0%, #c41e3a 100%);
  --border-radius: 12px;
  --box-shadow: 0 2px 8px rgba(196, 30, 58, 0.3);
  font-weight: 600;
  font-size: 14px;
  height: 44px;
  text-transform: none;
}

.withdraw-btn {
  --border-color: #c41e3a;
  --color: #c41e3a;
  --border-radius: 12px;
  --border-width: 2px;
  font-weight: 600;
  font-size: 14px;
  height: 44px;
  text-transform: none;
}

.withdraw-btn::part(native) {
  font-weight: 600;
}
.app-item {
  background: linear-gradient(135deg, rgb(249 103 102 / 93%), rgb(195, 33, 32));
}
</style>
