<script setup>

const toast = useToast()
const supabase = useSupabaseClient()
const dataStore = useDataStore()
const profileStore = useProfileStore()
const router = useRouter()
const modal = useModal()
const props = defineProps({
  documentData: {
    type: Object,
    required: true
  },
  openShowModal: {
    type: Boolean,
    required: false,
  },
  returnEmit: {
    type: Boolean
  }

})

const emits = defineEmits("close")

const showSlideover = ref(props.openShowModal)




//Functions
const openDocument = async () => {
  //selectedDocument.value = doc
  openShowModal.value = true
  console.log("open")
}

const updateDocument = async () => {
  console.log("Update")

  const {url, ...objData} = props.documentData
  delete objData.url
  delete objData.filetags


  const {data,error} = await supabase
      .from("files")
      .update(objData)
      .eq('id',objData.id)
      .select()

  if(error) {
    console.log(error)
  } else {
    toast.add({title: "Datei aktualisiert"})
    //openShowModal.value = false
  }

}

const archiveDocument = () => {
  //documentData.tags.push("Archiviert")
  //updateDocument()
}

const resourceOptions = ref([
    {label: 'Projekt', value: 'project', optionAttr: "name"},
  {label: 'Kunde', value: 'customer', optionAttr: "name"},
  {label: 'Lieferant', value: 'vendor', optionAttr: "name"},
  {label: 'Fahrzeug', value: 'vehicle', optionAttr: "licensePlate"},
  {label: 'Objekt', value: 'plant', optionAttr: "name"},
  {label: 'Vertrag', value: 'contract', optionAttr: "name"},
  {label: 'Produkt', value: 'product', optionAttr: "name"}
])
const resourceToAssign = ref("project")
const itemOptions = ref([])
const idToAssign = ref(null)
const getItemsBySelectedResource = () => {
  if(resourceToAssign.value === "project") {
    itemOptions.value = dataStore.projects
  } else if(resourceToAssign.value === "customer") {
    itemOptions.value = dataStore.customers
  } else if(resourceToAssign.value === "vendor") {
    itemOptions.value = dataStore.vendors
  } else if(resourceToAssign.value === "vehicle") {
    itemOptions.value = dataStore.vehicles
  } else if(resourceToAssign.value === "product") {
    itemOptions.value = dataStore.products
  } else if(resourceToAssign.value === "plant") {
    itemOptions.value = dataStore.plants
  } else if(resourceToAssign.value === "contract") {
    itemOptions.value = dataStore.contracts
  } else {
    itemOptions.value =  []
  }
}
getItemsBySelectedResource()

const updateDocumentAssignment = async () => {
  props.documentData[resourceToAssign.value] = idToAssign.value
  await updateDocument()
}

</script>

<template>
  <UModal >
    <UCard :ui="{ body: { base: 'flex-1' }, ring: '', divide: 'divide-y divide-gray-100 dark:divide-gray-800' }">
      <template #header>
        <div class="flex flex-row justify-between">
          <div class="flex items-center gap-2">
            <UBadge
                v-for="tag in props.documentData.filetags"
            >
              {{tag.name}}
            </UBadge>
          </div>
          <UButton color="gray" variant="ghost" icon="i-heroicons-x-mark-20-solid" class="-my-1" @click="modal.close()" />
        </div>

      </template>
      <div class="flex flex-row">
        <div class="w-1/2">
          <object
              class="bigPreview"
              :data="`${props.documentData.url}#toolbar=0&navpanes=0&scrollbar=0`"
              type="application/pdf"
              v-if="!props.documentData.filetags.includes('Bild')"

          />
          <img
              class=" w-full"
              :src="props.documentData.url"
              alt=""
              v-else
          />
        </div>
        <div class="w-1/2 p-5">
          <UButtonGroup>
            <!--          <UButton
                        @click="archiveDocument"
                      >
                        Archivieren
                      </UButton>-->
            <UButton
                :to="props.documentData.url"
                variant="outline"
                icon="i-heroicons-arrow-top-right-on-square"
                target="_blank"
            >
              Öffnen
            </UButton>
          </UButtonGroup>


          <p>Dokument zuweisen:</p>
          <UFormGroup
              label="Resource auswählen"
          >
            <USelectMenu
                :options="resourceOptions"
                v-model="resourceToAssign"
                value-attribute="value"
                option-attribute="label"
                @change="getItemsBySelectedResource"
            >

            </USelectMenu>
          </UFormGroup>
          <UFormGroup
              label="Eintrag auswählen:"
          >

          </UFormGroup>

          <USelectMenu
              :options="itemOptions"
              v-model="idToAssign"
              :option-attribute="resourceOptions.find(i => i.value === resourceToAssign)? resourceOptions.find(i => i.value === resourceToAssign).optionAttr : 'name'"
              value-attribute="id"
              @change="updateDocumentAssignment"
          ></USelectMenu>
        </div>
      </div>
    </UCard>
  </UModal>
</template>

<style scoped>

.bigPreview {
  width: 100%;
  aspect-ratio: 1/ 1.414;
}

</style>