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.model.User
import com.quansu.heifengwuliu.utils.net.NetEngine
import com.ysnows.base.base.BRepository
import com.ysnows.base.base.BViewModel

open class PasswordVModel : BViewModel<BRepository>() {


    var isShow: MutableLiveData<Boolean> = MutableLiveData( User.get()!!.isPwd)

    var type: MutableLiveData<Int> = MutableLiveData(1)



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


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

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


    fun toSubmit(){
        if(type.value==1){//设置登录密码

            var oldpwd=""
            if(isShow.value!!) {
                if (TextUtils.isEmpty(oldpassWord)) {
                    toast("请输入旧密码")
                    return
                }
                oldpwd= oldpassWord.toString()
            }
            if(TextUtils.isEmpty(passWord)){
                toast("请输入新密码")
                return
            }

            if(TextUtils.isEmpty(passWordTo)){
                toast("请再次输入新密码")
                return
            }
            if(passWord!=passWordTo){
                toast("两次新密码不一致！")
                return
            }

            repository().lreq(NetEngine.service.setPwd(oldpwd, passWord,"0"))
                    .doOnNext {
                        if (it.ok(true)) {
                            toast(it.msg())
                            (repository().context as Activity).finish()
                        }
                    }
                    .subscribe()

            return
        }


    }


    fun toPaySubmit(oldPassword:String,newPassword:String){
        if(TextUtils.isEmpty(oldPassword)){
            toast("请输入旧支付密码")
            return
        }

        if(TextUtils.isEmpty(newPassword)){
            toast("请输入新支付密码")
            return
        }

        repository().lreq(NetEngine.service.setPwd(oldPassword, newPassword,"1"))
                .doOnNext {
                    if (it.ok(true)) {
                        toast(it.msg())
                        (repository().context as Activity).finish()
                    }
                }
                .subscribe()

    }



}
