<script lang='ts' setup>
import type { InfiniteScrollCustomEvent, RefresherCustomEvent } from "@ionic/vue";
import type { UserWithdrawOrderBody, UserWithdrawOrderData } from "@/api/types";
import { alertController } from "@ionic/vue";
import CryptocurrencyColorNuls from "~icons/cryptocurrency-color/nuls";
import { client, safeClient } from "@/api";

const [query] = useResetRef<UserWithdrawOrderBody>({
  limit: 20,
  offset: 0,
});

const withdrawData = ref<UserWithdrawOrderData[]>([]);
const isFinished = ref(false);
async function fetchRwaData() {
  const { data } = await safeClient(() => client.api.withdraw.get({
    query: query.value,
  }));
  withdrawData.value.push(...(data.value?.data || []));
  isFinished.value = (data.value?.data.length || 0) < query.value.limit!;
}
function resetRwaData() {
  query.value.offset = 0;
  withdrawData.value = [];
  isFinished.value = false;
}
async function handleRefresh(event: RefresherCustomEvent) {
  resetRwaData();
  await fetchRwaData();
  setTimeout(() => {
    event.target.complete();
  }, 500);
}

async function handleInfinite(event: InfiniteScrollCustomEvent) {
  if (isFinished.value) {
    event.target.complete();
    event.target.disabled = true;
    return;
  }
  query.value.offset! += query.value.limit!;
  await fetchRwaData();
  setTimeout(() => {
    event.target.complete();
  }, 500);
}
async function handleCancel(id: string) {
  const alert = await alertController.create({
    header: "确认取消提现？",
    buttons: [
      {
        text: "取消",
        role: "cancel",
      },
      {
        text: "确认取消",
        role: "destructive",
        handler: async () => {
          await safeClient(client.api.withdraw({ orderId: id }).delete());
          resetRwaData();
          await fetchRwaData();
        },
      },
    ],
  });

  await alert.present();
}

onBeforeMount(() => {
  fetchRwaData();
});
</script>

<template>
  <ion-refresher slot="fixed" @ion-refresh="handleRefresh($event)">
    <ion-refresher-content />
  </ion-refresher>

  <div class="space-y-3 antialiased my-3">
    <div v-for="item in withdrawData" :key="item.id">
      <ion-grid class="py-3 space-y-2">
        <ion-row class="ion-align-items-center">
          <ion-col size="12" class="flex items-center">
            <div class="mr-2">
              <CryptocurrencyColorNuls class="text-md" />
            </div>
            <div class="text-xs font-semibold">
              <span class="mr-2">{{ item.assetCode }}</span>
              <ui-tag type="primary" size="small">
                {{ $t(`recharge.status.${item.status}`) }}
              </ui-tag>
            </div>
          </ion-col>
        </ion-row>
        <ion-row class="ion-align-items-center">
          <ion-col size="3" class="flex flex-col">
            <div class="text-xs text-text-500">
              金额
            </div>
            <div class="text-sm font-bold">
              {{ Number(item.amount).toFixed(2) }}
            </div>
          </ion-col>
          <ion-col size="3" class="flex flex-col">
            <div class="text-xs text-text-500">
              实际到账
            </div>
            <div class="text-sm font-bold">
              {{ Number(item.actualAmount).toFixed(2) }}
            </div>
          </ion-col>
          <ion-col size="6" class="flex flex-col">
            <div class="text-xs text-text-500 text-right">
              创建时间
            </div>
            <div class="text-xs font-bold text-right">
              {{ useDateFormat(item.createdAt, 'MM/DD HH:mm') }}
            </div>
          </ion-col>
        </ion-row>
        <ion-row class="ion-align-items-center">
          <ion-col size="3" class="flex flex-col">
            <div class="text-xs text-text-500">
              手续费
            </div>
            <div class="text-sm font-bold">
              {{ Number(item.fee).toFixed(2) }}
            </div>
          </ion-col>
          <ion-col size="3" class="flex flex-col">
            <div class="text-xs text-text-500">
              提现方式
            </div>
            <div class="text-xs font-bold">
              <span>{{ item.withdrawMethod }}</span>
              <span v-show="item.withdrawMethod === 'crypto'" class="ml-1">/ {{ item.chain }}</span>
            </div>
          </ion-col>
          <ion-col size="6" class="text-right">
            <ion-button v-if="item.status === 'pending'" size="small" fill="outline" color="success" @click.stop="handleCancel(item.id)">
              取消提现
            </ion-button>
          </ion-col>
        </ion-row>
      </ion-grid>
    </div>
  </div>

  <ion-infinite-scroll threshold="100px" @ion-infinite="handleInfinite">
    <ion-infinite-scroll-content
      loading-spinner="bubbles"
    />
  </ion-infinite-scroll>
</template>

<style lang='css' scoped>
ion-grid {
  border-bottom: 1px solid var(--ion-background-color-step-150);
}
</style>
