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

import brandsTable from './../components/brands_table.vue'
import newBrand from './../components/new_brand.vue'

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

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

const getAllBrands = async () => {
  allBrands.value = []
  isLoading.value = true

  try {
    const res = await axios.get('/brand')
    if (res.status === 200) {
      allBrands.value = res.data.data
    }
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  isLoading.value = false
}

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

<template>
  <newBrand @added="getAllBrands()" />
  <brandsTable :isLoading="isLoading" :brands="allBrands" @refresh="getAllBrands()" />
</template>
