package com.quansu.heifengwuliu.vmodel

import android.app.Activity
import android.text.TextUtils
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import com.quansu.heifengwuliu.BR
import com.quansu.heifengwuliu.activity.HomeActivity
import com.quansu.heifengwuliu.model.LoginBean
import com.quansu.heifengwuliu.model.User
import com.quansu.heifengwuliu.repository.PersonalVerifyRepository
import com.quansu.heifengwuliu.utils.ChosePhotoUtils
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.tencent.mmkv.MMKV
import com.ysnows.base.base.BViewModel
import com.ysnows.base.utils.UiSwitch

class PersonalVerityVModel : BViewModel<PersonalVerifyRepository>() {

    //https://cdnheifengwuliu.qs110.com/idcard/xuzgomb3/android/v/mmexport1599207639412.jpg
    var imgIDMan: MutableLiveData<String> = MutableLiveData("")
    var imgIDEmblem: MutableLiveData<String> = MutableLiveData("")
    var type:String="1"//1:身份证正面  2:身份证背面

    var isShow: MutableLiveData<Boolean> = MutableLiveData(true)


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

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


    fun choseImg(type:Int){
        if(!isShow.value!!){
            return
        }
        //选择照片
        this.type=type.toString()
        ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

    }

    fun  toSubmit(){

        if(TextUtils.isEmpty(imgIDMan.value)){
            toast("请上传省份证人像面图片")
            return
        }

        if(TextUtils.isEmpty(imgIDEmblem.value)){
            toast("请上传国徽面图片")
            return
        }
        if(TextUtils.isEmpty(name)){
            toast("请输入姓名")
            return
        }

        if(TextUtils.isEmpty(iDNumber)){
            toast("请输入身份证号")
            return
        }

        repository().lreq(NetEngine.service.setInfo(imgIDMan.value, imgIDEmblem.value,name,iDNumber))
                .doOnNext {
                    if (it.ok(true)) {
                        (repository().context as Activity).finish()
                    }
                }
                .subscribe()

    }


    fun getInfo(){
        //获取个人信息
        repository().lreq(NetEngine.service.getInfo("1"))
                .doOnNext {
                    if (it.ok(true)) {
                        var bean=it.data()
                        if(null!=bean){
                            isShow.value = bean.status != "1"
                            //设置值
                            imgIDMan.value=bean.idcard_z
                            imgIDEmblem.value=bean.idcard_f
                            name=bean.name
                            iDNumber=bean.idcard

                        }

                    }
                }
                .subscribe()

    }


}
