package com.zswc.creativity.vmodel

import android.app.Activity
import android.app.Application
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.zswc.creativity.activity.LoginActivity
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.UiSwitch
import com.zswc.creativity.BR
import com.zswc.creativity.R
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.model.User
import com.zswc.creativity.utils.net.NetEngine
import kotlinx.coroutines.launch

class CertificationVModel(application: Application) : BViewModel<BRepository>(application)  {

    var phone = MutableLiveData<String>()
    var realPhone=MutableLiveData<String>()

    @Bindable
    open var name: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.name)
        }

    @Bindable
    open var card: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.card)
        }

    fun toSure(){
        if (name.isNullOrEmpty()) {
            toast("请输入姓名")
            return
        }

        if (card.isNullOrEmpty()) {
            toast("请输入身份证号")
            return
        }

        viewModelScope.launch {

            val it = repository().lreq { NetEngine.service.setUserCertification(name, card,realPhone.value) }
            if (it.ok(true)) {
                //保存数据
//                var user = User.get()
//                user!!.is_auth = "1"
//                user!!.true_name = name
//                // user!!.snap_card = card
//                user.save()

               //刷新个人信息
                RxBus.get().post(ConfigRx.REFRESH_USER, "")

                //发送消息--

                var con = repository().context as Activity
                con.finish()
            }
        }


    }



}
