<script setup>

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({
  infoData: {}
})

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

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

const editItem = async () => {
  router.push(`/vendors/edit/${currentItem.value.id}`)
}

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

setupPage()
</script>

<template>
  <UDashboardNavbar :title="currentItem ? `Lieferant: ${currentItem.name}` : (mode === 'create' ? 'Lieferant erstellen' : 'Lieferant bearbeiten')">
    <template #right>
      <UButton
          v-if="mode === 'edit'"
          @click="dataStore.updateItem('vendors',itemInfo)"
      >
        Speichern
      </UButton>
      <UButton
          v-else-if="mode === 'create'"
          @click="dataStore.createNewItem('vendors',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="editItem"
      >
        Bearbeiten
      </UButton>
    </template>
  </UDashboardNavbar>
  <UTabs
    :items="[{label: 'Informationen'},{label: 'Dokumente'}]"
    v-if="currentItem && mode == 'show'"
    class="p-5"
  >
    <template #item="{item}">
      <div v-if="item.label === 'Informationen'" class="mt-5 flex">
        <div class="w-1/2 mr-5">
          <UCard>
            <div v-if="currentItem.infoData" class="text-wrap">
              <p v-if="currentItem.infoData.street">Straße + Hausnummer: {{currentItem.infoData.street}}</p>
              <p v-if="currentItem.infoData.zip && currentItem.infoData.city">PLZ + Ort: {{currentItem.infoData.zip}} {{currentItem.infoData.city}}</p>
              <p v-if="currentItem.infoData.tel">Telefon: {{currentItem.infoData.tel}}</p>
              <p v-if="currentItem.infoData.email">E-Mail: {{currentItem.infoData.email}}</p>
              <p v-if="currentItem.infoData.web">Web: {{currentItem.infoData.web}}</p>
              <p v-if="currentItem.infoData.ustid">USt-Id: {{currentItem.infoData.ustid}}</p>
              <p v-if="currentItem.notes">Notizen:<br> {{currentItem.notes}}</p>
            </div>
          </UCard>
          <UCard class="mt-5">
            <Toolbar>
              <UButton
                  @click="router.push(`/contacts/create?vendor=${currentItem.id}`)"
              >
                + Ansprechpartner
              </UButton>
            </Toolbar>
            <UTable
                :rows="dataStore.getContactsByVendorId(currentItem.id)"
                @select="(row) => router.push(`/contacts/show/${row.id}`)"
                :columns="[{label: 'Anrede', key: 'salutation'},{label: 'Name', key: 'fullName'},{label: 'Rolle', key: 'role'}]"
                :empty-state="{ icon: 'i-heroicons-circle-stack-20-solid', label: 'Keine zugehörigen Ansprechpartner' }"

            >

            </UTable>
          </UCard>
        </div>
        <div class="w-1/2">
          <UCard class="h-full">
            <HistoryDisplay
                type="vendor"
                v-if="currentItem"
                :element-id="currentItem.id"
                :render-headline="true"
            />
          </UCard>
        </div>
      </div>


      <UCard class="mt-5" v-else>
        <div v-if="item.label === 'Dokumente'">
          <InputGroup>
            <DocumentUpload
                type="vendor"
                :element-id="currentItem.id"
            />
          </InputGroup>

          <DocumentList
              :documents="dataStore.getDocumentsByVendorId(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="Lieferantennr.:"
    >
      <UInput
          v-model="itemInfo.vendorNumber"
          placeholder="Leer lassen für automatisch generierte Nummer"
      />
    </UFormGroup>
    <UFormGroup
        label="Straße + Hausnummer"
    >
      <UInput
          v-model="itemInfo.infoData.street"
      />
    </UFormGroup>
    <UFormGroup
        label="Postleitzahl"
    >
      <UInput
          v-model="itemInfo.infoData.zip"
      />
    </UFormGroup>
    <UFormGroup
        label="Ort"
    >
      <UInput
          v-model="itemInfo.infoData.city"
      />
    </UFormGroup>
    <UFormGroup
        label="Land"
    >
      <USelectMenu
          :options="['Deutschland','Niederlande','Belgien','Italien', 'Frankreich','Irland','USA','Spanien', 'Schweden']"
          v-model="itemInfo.infoData.country"
      />
    </UFormGroup>

    <UFormGroup
        label="Telefon:"
    >
      <UInput
          v-model="itemInfo.infoData.tel"
      />
    </UFormGroup>

    <UFormGroup
        label="Notizen:"
    >
      <UTextarea
          v-model="itemInfo.notes"
      />
    </UFormGroup>
    <UFormGroup
        label="E-Mail:"
    >
      <UInput
          v-model="itemInfo.infoData.email"
      />
    </UFormGroup>
    <UFormGroup
        label="Webseite:"
    >
      <UInput
          v-model="itemInfo.infoData.web"
      />
    </UFormGroup>
    <UFormGroup
        label="USt-Id:"
    >
      <UInput
          v-model="itemInfo.infoData.ustid"
      />
    </UFormGroup>
  </UForm>
</template>

<style scoped>

</style>