<script setup lang="js">
import axios from 'axios'
import { useToast } from 'vue-toast-notification'

const props = defineProps(["items", "isLoading", "preview"])
const emit = defineEmits(["refresh"])

const toast = useToast({
  position: 'top-right'
})

const handleDelete = async (id) => {
  try {
    await axios.delete(`/invoice/item/${id}`)
    emit("refresh")
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }
}
</script>

<template>
  <div v-if="props.isLoading" class="w-100 d-flex justify-content-center">
    <div class="spinner-border" role="status">
      <span class="sr-only"></span>
    </div>
  </div>

  <table v-else :class="`invoice-items-table table table-striped table-hover ${props.preview ? 'table-light' : ''}`">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Item Name</th>
        <th scope="col">Description</th>
        <th scope="col">Brand</th>
        <th scope="col">HSN</th>
        <th scope="col">Unit Price</th>
        <th scope="col">GST <span class="sup text-muted">%</span></th>
        <th scope="col">Quantity <span class="sup text-muted">Unit</span></th>
        <th scope="col">Amount</th>

        <th v-if="!props.preview" scope="col" class="table-action-column">
          <div class="wrapper">
            <button class="btn btn-dark" v-on:click="emit('refresh')">
              <i class="bi bi-arrow-clockwise"></i>
            </button>
          </div>
        </th>
      </tr>
    </thead>

    <tbody>
      <tr v-for="(item, index) in props.items" :key="item['id']">
        <td scope="row">{{ index + 1 }}</td>
        <td>{{ item.Name }}</td>
        <td>{{ item.Description }}</td>
        <td>{{ item.BrandName }}</td>
        <td>{{ item.HSN }}</td>
        <td>{{ item.UnitPrice }}</td>
        <td>
          {{ item.GSTValue }} <span class="sup text-muted">{{ item.GSTPercentage }}%</span>
        </td>
        <td>{{ item.Quantity }} <span class="sup text-muted">{{ item.UnitOfMeasure }}</span></td>
        <td>{{ item.TotalAmount }} <span class="sup text-muted">{{ item.AmountWithoutGST }} + {{ item.TotalGSTValue }}</span> </td>

        <td v-if="!props.preview" class="table-action-column">
          <div class="wrapper">
            <button class="btn" data-bs-toggle="dropdown" aria-expanded="false">
              <i class="bi bi-caret-down-fill"></i>
            </button>

            <div class="dropdown">
              <ul class="dropdown-menu">
                <li>
                  <button class="dropdown-item disabled" v-on:click="console.log('Edit: ', item.ID)">
                    Edit Item
                  </button>
                </li>
                <li>
                  <button class="dropdown-item" v-on:click="handleDelete(item.ID)">
                    Delete Item
                  </button>
                </li>
              </ul>
            </div>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<style>
.table-action-column .wrapper {
  display: flex;
  justify-content: flex-end;
}

thead .table-action-column .wrapper {
  gap: 0.5rem;
}

tbody .table-action-column .btn {
  opacity: 0;
  transition: opacity 300ms;
}

tbody tr:hover .table-action-column .btn {
  opacity: 1;
  transition: opacity 300ms;
}
</style>
