import { defineStore } from 'pinia'
import { ref, reactive, computed } from 'vue'
import { usePiyangoStore } from './piyangoStore'
import { usePiyangoKatilimciValidationStore } from '../validation/piyangoKatilimciValidationStore'

export const usePiyangoTalihliStore = defineStore('piyangoTalihliStore', () => {
  const piyangoStore = usePiyangoStore()
  const piyangoKatilimciValidationStore = usePiyangoKatilimciValidationStore()

  const piyangoKatilimciUserSafeFormData = reactive<Record<string, any>>({
    ikramiyeId: null,
    cekilisKatilimSiraNo: null,
    sifreCode: '',
    magazaKartNumarasi: '',
    adi: '',
    soyadi: '',
    dogumTarihi: '',
    telefonNo: '',
    evIkametgahAdresi: '',
    ilce: '',
    il: '',
    eposta: '',
    katilimTarihi: '',
    fisFaturaTarihi: '',
    fisFaturaNo: '',
    kampanyaliUrunTutari: null,
    toplamTutar: null,
    magazaAdiYeri: '',
    cekilisId: piyangoStore.selectedLottery,
    asilTalihli: false,
    yedekTalihli: false
  })

  const piyangoKatilimciFileSafeFormData = reactive<Record<string, any>>({
    excelFile: ''
  })

  const piyangoKatilimciUserFormData = reactive<Record<string, any>>({})

  const piyangoTalihliFileFormData = reactive<Record<string, any>>({})


  const isPiyangoKatilimciUserUpdate = ref<boolean>(false)
  const refreshPiyangoKatilimciList = ref<boolean>(false)
  const loaded = ref<boolean>(false)
  const katilimciUserPanel = ref<boolean>(false)
  const katilimciFilePanel = ref<boolean>(false)
  const isNewKatilimci = ref<boolean>(false)

  const drawDateControl = computed<boolean>(() => {
    let drawDate = new Date(piyangoStore.lotteryDrawDate)
    let today = new Date()
    drawDate.setHours(0, 0, 0, 0)
    today.setHours(0, 0, 0, 0)
    return today >= drawDate
  })

  const ResetFormData = () => {
    loaded.value = false
    Object.assign(piyangoKatilimciUserFormData, piyangoKatilimciUserSafeFormData)
    piyangoKatilimciValidationStore.userFormChanged = false
    setTimeout(() => {
      loaded.value = true
    })
  }

  const RestoreUserFormData = () => {
    Object.assign(piyangoKatilimciUserFormData, piyangoKatilimciUserSafeFormData)
    delete piyangoKatilimciUserFormData.id
  }

  const RestoreFileFormData = () => {
    Object.assign(piyangoTalihliFileFormData, piyangoKatilimciFileSafeFormData)
  }

  const ResetTalihli = () => {
    loaded.value = false
    piyangoKatilimciUserFormData.ikramiyeId = null
    piyangoKatilimciUserFormData.asilTalihli = false
    piyangoKatilimciUserFormData.yedekTalihli = false
    piyangoKatilimciValidationStore.userFormInvalidTexts.ikramiyeId = ''
    piyangoKatilimciValidationStore.userFormChanged = true
    setTimeout(() => {
      loaded.value = true
    }, 50)
  }

  return {
    piyangoKatilimciUserSafeFormData,
    piyangoKatilimciFileSafeFormData,
    piyangoKatilimciUserFormData,
    piyangoTalihliFileFormData,
    loaded,
    isPiyangoKatilimciUserUpdate,
    refreshPiyangoKatilimciList,
    katilimciUserPanel,
    katilimciFilePanel,
    drawDateControl,
    isNewKatilimci,
    ResetFormData,
    RestoreUserFormData,
    RestoreFileFormData,
    ResetTalihli
  }
})
