import { defineStore } from 'pinia'
import { useDataStore } from '@/stores/dataStore'
import { useUsersStore } from '@/stores/usersStore'
import { usePiyangoStore } from '../stores/piyangoStore'
import { usePiyangoTeminatStore } from '../stores/piyangoTeminatStore'
import { usePiyangoTeminatValidationStore } from '../validation/piyangoTeminatValidationStore'
import { useGlobalStore } from '@/stores/globalStore'

export const usePiyangoTeminatService = defineStore('piyangoTeminatService', () => {
  const dataStore = useDataStore()
  const usersStore = useUsersStore()
  const piyangoStore = usePiyangoStore()
  const piyangoTeminatStore = usePiyangoTeminatStore()
  const piyangoTeminatValidationStore = usePiyangoTeminatValidationStore()
  const globalStore = useGlobalStore()

  const SaveTeminatDurum = async () => {
    if (piyangoTeminatValidationStore.FormCheck()) {
      let form: any
      let dataForm = new FormData()

      dataForm.append('state', piyangoTeminatStore.piyangoTeminatFormData.state)
      dataForm.append(
        'amount',
        globalStore.floatEnLocale(piyangoTeminatStore.piyangoTeminatFormData.amount)
      )
      dataForm.append('bankName', piyangoTeminatStore.piyangoTeminatFormData.bankName)
      dataForm.append('bankBranch', piyangoTeminatStore.piyangoTeminatFormData.bankBranch)
      dataForm.append(
        'teminatParentType',
        piyangoTeminatStore.piyangoTeminatFormData.teminatParentType
      )
      dataForm.append(
        'teminantChildType',
        piyangoTeminatStore.piyangoTeminatFormData.teminantChildType
      )
      dataForm.append(
        'teminantDate',
        piyangoTeminatStore.piyangoTeminatFormData.teminantDate
      )
      dataForm.append('teminantNo', piyangoTeminatStore.piyangoTeminatFormData.teminantNo)
      dataForm.append('cekilisId', String(piyangoStore.selectedLottery))
      dataForm.append('kisiId', String(usersStore.userId))
      dataForm.append('file', piyangoTeminatStore.piyangoTeminatFormData.file)
      dataForm.append(
        'description',
        piyangoTeminatStore.piyangoTeminatFormData.description
      )

      if (usersStore.isPanelUser) {
        if (piyangoTeminatStore.piyangoTeminatFormData.state === 1) {
          piyangoTeminatStore.piyangoTeminatFormData.refundDate = ''
          piyangoTeminatStore.piyangoTeminatFormData.refundCount = ''
          piyangoTeminatStore.piyangoTeminatFormData.refundDocumentUrl = ''
          piyangoTeminatStore.piyangoTeminatFormData.refundDescription = ''
        }
        dataForm.append(
          'refundDate',
          String(piyangoTeminatStore.piyangoTeminatFormData.refundDate)
        )
        dataForm.append(
          'refundCount',
          String(piyangoTeminatStore.piyangoTeminatFormData.refundCount)
        )
        dataForm.append(
          'refundDocumentUrl',
          piyangoTeminatStore.piyangoTeminatFormData.refundDocumentUrl
        )
        dataForm.append(
          'refundDescription',
          piyangoTeminatStore.piyangoTeminatFormData.refundDescription
        )
      }

      if (piyangoTeminatStore.isNew) {
        form = await dataStore.dataPost('TeminantStates/', {
          data: dataForm,
          headers: { 'Content-Type': 'multipart/form-data' },
          toast: { toast: 'Teminat durumu başarıyla kaydedildi', type: 'success' }
        })
      } else {
        form = await dataStore.dataPut(
          'TeminantStates/' + piyangoTeminatStore.piyangoTeminatFormData.id,
          {
            data: dataForm,
            headers: { 'Content-Type': 'multipart/form-data' },
            toast: { toast: 'Teminat durumu başarıyla güncellendi', type: 'success' }
          }
        )
      }

      if (form !== 'errorfalse') {
        piyangoTeminatStore.isNew = false
        piyangoTeminatValidationStore.formChanged = false
        piyangoTeminatStore.refreshList = true
        piyangoTeminatStore.teminatPanel = false
      }
    } else {
      piyangoTeminatValidationStore.isFormValid = true
    }
  }

  const GetTeminatDurum = async () => {
    piyangoTeminatStore.loaded = false

    let form = await dataStore.dataGet(
      'TeminantStates/' + piyangoTeminatStore.selectedTeminatId
    )

    if (form !== 'errorfalse') {
      piyangoTeminatStore.isNew = false
      await Object.assign(piyangoTeminatStore.piyangoTeminatFormData, form)
    } else {
      await piyangoTeminatStore.ResetFormData()
      await piyangoTeminatStore.RestoreFormData()
      piyangoTeminatStore.isNew = true
    }
    setTimeout(() => {
      piyangoTeminatStore.loaded = true
    }, 30)
  }

  return { SaveTeminatDurum, GetTeminatDurum }
})
