<script setup>
import HistoryDisplay from "~/components/HistoryDisplay.vue";
import DocumentList from "~/components/DocumentList.vue";
import DocumentUpload from "~/components/DocumentUpload.vue";

definePageMeta({
  middleware: "auth"
})

const dataStore = useDataStore()
const route = useRoute()
const router = useRouter()
const toast = useToast()
const id = ref(route.params.id ? route.params.id : null )

let currentItem = ref(null)

//Working
const mode = ref(route.params.mode || "show")
const itemInfo = ref({
    unit: 1,
    tags: []
})

//Functions
const setupPage = () => {
  if(mode.value === "show" || mode.value === "edit"){
    currentItem.value = dataStore.getProductById(Number(useRoute().params.id))
  }

  if(mode.value === "edit") itemInfo.value = currentItem.value

}

const cancelEditorCreate = () => {
  if(currentItem.value) {
    router.push(`/products/show/${currentItem.value.id}`)
  } else {
    router.push(`/products/`)
  }
}

setupPage()
</script>

<template>
  <UDashboardNavbar
      :title="currentItem ? currentItem.name : (mode === 'create' ? 'Artikel erstellen' : 'Artikel bearbeiten')"
      :ui="{center: 'flex items-stretch gap-1.5 min-w-0'}"
  >
    <template #left>
      <UButton
          icon="i-heroicons-chevron-left"
          variant="outline"
          @click="router.push(`/products`)"
      >
        Artikel
      </UButton>
    </template>
    <template #center>
      <h1
          v-if="currentItem"
          class="text-xl font-medium"
      >{{currentItem ? `Artikel: ${currentItem.name}` : (mode === 'create' ? 'Artikel erstellen' : 'Artikel bearbeiten')}}</h1>
    </template>
    <template #right>
      <UButton
          v-if="mode === 'edit'"
          @click="dataStore.updateItem('products',itemInfo)"
      >
        Speichern
      </UButton>
      <UButton
          v-else-if="mode === 'create'"
          @click="dataStore.createNewItem('products',itemInfo)"
      >
        Erstellen
      </UButton>
      <UButton
          @click="cancelEditorCreate"
          color="red"
          class="ml-2"
          v-if="mode === 'edit' || mode === 'create'"
      >
        Abbrechen
      </UButton>
      <UButton
          v-if="mode === 'show'"
          @click="  router.push(`/products/edit/${currentItem.id}`)"
      >
        Bearbeiten
      </UButton>
    </template>
  </UDashboardNavbar>
  <UTabs
    :items="[{label: 'Informationen'},{label: 'Logbuch'},{label: 'Bestand'},{label: 'Dokumente'}]"
    v-if="mode === 'show'"
    class="p-5"
  >
    <template #item="{item}">
      <UCard class="mt-5">
        <div
          v-if="item.label === 'Informationen'"
        >
          <UBadge
              v-for="tag in currentItem.tags"
              class="mr-2"
          >
            {{tag}}
          </UBadge>
          <UDivider
              class="my-2"
          />
          <span v-if="currentItem.purchasePrice">Einkaufspreis: {{Number(currentItem.purchasePrice).toFixed(2)}} €<br></span>

        </div>
        <div
          v-if="item.label === 'Logbuch'"
        >
          <HistoryDisplay
              type="product"
              v-if="currentItem"
              :element-id="currentItem.id"
          />
        </div>
        <div
          v-if="item.label === 'Bestand'"
        >
          Bestand: {{dataStore.getStockByProductId(currentItem.id)}} {{dataStore.units.find(unit => unit.id === currentItem.unit) ? dataStore.units.find(unit => unit.id === currentItem.unit).name : ""}}

        </div>
        <div
          v-if="item.label === 'Dokumente'"
        >
          <Toolbar>
            <DocumentUpload
                type="product"
                :element-id="currentItem.id"
            />
          </Toolbar>


          <DocumentList :documents="dataStore.getDocumentsByProductId(currentItem.id)"/>
        </div>
      </UCard>
    </template>
  </UTabs>
  <UForm
      v-else-if="mode == 'edit' || mode == 'create'"
      class="p-5"
  >
    <UFormGroup
        label="Name:"
    >
      <UInput
          v-model="itemInfo.name"
      />
    </UFormGroup>
    <UFormGroup
        label="Hersteller:"
    >
      <UInput
          v-model="itemInfo.manufacturer"
      />
    </UFormGroup>
    <UFormGroup
        label="Hersteller Nr.:"
    >
      <UInput
          v-model="itemInfo.manufacturerNumber"
      />
    </UFormGroup>
    <UFormGroup
        label="Einheit:"
    >
      <USelectMenu
          v-model="itemInfo.unit"
          :options="dataStore.units"
          option-attribute="name"
          value-attribute="id"
      >
        <template #label>
          {{dataStore.units.find(unit => unit.id === itemInfo.unit) ? dataStore.units.find(unit => unit.id === itemInfo.unit).name : itemInfo.unit }}
        </template>
      </USelectMenu>
    </UFormGroup>
    <UFormGroup
        label="Tags:"
    >
      <USelectMenu
          v-model="itemInfo.tags"
          :options="dataStore.ownTenant.tags.products"
          multiple
      >
        <template #label>
          {{itemInfo.tags.length > 0 ? itemInfo.tags.join(", ") : "Keine Tags ausgewählt"}}
        </template>
      </USelectMenu>
    </UFormGroup>
    <UFormGroup
        label="EAN:"
    >
      <UInput
          v-model="itemInfo.ean"
      />
    </UFormGroup>
    <UFormGroup
        label="Barcode:"
    >
      <UInput
          v-model="itemInfo.barcode"
      />
    </UFormGroup>
    <UFormGroup
        label="Verkaufspreis:"
    >
      <UInput
          v-model="itemInfo.sellingPrice"
          type="number"
          steps="0.01"
      >
        <template #trailing>
          <span class="text-gray-500 dark:text-gray-400 text-xs">EUR</span>
        </template>
      </UInput>
    </UFormGroup>
    <UFormGroup
        label="Beschreibung:"
    >
      <UTextarea
          v-model="itemInfo.description"
      />
    </UFormGroup>
  </UForm>
</template>

<style scoped>

</style>