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

const baseWalletsCode = ["balance", "profit", "team", "check_in", "meeting_reward", "reform_pioneer_allowance", "reform_monthly_enjoy"];

const router = useRouter();
const walletStore = useWalletStore();
const { wallets } = storeToRefs(walletStore);
const baseWallets = computed(() => wallets.value.filter(w => baseWalletsCode.includes(w.walletType.code)));
const otherWallets = computed(() => wallets.value.filter(w => !baseWalletsCode.includes(w.walletType.code)));

// 当前激活的tab: base 基础资产, livelihood 七大民生保障
const activeTab = ref<"base" | "livelihood">("base");

// 当前显示的钱包列表
const displayWallets = computed(() => {
  return activeTab.value === "base" ? baseWallets.value : otherWallets.value;
});

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

function switchTab(tab: typeof activeTab.value) {
  activeTab.value = tab;
}

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>

          <!-- Tab 切换栏 -->
          <div class="tab-bar">
            <div
              class="tab-item"
              :class="{ active: activeTab === 'base' }"
              @click="switchTab('base')"
            >
              <span>基础资产</span>
              <div v-if="activeTab === 'base'" class="tab-indicator" />
            </div>
            <div
              class="tab-item"
              :class="{ active: activeTab === 'livelihood' }"
              @click="switchTab('livelihood')"
            >
              <span>七大民生保障</span>
              <div v-if="activeTab === 'livelihood'" class="tab-indicator" />
            </div>
          </div>

          <empty v-if="displayWallets.length === 0" class="my-10" title="暂无钱包" />
          <div v-else class="wallet-grid">
            <div
              v-for="wallet in displayWallets"
              :key="wallet.id"
              class="wallet-card"
            >
              <!-- 卡片头部 -->
              <div class="wallet-header">
                <div class="wallet-icon-wrapper">
                  <ion-icon :icon="cardOutline" class="wallet-icon" />
                </div>
                <div class="wallet-title">
                  <div class="wallet-name">
                    {{ wallet.walletType.name }}
                  </div>
                  <div class="wallet-code">
                    {{ wallet.walletType.code }}
                  </div>
                </div>
              </div>

              <!-- 余额信息 -->
              <div class="balance-section">
                <div class="balance-label">
                  可用余额
                </div>
                <div class="balance-amount">
                  <span class="currency">¥</span>
                  <span class="amount">{{ formatAmount(wallet.available) }}</span>
                </div>
              </div>

              <!-- 额外信息 -->
              <div class="wallet-footer">
                <div class="footer-item">
                  <span class="footer-label">冻结：</span>
                  <span class="footer-value">¥{{ formatAmount(wallet.frozen || 0) }}</span>
                </div>
                <div class="footer-item">
                  <span class="footer-label">总额：</span>
                  <span class="footer-value">¥{{ formatAmount(wallet.available || 0) }}</span>
                </div>
              </div>

              <!-- 装饰元素 -->
              <div class="card-decoration" />
            </div>
          </div>
        </section>
      </div>
    </ion-content>
  </ion-page>
</template>

<style lang='css' scoped>
.flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.tab-bar {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  background: #f5f5f5;
  border-radius: 12px;
  padding: 4px;
}

.tab-item {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  color: #666;
  cursor: pointer;
  transition: all 0.3s ease;
}

.tab-item.active {
  background: white;
  color: var(--ion-color-primary);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

.tab-item:active {
  transform: scale(0.98);
}

.tab-indicator {
  position: absolute;
  bottom: 2px;
  width: 20px;
  height: 3px;
  background: var(--ion-color-primary);
  border-radius: 2px;
}

.wallet-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.wallet-card {
  position: relative;
  background: linear-gradient(135deg, #ffffff 0%, #fff9f9 100%);
  border-radius: 12px;
  padding: 14px 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  transition: all 0.3s ease;
  border: 1px solid rgba(196, 30, 58, 0.08);
}

.wallet-card:active {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
}

.wallet-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.wallet-icon-wrapper {
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #c41e3a 0%, #8b1a2e 100%);
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(196, 30, 58, 0.25);
}

.wallet-icon {
  font-size: 20px;
  color: white;
}

.wallet-title {
  flex: 1;
}

.wallet-name {
  font-size: 15px;
  font-weight: 700;
  color: #1a1a1a;
  margin-bottom: 1px;
  letter-spacing: 0.2px;
}

.wallet-code {
  font-size: 11px;
  color: #999;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.balance-section {
  margin-bottom: 10px;
  padding: 10px 0;
  border-top: 1px solid rgba(0, 0, 0, 0.05);
  border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.balance-label {
  font-size: 11px;
  color: #999;
  margin-bottom: 4px;
  font-weight: 500;
}

.balance-amount {
  display: flex;
  align-items: baseline;
  gap: 3px;
}

.currency {
  font-size: 16px;
  font-weight: 700;
  color: #c41e3a;
}

.amount {
  font-size: 24px;
  font-weight: 800;
  color: #c41e3a;
  letter-spacing: 0.3px;
  line-height: 1;
}

.wallet-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.footer-item {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.footer-label {
  font-size: 11px;
  color: #999;
  font-weight: 500;
}

.footer-value {
  font-size: 12px;
  color: #666;
  font-weight: 600;
}

.card-decoration {
  position: absolute;
  right: -20px;
  bottom: -20px;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, rgba(196, 30, 58, 0.05) 0%, transparent 70%);
  border-radius: 50%;
  pointer-events: none;
}

/* 为不同的钱包添加不同的装饰颜色 */
.wallet-card:nth-child(2) .card-decoration {
  background: radial-gradient(circle, rgba(255, 140, 0, 0.05) 0%, transparent 70%);
}

.wallet-card:nth-child(3) .card-decoration {
  background: radial-gradient(circle, rgba(30, 136, 229, 0.05) 0%, transparent 70%);
}
</style>
