<template>
  <div :class="['form-part', piyangoContactValidationStore.formChanged ? 'changed' : '']">
    <div class="form-part-title">
      <h4>İrtibat Bilgileri</h4>
      <div class="form-part-title-buttons"></div>
    </div>
    <div class="form-part-content" v-if="loaded">
      <form-input
        v-model="piyangoContactStore.contactData.adSoyad"
        label="Adı ve Soyadı"
        half
        modelKey="adSoyad"
        required
        minlength="2"
        :invalidText="piyangoContactValidationStore.invalidTexts.adSoyad"
        @keydown="validationStore.allowLettersWithKeysExtra($event, /^[ ]*$/)"
        @keyup="OnKeyup" />
      <form-input
        v-model="piyangoContactStore.contactData.unvan"
        label="Ünvanı"
        half
        modelKey="unvan"
        required
        minlength="2"
        :invalidText="piyangoContactValidationStore.invalidTexts.unvan"
        @keydown="validationStore.allowLettersWithKeysExtra($event, /^[ ]*$/)"
        @keyup="OnKeyup" />
      <form-input
        half
        v-model="piyangoContactStore.contactData.telefonNumarasi"
        label="Telefon Numarası"
        placeholder="Örn: 5555555555"
        type="tel"
        required
        @keydown="validationStore.allowNumbersWithKeys"
        maxlength="10"
        minlength="10"
        :invalidText="piyangoContactValidationStore.invalidTexts.telefonNumarasi"
        modelKey="telefonNumarasi"
        @keyup="OnKeyup" />
      <form-input
        half
        v-model="piyangoContactStore.contactData.faksNumarasi"
        label="Faks Numarası"
        placeholder="Örn: 5555555555"
        type="tel"
        @keydown="validationStore.allowNumbersWithKeys"
        maxlength="10"
        minlength="10"
        modelKey="faksNumarasi"
        @keyup="OnKeyup" />
      <form-input
        half
        v-model="piyangoContactStore.contactData.eposta"
        label="E-posta adresi"
        placeholder="Örn: adres@mail.com"
        type="email"
        :invalidText="piyangoContactValidationStore.invalidTexts.eposta"
        modelKey="eposta" />
      <div class="form-item" v-if="piyangoContactValidationStore.formChanged">
        <button class="button-c button-save" @click="SaveData">Kaydet</button>
        <button class="button-c button-cancel" @click="CancelSave">Vazgeç</button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
  import { ref, reactive, onBeforeMount } from 'vue'
  import { useDataStore } from '@/stores/dataStore'
  const dataStore = useDataStore()
  import { useValidationStore } from '@/stores/validationStore'
  const validationStore = useValidationStore()
  import { usePiyangoStore } from '../../stores/piyangoStore'
  const piyangoStore = usePiyangoStore()
  import { usePiyangoContactStore } from '../../stores/piyangoContactStore'
  const piyangoContactStore = usePiyangoContactStore()
  import { usePiyangoContactValidationStore } from '../../validation/piyangoContactValidationStore'
  const piyangoContactValidationStore = usePiyangoContactValidationStore()

  const isNew = ref<boolean>(false)
  const loaded = ref<boolean>(false)

  const OnKeyup = () => {
    piyangoContactValidationStore.formChanged = true
  }

  const GetData = async () => {
    loaded.value = false
    let dt = await dataStore.dataGet(
      'IrtibatBilgi/GetIrtibatBilgiCekilis/' + piyangoStore.selectedLottery,
      {
        skipErrorForStatuses: [404]
      }
    )
    piyangoContactStore.ResetFormData()

    if (dt !== 'errorfalse' && !dt?._error) {
      Object.assign(piyangoContactStore.contactData, dt)
      Object.assign(piyangoContactStore.safeContactData, dt)
    } else {
      Object.assign(piyangoContactStore.contactData, piyangoContactStore.safeContactData)
      isNew.value = true
    }
    setTimeout(() => {
      loaded.value = true
    }, 30)
  }
  const SaveData = async () => {
    if (piyangoContactValidationStore.FormCheck()) {
      let res: any

      if (isNew.value) {
        piyangoContactStore.contactData.cekilisId = piyangoStore.selectedLottery
        res = await dataStore.dataPost('IrtibatBilgi', {
          data: piyangoContactStore.contactData
        })
      } else {
        res = await dataStore.dataPut(
          'IrtibatBilgi/' + piyangoContactStore.contactData.id,
          {
            data: piyangoContactStore.contactData
          }
        )
      }

      if (res !== 'errorfalse') {
        GetData()
        piyangoContactValidationStore.formChanged = false
      }
    }
  }
  const CancelSave = () => {
    Object.assign(piyangoContactStore.contactData, piyangoContactStore.safeContactData)
    piyangoContactValidationStore.formChanged = false
  }

  onBeforeMount(async () => {
    piyangoContactStore.ResetFormData()
    await GetData()
  })
</script>
