<script lang='ts' setup>
import { arrowDownOutline, arrowUpOutline, cardOutline, swapHorizontalOutline, syncOutline } from "ionicons/icons";

const router = useRouter();
const walletStore = useWalletStore();
const { wallets } = storeToRefs(walletStore);

onMounted(async () => {
  await walletStore.syncWallets();
});

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

// 操作按钮配置
const actions = [
  { id: "recharge", name: "充值", icon: arrowDownOutline, color: "#c32120" },
  { id: "withdraw", name: "提现", icon: arrowUpOutline, color: "#c32120" },
  { id: "transfer", name: "转账", icon: swapHorizontalOutline, color: "#c32120" },
  { id: "exchange", name: "兑换", icon: syncOutline, color: "#c32120" },
];

function handleAction(actionId: string) {
  router.push(`/${actionId}`);
}
</script>

<template>
  <ion-page>
    <ion-header class="ion-no-border">
      <ion-toolbar class="ion-toolbar">
        <ion-buttons slot="start">
          <back-button />
        </ion-buttons>
        <ion-title>资产中心</ion-title>
      </ion-toolbar>
    </ion-header>

    <ion-content :fullscreen="true">
      <div class="ion-padding">
        <!-- 操作按钮 -->
        <section class="my-5 grid grid-cols-4 gap-4">
          <div
            v-for="action in actions"
            :key="action.id"
            class="flex flex-col items-center gap-2 cursor-pointer transition-transform active:scale-95"
            @click="handleAction(action.id)"
          >
            <div
              class="w-12 h-12 rounded-xl flex-center shadow-lg"
              :style="{ background: action.color }"
            >
              <ion-icon :icon="action.icon" class="text-xl text-white" />
            </div>
            <span class="text-xs text-[#333] font-medium text-center">{{ action.name }}</span>
          </div>
        </section>

        <!-- 钱包列表 -->
        <section class="mt-8 mb-5">
          <div class="flex items-center gap-2 mb-4">
            <img src="@/assets/images/icon.png" class="size-7">
            <div class="text-xl font-bold text-[#1a1a1a]">
              我的钱包
            </div>
          </div>
          <empty v-if="wallets.length === 0" class="my-10" />
          <div v-else class="flex flex-col gap-4">
            <div
              v-for="wallet in wallets"
              :key="wallet.id"
              class="card rounded-2xl overflow-hidden shadow-sm p-5"
            >
              <div class="flex items-center justify-between">
                <div class="flex-1">
                  <div class="text-sm text-[#666] mb-1 flex items-center gap-2 font-bold">
                    <ion-icon :icon="cardOutline" class="text-xl text-primary" />
                    {{ wallet.walletType.name }}
                  </div>
                  <div class="text-2xl font-bold text-[#1a1a1a]">
                    ¥ {{ formatAmount(wallet.available) }}
                  </div>
                </div>
              </div>
            </div>
          </div>
        </section>
      </div>
    </ion-content>
  </ion-page>
</template>

<style lang='css' scoped>
.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}
.card {
  /* background: linear-gradient(45deg, #ffffff, #ffdbdb 100%); */
}
</style>
