<template>
  <div :class="['form-part']" id="display-bilgi">
    <div class="form-part-title">
      <h4>Piyango Bilgileri</h4>
      <div class="form-part-title-buttons" v-if="!isPreview">
        <button
          @click="BasvuruBedeliMuhasebelestir"
          v-if="
            (piyangoStore.lotteryApprove === 1 || piyangoStore.lotteryApprove === 4) &&
            (!piyangoStore.lotteryBasvuruBedeliStatus ||
              piyangoStore.lotteryBasvuruBedeliStatus === null)
          "
          class="button-save">
          Başvuru Bedelini Muhasebeleştir
        </button>
        <button
          @click="IzinBedeliMuhasebelestir"
          v-if="
            piyangoStore.lotteryApprove === 4 &&
            piyangoStore.lotteryData.amacpiyangoId !== 3 &&
            (!piyangoStore.lotteryIzinBedeliStatus ||
              piyangoStore.lotteryIzinBedeliStatus === null)
          "
          class="button-save">
          İzin Bedelini Muhasebeleştir
        </button>
        <router-link :to="'/piyangolar/detay/' + route.params.piyangoId" class="button-c">
          Piyango Detayına Git
        </router-link>
      </div>
    </div>
    <piyango-bilgileri-display-content v-if="loaded" :isPreview="isPreview" />
  </div>
</template>
<script setup lang="ts">
  import { ref, onBeforeMount } from 'vue'

  import { useToastStore } from '@/components/global/toastStore'
  const toastStore = useToastStore()

  export interface Props {
    isPreview?: boolean
  }

  const props = withDefaults(defineProps<Props>(), { isPreview: false })

  import PiyangoBilgileriDisplayContent from '@/module/cekilisler/components/content/PiyangoBilgileriDisplayContent.vue'

  import { useRoute } from 'vue-router'
  const route = useRoute()

  import { useDataStore } from '@/stores/dataStore'
  const dataStore = useDataStore()
  import { usePreviewStore } from '@/stores/previewStore'
  const previewStore = usePreviewStore()
  import { usePiyangoStore } from '@/module/cekilisler/stores/piyangoStore'
  const piyangoStore = usePiyangoStore()

  const loaded = ref<boolean>(false)

  const GetData = async () => {
    let data = await dataStore.dataGet(
      'CekilisBilgi/Cekilis/' + piyangoStore.selectedLottery
    )

    if (data !== 'errorfalse') {
      Object.assign(piyangoStore.lotteryData, data)
      piyangoStore.lotterySelectedProvience =
        piyangoStore.lotteryData.kampsamIlStrList.join(', ')

      GetDuzenleyen()

      if (piyangoStore.lotteryData.baglisirketId !== null) GetAraciUye()

      loaded.value = true
    }
  }

  const GetDuzenleyen = async () => {
    let data = await dataStore.dataGet(
      'Auth/userbyuserid/' + piyangoStore.lotteryData.duzenleyenId
    )

    if (data !== 'errorfalse') {
      Object.assign(piyangoStore.duzenleyenData, data)
      piyangoStore.customerApproved = data.islemTipi === 4
    }
  }

  const GetAraciUye = async () => {
    let data = await dataStore.dataGet(
      'Auth/userbyuserid/' + piyangoStore.lotteryData.baglisirketId
    )
    if (data !== 'errorfalse') {
      Object.assign(piyangoStore.araciUyeData, data)
    }
  }
  const BasvuruBedeliMuhasebelestir = async () => {
    let data = await dataStore.dataGet(
      'MuhasebeSettings/BasvuruBedeli/' + piyangoStore.selectedLottery
    )
    if (data !== 'errorfalse') {
      toastStore.AddToast('Başvuru bedeli başarıyla muhasebeleştirildi. Fatura No: '+data.belgeNo, 'success', 30000)
      loaded.value = false
      await GetData()
      piyangoStore.lotteryBasvuruBedeliStatus = true

      setTimeout(() => {
        loaded.value = true
      }, 30)
    }
  }

  const IzinBedeliMuhasebelestir = async () => {
    let data = await dataStore.dataGet(
      'MuhasebeSettings/IzinBasvuruBedeli/' + piyangoStore.selectedLottery
    )
    if (data !== 'errorfalse') {
      toastStore.AddToast('İzin bedeli başarıyla muhasebeleştirildi. Fatura No: '+data.izinBelgeNo , 'success', 30000)
      loaded.value = false
      await GetData()
      piyangoStore.lotteryIzinBedeliStatus = true
      setTimeout(() => {
        loaded.value = true
      }, 30)
    }
  }

  onBeforeMount(async () => {
    await dataStore.GetIlList()
    await GetData()
  })
</script>
