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

definePageMeta({
  middleware: "auth"
})

const supabase = useSupabaseClient()
const route = useRoute()
const router = useRouter()
const toast = useToast()
const id = ref(route.params.id ? route.params.id : null )
const numberRange = useNumberRange("customers")
const dataStore = useDataStore()

let currentItem = ref(null)



//Working
const mode = ref(route.params.mode || "show")
const itemInfo = ref({
  name: "",
  infoData: {},
  active: true
})

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

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


}

const editCustomer = async () => {
  router.push(`/customers/edit/${currentItem.value.id}`)
  setupPage()
}

const cancelEditorCreate = () => {
  mode.value = "show"
  itemInfo.value = {
    id: 0,
    name: "",
    infoData: {}
  }
}





setupPage()
</script>

<template>
  <UCard v-if="currentItem && mode == 'show'" >
    <template #header>
      <UBadge
          v-if="currentItem.active"
      >
        Kunde aktiv
      </UBadge>
      <UBadge
          v-else
          color="red"
      >
        Kunde gesperrt
      </UBadge>
      {{currentItem.name}}
    </template>

    <InputGroup>
      <UButton
        @click="router.push(`/projects/create?customer=${currentItem.id}`)"
      >
        + Projekt
      </UButton>
      <UButton
        @click="router.push(`/contacts/create?customer=${currentItem.id}`)"
      >
        + Ansprechpartner
      </UButton>
    </InputGroup>


    Kundennummer: {{currentItem.customerNumber}} <br>

    <UDivider
        class="my-2"
    />

    Informationen:<br>
    <div v-if="currentItem.infoData">
      <span v-if="currentItem.infoData.street">Straße + Hausnummer: {{currentItem.infoData.street}}<br></span>
      <span v-if="currentItem.infoData.zip && currentItem.infoData.city">PLZ + Ort: {{currentItem.infoData.zip}} {{currentItem.infoData.city}}<br></span>
      <span v-if="currentItem.infoData.tel">Telefon: {{currentItem.infoData.tel}}<br></span>
      <span v-if="currentItem.infoData.email">E-Mail: {{currentItem.infoData.email}}<br></span>
      <span v-if="currentItem.infoData.web">Web: {{currentItem.infoData.web}}<br></span>
      <span v-if="currentItem.infoData.ustid">USt-Id: {{currentItem.infoData.ustid}}<br></span>
    </div>

    <UDivider
        class="my-2"
    />

    Notizen:<br>
    {{currentItem.notes}}<br>

    <UDivider
        class="my-2"
    />

    Kontakte: <br>

    <ul>
      <li
          v-for="contact in dataStore.getContactsByCustomerId(currentItem.id)"
      >
        <router-link :to="'/contacts/show/' + contact.id">{{contact.salutation}} {{contact.fullName}} - {{contact.role}}</router-link>
      </li>
    </ul>




    <template #footer>
      <UButton
          v-if="mode == 'show' && currentItem.id"
          @click="editCustomer"
      >
        Bearbeiten
      </UButton>
      <UButton
          color="red"
          class="ml-2"
          disabled
      >
        Archivieren
      </UButton>
      <!-- TODO: Kunde archivieren        -->
    </template>



  </UCard>
  <UCard v-else-if="mode == 'edit' || mode == 'create'" >
    <template #header v-if="mode === 'edit'">
      <UBadge>{{itemInfo.customerNumber}}</UBadge>{{itemInfo.name}}
    </template>

    <UFormGroup
        label="Name:"
    >
      <UInput
          v-model="itemInfo.name"
      />
    </UFormGroup>

    <UFormGroup
        label="Kundennummer:"
    >
      <UInput
          v-model="itemInfo.customerNumber"
          placeholder="Leer lassen für automatisch generierte Nummer"
      />
    </UFormGroup>

    <UTooltip text="Ist ein Kunde nicht aktiv so wird er für neue Aufträge gesperrt">
      <UFormGroup
          label="Kunde aktiv:"
      >
        <UCheckbox
            v-model="itemInfo.active"
        />
      </UFormGroup>
    </UTooltip>
    <UFormGroup
        label="Notizen:"
    >
      <UTextarea
          v-model="itemInfo.notes"
      />
    </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="Telefon:"
    >
      <UInput
          v-model="itemInfo.infoData.tel"
      />
    </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>

    <template #footer>
      <UButton
          v-if="mode == 'edit'"
          @click="dataStore.updateItem('customers', itemInfo)"
      >
        Speichern
      </UButton>
      <UButton
          v-else-if="mode == 'create'"
          @click="dataStore.createNewItem('customers',itemInfo)"
      >
        Erstellen
      </UButton>
      <UButton
          @click="cancelEditorCreate"
          color="red"
          class="ml-2"
      >
        Abbrechen
      </UButton>
    </template>

  </UCard>

  <HistoryDisplay
      type="customer"
      v-if="currentItem"
      :element-id="currentItem.id"
  />
</template>

<style scoped>
td {
  padding: 0.2em;
}
</style>