package com.proyek.akhir.kodel.ui.user.dashboard.history

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.proyek.akhir.kodel.data.response.Transaksi
import com.proyek.akhir.kodel.utils.ApiClient
import retrofit2.Call
import retrofit2.Response

class HistoryViewModel : ViewModel() {
    private val anggotaHistory = MutableLiveData<Transaksi>()

    init {
        getAnggotaTransaksi()
    }

    private fun getAnggotaTransaksi() {
        ApiClient().getServiceKode().getAllTransaksi()
            .enqueue(object : retrofit2.Callback<Transaksi> {
                override fun onFailure(call: Call<Transaksi>, t: Throwable) {
                    anggotaHistory.value = null
                }

                override fun onResponse(call: Call<Transaksi>, response: Response<Transaksi>) {
                    if (response.isSuccessful) {
                        anggotaHistory.value = response.body()
                    } else {
                        anggotaHistory.value = null
                    }
                }
            })
    }


    fun setDataTransaksi(): MutableLiveData<Transaksi> {
        return anggotaHistory
    }
}