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.repository.ComVerifyRepository
import com.quansu.heifengwuliu.utils.ChosePhotoUtils
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.ysnows.base.base.BViewModel

class ComVerityVModel : BViewModel<ComVerifyRepository>() {

    var imgBL: MutableLiveData<String> = MutableLiveData("")
    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(){
        //选择照片
        ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)

    }

    fun  toSubmit(){

        if(TextUtils.isEmpty(imgBL.value)){
            toast("请上传营业执照照片")
            return
        }
        if(TextUtils.isEmpty(name)){
            toast("请输入公司名称")
            return
        }

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

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

    fun getInfo(){
        //获取个人信息
        repository().lreq(NetEngine.service.getInfo("3"))
                .doOnNext {
                    if (it.ok(true)) {
                        var bean=it.data()
                        if(null!=bean){
                            isShow.value = bean.status != "1"
                            //设置值
                            imgBL.value=bean.license_img
                            name=bean.company_name
                            iDNumber=bean.license_sn

                        }

                    }
                }
                .subscribe()

    }



}
