import { defineStore } from 'pinia'
import { ref, reactive, computed } from 'vue'
import { useUsersStore } from '@/stores/usersStore'

export const useCustomerStore = defineStore('customerStore', () => {
  const usersStore = useUsersStore()

  const customerMail = ref<string>('')
  const selectedCustomer = ref<number>(0)
  const selectedSubCustomer = ref<number>(0)
  const customerApproveId = ref<number | null>(null)
  const subCustomerApproveId = ref<number | null>(null)
  const onayIslemTipleri = ref<Record<string, any>[]>([])
  const uyeOnayRefresh = ref<boolean>(false)
  
  const customerBaseForm = reactive<Record<string, any>>({
    appUserId: usersStore.selectedUserId,
    firstName: '',
    lastName: '',
    email: '',
    phone: ''
  })
  const customerForm = reactive<Record<string, any>>({})
  const customerSafeForm = reactive<Record<string, any>>({})
  const isUpdate = ref<boolean>(false)
  const loaded = ref<boolean>(false)
  const isNew = ref<boolean>(false)

  const GetUnvan = (v: Record<string, any>): string => {
    switch (v.basvuruTipId) {
      case 18: {
        return `${v.gercekAdi} ${v.gercekSoyadi} (${v.gercekTicariUnvan})`
      }
      case 21: {
        return v.tuzelUnvan
      }
      case 22: {
        return v.dernekUnvan
      }
      case 23: {
        return v.sirketUnvan
      }
      case 26: {
        return v.kamuUnvan
      }
      default: {
        return ''
      }
    }
  }

  const CellStatusClass = (d: number): string => {
    switch (d) {
      case 1: {
        return 'back-grad-waiting'
      }
      case 2: {
        return 'back-grad-waiting'
      }
      case 3: {
        return 'back-grad-next'
      }
      case 4: {
        return 'back-grad-ok'
      }
      case 5: {
        return 'back-grad-alert'
      }
      case 6: {
        return 'back-grad-beforeok'
      }
      case 7: {
        return 'back-grad-change'
      }
      default: {
        return 'back-grad-new'
      }
    }
  }

  const CellStatusName = (d: number): string => {
    const tip: Record<string, any> = onayIslemTipleri.value.filter(
      (v: Record<string, any>) => v.id === d
    )

    if (tip[0] !== undefined) return tip[0].tipAdi
    else return 'Yeni Üye'
  }

  const customerApproved = computed<boolean>(() => {
    return customerApproveId.value === 4
  })

  const subCustomerApproved = computed<boolean>(() => {
    return subCustomerApproveId.value === 4
  })

  const ResetForm = () => {
    Object.assign(customerForm, customerBaseForm)
  }
  const SetSafeIrtibatData = () => {
    Object.assign(customerSafeForm, customerForm)
  }
  const RestoreData = () => {
    Object.assign(customerForm, customerSafeForm)
  }
  const CancelSave = () => {
    RestoreData()
    //customerIrtibatValidationStore.formChanged = false
  }

  return {
    customerMail,
    selectedCustomer,
    selectedSubCustomer,
    customerApproveId,
    customerApproved,
    subCustomerApproveId,
    subCustomerApproved,
    onayIslemTipleri,
    uyeOnayRefresh,
    customerBaseForm,
    customerForm,
    customerSafeForm,
    isUpdate,
    loaded,
    isNew,
    GetUnvan,
    CellStatusClass,
    CellStatusName,
    ResetForm,
    SetSafeIrtibatData,
    RestoreData,
    CancelSave
  }
})
