<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="SendChangeRequest"
          v-if="piyangoStore.lotteryApprove === 4 && !usersStore.isPanelUser && loaded">
          Değişiklik Talebi Gönder
        </button>
        <button
          @click="OpenPreviewPanel"
          v-if="
            piyangoStore.lotteryData.amacpiyangoId !== 3 &&
            piyangoStore.lotteryApprove !== 12 &&
            loaded
          ">
          Önizleme
        </button>
      </div>
    </div>
    <div class="form-inner-comment waiting-d" v-if="!loaded">Yükleniyor, lütfen bekleyiniz...</div>
    <piyango-bilgileri-display-content v-if="loaded" :isPreview="isPreview" />
  </div>
  <panel-wrapper
    wide
    v-if="previewPanel"
    v-model="previewPanel"
    panel-title="Piyango Bilgileri Önizleme">
    <template #panelContent>
      <panel-piyango-yardim-amacli-onizleme
        v-model:id="previewId"
        v-if="piyangoStore.lotteryData.amacpiyangoId === 2" />
      <panel-piyango-onizleme v-model:id="previewId" v-else />
    </template>
    <template #footerButton>
      <div class="button-c button-save" @click="DownloadPDF">PDF İndir</div>
      <div class="button-c button-save" @click="PrintContent">Yazdır</div>
    </template>
  </panel-wrapper>
</template>
<script setup lang="ts">
  import { ref, onBeforeMount } from 'vue'
  import { usePDF } from 'vue3-pdfmake'
  import { useUsersStore } from '@/stores/usersStore'
  const usersStore = useUsersStore()

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

  import PanelPiyangoOnizleme from '../panel/PanelPiyangoOnizleme.vue'
  import PanelPiyangoYardimAmacliOnizleme from '../panel/PanelPiyangoYardimAmacliOnizleme.vue'
  import PanelWrapper from '@/components/PanelWrapper.vue'
  import PiyangoBilgileriDisplayContent from '../content/PiyangoBilgileriDisplayContent.vue'

  import { useDataStore } from '@/stores/dataStore'
  const dataStore = useDataStore()
  import { usePreviewStore } from '@/stores/previewStore'
  const previewStore = usePreviewStore()
  import { usePiyangoStore } from '../../stores/piyangoStore'
  const piyangoStore = usePiyangoStore()
  import { usePiyangoOnayService } from '../../service/piyangoOnayService'
  const piyangoOnayService = usePiyangoOnayService()
  import { usePiyangoOnayStore } from '../../stores/piyangoOnayStore'
  const piyangoOnayStore = usePiyangoOnayStore()

  const loaded = ref<boolean>(false)
  const previewPanel = ref<boolean>(false)
  const previewId = ref<number>(0)

  const GetData = async () => {
    piyangoStore.ResetLotteryContent()

    let data = await dataStore.dataGet(
      'CekilisBilgi/Cekilis/' + piyangoStore.selectedLottery
    )

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

      await GetDuzenleyen()

      if (piyangoStore.lotteryData.baglisirketId !== null) await GetAraciUye()
      piyangoStore.lotteryIlanTarihi = piyangoStore.lotteryData.ilantarihi
      piyangoStore.lotteryCreationDate = piyangoStore.lotteryData.olusturmaTarihi

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

  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 OpenPreviewPanel = () => {
    previewPanel.value = true
  }

  const pdfmake = usePDF()
  const DownloadPDF = async () => {
    var pdfData: string = document.querySelector('.pdf-content')?.outerHTML || ''
    previewStore.CreatePdfFromHtml(pdfmake, pdfData)
  }
  const PrintContent = () => {
    previewStore.PrintContent(previewId.value)
  }

  const SendChangeRequest = async () => {
    piyangoOnayStore.piyangoOnayForm.onayDurumuIslemTipiId = 7
    piyangoOnayStore.piyangoOnayForm.file = ''
    piyangoOnayStore.piyangoOnayForm.aciklama = ''
    await piyangoOnayService.SaveOnayDurum()
  }

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