package com.xhly.manageapp.ui.login.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.xhly.corelib.utils.LogShow
import com.xhly.manageapp.bean.login.LoginBean
import com.xhly.manageapp.bean.user.UserBean
import com.xhly.manageapp.viewmodel.BaseViewModel

class LoginViewModel: BaseViewModel() {

    private val loginResult=MutableLiveData<UserBean>()
    val loginData:LiveData<UserBean> =loginResult
    private val regionCodeResult=MutableLiveData<Boolean>()
    val regionCodeData:LiveData<Boolean> =regionCodeResult

    fun login(loginBean: LoginBean){
        launchUI {
          try {
              val postLogin = loginService.postLogin(loginBean)
              if (postLogin.code==0){
                  if (postLogin.obj!=null){
                      loginResult.value=postLogin.obj
                  }
              }else{
                  val userBean=UserBean()
                  userBean.xhlyLoginSucess=false
                  if (postLogin.msg!=null){
                      userBean.xhlyLoginMessage=postLogin.msg
                  }
                  loginResult.value=userBean
              }
          }catch (e:Exception){
              val userBean=UserBean()
              userBean.xhlyLoginSucess=false
              loginResult.value=userBean
              LogShow(e.toString())
          }
        }
    }
  fun postRegionCheckCode(regionCode:String){
      launchUI {
          val map=HashMap<String,Any>()
          map["regionCode"]=regionCode
          val result = loginService.postRegionCheckCode(map)
          regionCodeResult.value = result.code==0&& result.obj
          if (!result.msg.isNullOrEmpty()){
              netMessage.value=result.msg
          }
      }
  }
}