<script setup lang="ts">
import { formatMoney } from '@/api/data'
import { getSelectedProfile } from '@/api/profile'
import type { TransactionDraftListItem } from '@/api/transaction'
import { useRoute, useRouter } from 'vue-router'
import AppBadge from './common/AppBadge.vue'
import CategoryLabel from './CategoryLabel.vue'
import TagLabel from './TagLabel.vue'

const router = useRouter()
const route = useRoute()

const props = defineProps<{ draft: TransactionDraftListItem }>()

function goToDraft() {
  const profile = getSelectedProfile(route)
  router.push(`/profiles/${profile}/transaction-drafts/${props.draft.id}`)
}
</script>
<template>
  <div
    class="transaction-draft-card"
    @click="goToDraft()"
  >
    <div>
      <!-- Top row contains timestamp and amount. -->
      <div style="display: flex; justify-content: space-between">
        <div>
          <div class="font-mono font-size-xsmall text-normal">
            Draft #{{ draft.id }}
            <AppBadge
              v-if="draft.templateName"
              size="sm"
            >
              Template: {{ draft.templateName }}
            </AppBadge>
          </div>
          <div
            class="text-muted font-mono font-size-xsmall"
            v-if="draft.timestamp"
          >
            {{ new Date(draft.timestamp).toLocaleString() }}
          </div>
        </div>
        <div>
          <div
            class="font-mono align-right font-size-small"
            v-if="draft.amount && draft.currency"
          >
            {{ formatMoney(draft.amount, draft.currency) }}
          </div>
          <div
            v-if="draft.creditedAccount !== null"
            class="font-size-small text-muted"
          >
            Credited to <span class="text-normal font-bold">{{ draft.creditedAccount.name }}</span>
          </div>
          <div
            v-if="draft.debitedAccount !== null"
            class="font-size-small text-muted"
          >
            Debited to <span class="text-normal font-bold">{{ draft.debitedAccount.name }}</span>
          </div>
        </div>
      </div>

      <!-- Middle row contains the description. -->
      <div>
        <p class="transaction-draft-card-description">{{ draft.description }}</p>
      </div>
    </div>

    <!-- Bottom row contains other links. -->
    <div style="display: flex; justify-content: space-between">
      <div>
        <CategoryLabel
          :category="draft.category"
          v-if="draft.category"
          style="margin-left: 0"
        />
        <AppBadge v-if="draft.vendor">{{ draft.vendor.name }}</AppBadge>
      </div>
      <div>
        <!-- Only show the first 3 tags, and add a "+N" badge for any more. -->
        <TagLabel
          v-for="tag in draft.tags.slice(0, 3)"
          :key="tag"
          :tag="tag"
        />
        <AppBadge
          v-if="draft.tags.length > 3"
          class="text-muted"
          >+{{ draft.tags.length - 3 }}</AppBadge
        >
      </div>
    </div>
  </div>
</template>
<style>
.transaction-draft-card {
  background-color: var(--bg);
  padding: 0.5rem;
  border-radius: 0.5rem;
  margin: 0.5rem 0;
  cursor: pointer;
  height: 120px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.transaction-draft-card:hover {
  background-color: var(--bg-darker);
}

.transaction-draft-card-description {
  margin: 0.25rem 0;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
