<script setup>

const dataStore = useDataStore()
const modal = useModal()
const router = useRouter()
const props = defineProps({
  id: {
    type: String,
    required: true
  },
  type: {
    type: String,
    required: true
  },
})



const emit = defineEmits(["updateNeeded","returnData"])

const documentTypeToUse = ref(props.type)
const documentTypeItems = computed(() => {
  return Object.keys(dataStore.documentTypesForCreation).map((key) => ({
    ...dataStore.documentTypesForCreation[key],
    key
  }))
})

const visibleImportKeys = computed(() => {
  return Object.keys(optionsToImport.value).filter((key) => {
    if (documentTypeToUse.value !== props.type) {
      return !['startText', 'endText'].includes(key)
    }

    return true
  })
})

const optionsToImport = ref({
  taxType: true,
  customer: true,
  letterhead: true,
  contact: true,
  deliveryDateType: true,
  deliveryDate: true,
  deliveryDateEnd: true,
  documentDate: false,
  paymentDays: true,
  customSurchargePercentage: true,
  contactPerson: true,
  plant: true,
  project:true,
  description: true,
  startText: false,
  rows: true,
  endText: false,
})

const mappings = ref({
  customer: "Kunde",
  taxType: "Steuertyp",
  letterhead: "Briefpapier",
  contact: "Ansprechpartner",
  deliveryDateType: "Lieferdatumsart",
  deliveryDate: "Lieferdatum / Lieferzeitraum Start",
  deliveryDateEnd: "Lieferzeitraum Ende",
  documentDate: "Belegdatum",
  paymentDays: "Zahlungsziel in Tagen",
  customSurchargePercentage: "Individueller Aufschlag",
  contactPerson: "Ansprechpartner Mitarbeiter",
  plant: "Objekt",
  project: "Projekt",
  description: "Beschreibung",
  startText: "Einleitung",
  rows: "Positionen",
  endText: "Nachbemerkung",
})

const startImport = () => {
  router.push(`/createDocument/edit/?createddocument=${props.id}&type=${documentTypeToUse.value}&optionsToImport=${encodeURIComponent(JSON.stringify(optionsToImport.value))}`)
  modal.close()
}
</script>

<template>
  <UModal>
    <template #content>
      <UCard class="mx-auto w-full max-w-2xl shadow-xl ring-1 ring-black/5">
        <template #header>
          <div class="flex items-start gap-4">
            <div class="flex h-12 w-12 items-center justify-center rounded-2xl bg-primary/10 text-primary ring-1 ring-primary/15">
              <UIcon name="i-heroicons-document-duplicate" class="h-6 w-6" />
            </div>
            <div class="min-w-0">
              <h2 class="text-lg font-semibold text-highlighted">Erstelltes Dokument kopieren</h2>
              <p class="mt-1 text-sm text-muted">Wähle den Zieltyp und welche Inhalte in das neue Dokument übernommen werden sollen.</p>
            </div>
          </div>
        </template>

        <div class="space-y-6">
          <UFormField label="Dokumententyp" required>
            <USelectMenu
                v-model="documentTypeToUse"
                :items="documentTypeItems"
                value-key="key"
                label-key="labelSingle"
                class="w-full"
                size="lg"
                :search-input="{ placeholder: 'Dokumententyp suchen...' }"
                :filter-fields="['labelSingle']"
            />
          </UFormField>

          <div class="space-y-3">
            <div>
              <h3 class="text-sm font-medium text-highlighted">Inhalte übernehmen</h3>
              <p class="mt-1 text-sm text-muted">Nur die aktivierten Bereiche werden in das neue Dokument kopiert.</p>
            </div>

            <div class="grid gap-3 sm:grid-cols-2">
              <UCheckbox
                  v-for="key in visibleImportKeys"
                  :key="key"
                  v-model="optionsToImport[key]"
                  :label="mappings[key]"
                  class="rounded-xl border border-default px-3 py-2"
              />
            </div>
          </div>
        </div>

        <template #footer>
          <div class="flex justify-end gap-2">
            <UButton color="neutral" variant="ghost" @click="modal.close()">
              Abbrechen
            </UButton>
            <UButton @click="startImport">
              Kopieren
            </UButton>
          </div>
        </template>
      </UCard>
    </template>
  </UModal>
</template>

<style scoped>

</style>
