<script setup>
const auth = useAuthStore()

const resources = {
  customers: {
    label: "Kunden"
  },
  vendors: {
    label: "Lieferanten"
  },
  products: {
    label: "Artikel"
  },
  spaces: {
    label: "Lagerplätze"
  },
  customerspaces: {
    label: "Kundenlagerplätze"
  },
  invoices: {
    label: "Rechnungen"
  },
  quotes: {
    label: "Angebote"
  },
  costEstimates: {
    label: "Kostenschätzungen"
  },
  inventoryitems: {
    label: "Inventarartikel"
  },
  customerinventoryitems: {
    label: "Kundeninventarartikel"
  },
  projects: {
    label: "Projekte"
  },
  confirmationOrders: {
    label: "Auftragsbestätigungen"
  },
  deliveryNotes: {
    label: "Lieferscheine"
  },
  packingSlips: {
    label: "Packscheine"
  },
  costcentres: {
    label: "Kostenstellen"
  }
}

const numberRanges = ref(auth.activeTenantData.numberRanges || {})

Object.keys(resources).forEach((key) => {
  if (!numberRanges.value[key]) {
    numberRanges.value[key] = {
      prefix: "",
      suffix: "",
      nextNumber: 1000
    }
  }
})

const updateNumberRanges = async (range) => {

  const res = await useNuxtApp().$api(`/api/tenant/numberrange/${range}`,{
    method: "PUT",
    body: {
      numberRange: numberRanges.value[range]
    }
  })

  console.log(res)

}


</script>

<template>
  <UDashboardNavbar
      title="Nummernkreise bearbeiten"
  >

  </UDashboardNavbar>
  <UDashboardToolbar>
    <UAlert
        title="Änderungen an diesen Werten betreffen nur neu Erstellte Einträge."
        color="error"
        variant="outline"
        icon="i-heroicons-exclamation-triangle"
    />
  </UDashboardToolbar>

  <table
      class="m-3"
  >
    <tr class="text-left">
      <th>Typ</th>
      <th>Prefix</th>
      <th>Nächste Nummer</th>
      <th>Suffix</th>
    </tr>
    <tr
        v-for="key in Object.keys(resources)"
    >
      <td>{{resources[key].label}}</td>
      <td>
        <UInput
            v-model="numberRanges[key].prefix"
            @change="updateNumberRanges(key)"
        />
      </td>
      <td>
        <UInput
            v-model="numberRanges[key].nextNumber"
            @change="updateNumberRanges(key)"
            type="number"
            step="1"
        />
      </td>
      <td>
        <UInput
            v-model="numberRanges[key].suffix"
            @change="updateNumberRanges(key)"
        />
      </td>
    </tr>
  </table>
</template>

<style scoped>

</style>
