package com.zj365.health.act

import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxKeyboardTool
import com.xty.common.arouter.ARouterUrl
import com.xty.base.act.BaseVmAct
import com.xty.common.event.FamilyEvent
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.databinding.ActBindFamilyBinding
import com.zj365.health.vm.BindFamilyVm
import com.xty.network.model.FamilyBean
import org.greenrobot.eventbus.EventBus

/**
 * 绑定家人
 * 修改称呼
 */
@Route(path = ARouterUrl.BIND_FAMILY)
class BindFamilyAct : BaseVmAct<BindFamilyVm>() {
    val binding by lazy { ActBindFamilyBinding.inflate(layoutInflater) }

    var bean: FamilyBean?=null

    override fun setLayout() = binding.root

    override fun initData() {
        super.initData()
        isDarkBar = true
        intent.extras?.let {
            if(it.containsKey("bean")){
                bean=it.getSerializable("bean") as FamilyBean

            }
        }
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
        if(bean!=null){
            binding.mNum.visibility=View.GONE
            binding.mCode.visibility=View.GONE
            binding.mNickName.setText(bean!!.fCall)
            binding.mType.setText(bean!!.type)
            binding.title.mTvTitle.text = getString(R.string.change_nickname)
        }else{
            binding.title.mTvTitle.text ="添加家人"
        }
        binding.llAdd. setOnClickListener {
                RxKeyboardTool.hideSoftInput(this@BindFamilyAct)
                if(bean==null){
                    var nickName = binding.mNickName.text.toString().trim()
                    var typeName = binding.mType.text.toString().trim()
                    var num = binding.mNum.text.toString().trim()
                    var code = binding.mCode.text.toString().trim()
                    mViewModel.bindDev(nickName,num, code,typeName)
                }else{
                    var nickName = binding.mNickName.text.toString().trim()
                    var typeName = binding.mType.text.toString().trim()

                    mViewModel.changeFamilyNickName(bean!!,nickName,typeName)

            }
        }
    }

    override fun liveObserver() {
        mViewModel.bindOk.observe(this){
            CommonToastUtils.showSucceedToast("绑定成功")
            EventBus.getDefault().post(FamilyEvent())
            finish()
        }
        mViewModel.changeOk.observe(this){
            CommonToastUtils.showSucceedToast("修改成功")
            EventBus.getDefault().post(FamilyEvent())
            finish()
        }
    }
}