<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 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/?linkedDocument=${props.id}&type=${documentTypeToUse.value}&optionsToImport=${encodeURIComponent(JSON.stringify(optionsToImport.value))}`)
  modal.close()
}
</script>

<template>
  <UModal :fullscreen="false">
      <UCard>
        <template #header>
          Erstelltes Dokument Kopieren
        </template>

        <UFormGroup
            label="Dokumententyp:"
          class="mb-3"
        >
          <USelectMenu
              :options="Object.keys(dataStore.documentTypesForCreation).map(key => { return { ...dataStore.documentTypesForCreation[key], key}})"
              value-attribute="key"
              option-attribute="labelSingle"
              v-model="documentTypeToUse"
          >

          </USelectMenu>
        </UFormGroup>
        <UCheckbox
              v-for="key in Object.keys(optionsToImport).filter(i => documentTypeToUse !== props.type ? !['startText','endText'].includes(i) : true)"
              v-model="optionsToImport[key]"
            :label="mappings[key]"
        />


        <template #footer>
          <UButton
            @click="startImport"
          >
            Kopieren
          </UButton>
        </template>

      </UCard>
  </UModal>
</template>

<style scoped>

</style>