package com.cheng.blzb.ui.fragment.mine.account

import androidx.lifecycle.MutableLiveData
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.cheng.blzb.net.ApiFactory
import okhttp3.RequestBody

class AccountManageViewModel : BaseViewModel() {
    val accountLiveData = MutableLiveData<List<com.cheng.blzb.bean.AccountEntity>>()
    val loginLiveData = MutableLiveData<com.cheng.blzb.bean.LoginEntity>()

    fun accountList() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.accountList("account")
            if (response.status) {
                accountLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun login(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.login(requestBody)
            if (response.status) {
                loginLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }
}