<script setup lang="ts">
  import {useLabelEditorStore} from "~/store/labelEditorStore";
  import type {Ref} from "vue";
  import LabelCreate from "~/components/label/labelCreate.vue";

  let store = useLabelEditorStore();
  let dialog: Ref<boolean> = ref<boolean>(false);
  let loading: Ref<boolean> = ref<boolean>(false);

  watch(dialog, async (oldDialog) => {
    if (oldDialog) {
      loading.value = true;

      await store.loadSuppliers();

      loading.value = false;
    }
  })

  function closeDialog() {
    dialog.value = false;
  }
</script>

<template>
  <v-dialog
      v-model="dialog"
      :scrollable="true"
      width="900"
      max-height="750"
      height="750"
  >
    <template v-slot:activator="{ props }">
      <v-btn
          :flat="true"
          v-bind="props"
          size="small"
      >
        <v-icon>mdi-wheel-barrow </v-icon>
        <v-tooltip
            activator="parent"
            location="bottom"
        >
          Lieferanten
        </v-tooltip>
      </v-btn>
    </template>

    <v-card>
      <v-card-title>
        <v-row style="padding-top: 10px">
          <v-icon size="small">
            mdi-label
          </v-icon>

          Lieferanten

          <v-progress-circular v-if="loading" indeterminate :size="15" :width="2"></v-progress-circular>

          <v-spacer/>
          <v-divider vertical/>

          <SupplierCreate/>
        </v-row>
      </v-card-title>

      <v-divider/>

      <v-card-text>
        <SupplierRow
            v-for="(supplier) in store.suppliers"
            :key="supplier.id"
            v-bind:supplier="supplier"
        />
      </v-card-text>

      <v-divider/>

      <v-card-actions>
        <v-spacer/>

        <v-btn
            variant="text"
            :disabled="loading"
            @click="closeDialog()"
        >
          Schließen
        </v-btn>
      </v-card-actions>
    </v-card>
  </v-dialog>
</template>

<style scoped>

</style>