import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { useUsersStore } from '@/stores/usersStore'
import { useCustomerIrtibatValidationStore } from '../validation/customerIrtibatValidationStore'

export const useCustomerIrtibatStore = defineStore('customerIrtibatStore', () => {
  const usersStore = useUsersStore()
  const customerIrtibatValidationStore = useCustomerIrtibatValidationStore()

  const customerAllIrtibatList = ref<Record<string, any>[]>([])

  const customerIrtibatBaseForm = reactive<Record<string, any>>({
    appUserId: usersStore.selectedUserId(),
    firstName: '',
    lastName: '',
    email: '',
    phone: ''
  })

  const customerIrtibatForm = reactive<Record<string, any>>({})
  const customerIrtibatSafeForm = reactive<Record<string, any>>({})
  const isUpdate = ref<boolean>(false)
  const refreshList = ref<boolean>(false)
  const irtibatPanel = ref<boolean>(false)
  const loaded = ref<boolean>(false)
  const totalIrtibat = ref<number>(0)
  const isNew = ref<boolean>(false)

  const ResetForm = () => {
    Object.assign(customerIrtibatForm, customerIrtibatBaseForm)
    customerIrtibatForm.appUserId = usersStore.selectedUserId()
    delete customerIrtibatForm.id
  }
  const SetSafeIrtibatData = () => {
    Object.assign(customerIrtibatSafeForm, customerIrtibatForm)
  }
  const RestoreData = () => {
    Object.assign(customerIrtibatForm, customerIrtibatSafeForm)
  }
  const CancelSave = () => {
    RestoreData()
    customerIrtibatValidationStore.formChanged = false
  }
  const ResetData = () => {
    isNew.value = false
    isUpdate.value = false
    loaded.value = false
    Object.assign(customerIrtibatForm, {})
  }
  return {
    customerAllIrtibatList,
    customerIrtibatBaseForm,
    customerIrtibatForm,
    customerIrtibatSafeForm,
    isUpdate,
    refreshList,
    irtibatPanel,
    loaded,
    totalIrtibat,
    isNew,
    ResetForm,
    SetSafeIrtibatData,
    RestoreData,
    CancelSave,
    ResetData
  }
})
