<script setup lang="ts">
import type { TransactionLineItemResponse } from '@/api/transaction'
import AppButton from './common/AppButton.vue'
import { formatMoney, type Currency } from '@/api/data'

defineProps<{
  lineItem: TransactionLineItemResponse
  currency: Currency
  totalCount?: number
  editable?: boolean
}>()

defineEmits<{
  deleted: []
  movedUp: []
  movedDown: []
}>()
</script>
<template>
  <div class="line-item-card">
    <div class="line-item-card-description">
      {{ lineItem.description }}
    </div>
    <div class="line-item-card-details">
      <span class="font-mono font-size-small align-right text-muted">
        {{ lineItem.quantity }}x
      </span>
      <span class="font-mono font-size-small">
        {{ formatMoney(lineItem.valuePerItem, currency) }}
      </span>
      <AppButton
        icon="arrow-up"
        v-if="editable && lineItem.idx > 0"
        size="sm"
        @click="$emit('movedUp')"
      />
      <AppButton
        icon="arrow-down"
        v-if="editable && totalCount !== undefined && lineItem.idx < totalCount - 1"
        size="sm"
        @click="$emit('movedDown')"
      />
      <AppButton
        icon="trash"
        size="sm"
        v-if="editable"
        @click="$emit('deleted')"
      />
    </div>
  </div>
</template>
<style lang="css">
.line-item-card {
  display: flex;
  flex-direction: row;
  background-color: var(--bg);
  border-radius: 0.5rem;
  margin: 0.5rem 0;
}

.line-item-card-description {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  margin: 0.25rem;
}

.line-item-card-details {
  display: inline-block;
  margin: 0.25rem;
}
</style>
