<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { RouterLink } from 'vue-router'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'

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

const allCustomers = ref([])
const isLoading = ref(false)

const getAllCustomers = async () => {
  allCustomers.value = []
  isLoading.value = true

  try {
    const res = await axios.get('/customer')
    if (res.status === 200) {
      allCustomers.value = res.data.data
    } else if (res.status === 204) {
      toast.warning('No records found')
    }
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  isLoading.value = false
}

const handleDelete = async (id) => {
  try {
    const res = await axios.delete(`/customer/${id}`)
    if (res.status === 200) {
      toast.success('Successfully deleted customer')
    }

    getAllCustomers()
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }
}

onMounted(() => {
  getAllCustomers()
})
</script>

<template>
  <div v-if="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="table table-light table-striped table-hover">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">GSTIN</th>
        <th scope="col">Contact Name</th>
        <th scope="col">Phone Number</th>
        <th scope="col">E-Mail</th>

        <th scope="col" class="table-action-column">
          <div class="wrapper">
            <RouterLink to="/customer/new">
              <button class="btn btn-dark" v-on:click="getAllCustomers">
                <i class="bi bi-plus-lg"></i>
              </button>
            </RouterLink>
            <button class="btn btn-dark" v-on:click="getAllCustomers">
              <i class="bi bi-arrow-clockwise"></i>
            </button>
          </div>
        </th>
      </tr>
    </thead>

    <tbody>
      <tr v-for="(customer, index) in allCustomers">
        <td scope="row">{{ index + 1 }}</td>
        <td>{{ customer.Name }}</td>
        <td>{{ customer.Gstin }}</td>
        <td>{{ customer.ContactName }}</td>
        <td>{{ customer.Phone }}</td>
        <td>{{ customer.Email }}</td>

        <td 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" v-on:click="console.log('Edit: ', customer.ID)">
                    Edit Customer
                  </button>
                </li>
                <li>
                  <button class="dropdown-item" v-on:click="handleDelete(customer.ID)">
                    Delete Customer
                  </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>
