package com.zj365.health.act.familyhealth

import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.tamsiree.rxkit.RxRegTool
import com.xty.base.act.BaseVmAct
import com.xty.base.vm.BaseVm
import com.xty.common.R
import com.xty.common.arouter.ARouterUrl
import com.xty.common.event.AddSuccessEvent
import com.xty.common.util.CommonToastUtils
import com.zj365.health.databinding.ActAddFamilyGuardianBinding
import com.zj365.health.vm.AddFamilyGuardianVm
import org.greenrobot.eventbus.EventBus

@Route(path = ARouterUrl.ADD_FAMILY_GUARDIAN_ACT)
class AddFamilyGuardianAct : BaseVmAct<AddFamilyGuardianVm>() {
    val binding by lazy { ActAddFamilyGuardianBinding.inflate(layoutInflater) }

    var isFrist = false

    var familyRelation:String? = null
    var familyPhone:String? = null
    var guardian:String = "0"

    override fun liveObserver() {
        mViewModel.submitLiveData.observe(this){
            EventBus.getDefault().post(AddSuccessEvent())
            finish()
        }

    }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "添加监护人"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }

        binding.tvBind.setOnClickListener {
            familyRelation = binding.etName.text.toString()
            familyPhone = binding.etPhone.text.toString()
            if(TextUtils.isEmpty(familyRelation)){
                CommonToastUtils.showToast("请填写监护关系")
                return@setOnClickListener
            }else if (TextUtils.isEmpty(familyPhone)){
                CommonToastUtils.showToast("请填写手机号码")
                return@setOnClickListener
            }else if (!RxRegTool.isMobileSimple(familyPhone)){
                CommonToastUtils.showToast(getString(R.string.please_input_ok_phone))
                return@setOnClickListener
            }

            if(isFrist){
                guardian = "1"
            }else{
                guardian = "0"
            }

            mViewModel.addFamily(familyRelation!!,familyPhone!!,guardian)
        }

        binding.cbFrist.setOnCheckedChangeListener { compoundButton, b ->
            isFrist = b


        }
    }

    override fun initData() {
        super.initData()
    }
}