import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { useGlobalStore } from '@/stores/globalStore'
import { usePiyangoStore } from './piyangoStore'
import { useUsersStore } from '@/stores/usersStore'
import { usePiyangoTeminatValidationStore } from '../validation/piyangoTeminatValidationStore'

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

  const piyangoTeminatSafeFormData = reactive<Record<string, any>>({
    state: 1,
    amount: null,
    bankName: '',
    bankBranch: '',
    teminatParentType: null,
    teminantChildType: null,
    teminantDate: '',
    teminantNo: null,
    cekilisId: piyangoStore.selectedLottery,
    kisiId: usersStore.userId,
    description: '',
    file: '',
    refundDate: '',
    refundCount: '',
    refundDocumentUrl: '',
    refundDescription:''
  })

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

  const loaded = ref<boolean>(false)
  const isNew = ref<boolean>(false)
  const teminatPanel = ref<boolean>(false)
  const selectedTeminatId = ref<number | null>(null)
  const refreshList = ref<boolean>(false)

  const ResetFormData = () => {
    loaded.value = false
    globalStore.ResetObject(piyangoTeminatFormData, piyangoTeminatSafeFormData)
    piyangoTeminatValidationStore.formChanged = false
    setTimeout(() => {
      loaded.value = true
    }, 30)
  }
  const RestoreFormData = () => {
    Object.assign(piyangoTeminatFormData, piyangoTeminatSafeFormData)
  }
  return {
    piyangoTeminatSafeFormData,
    piyangoTeminatFormData,
    loaded,
    isNew,
    teminatPanel,
    selectedTeminatId,
    refreshList,
    ResetFormData,
    RestoreFormData
  }
})
