package com.zj365.health.act.familyhealth

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.arouter.ARouterUrl
import com.xty.common.event.AddSuccessEvent
import com.xty.network.model.FamilyGuardianBean
import com.xty.network.model.FamilyHealthBean
import com.zj365.health.R
import com.zj365.health.adapter.familyhealth.FamilyHealthGuardianAdapter
import com.zj365.health.databinding.ActFamilyHealthGuardianBinding
import com.zj365.health.vm.FamilyHealthVm
import org.greenrobot.eventbus.EventBus

@Route(path = ARouterUrl.FAMILY_GUARDIAN_LIST_ACT)
class FamilyHealthGuardianListAct : BaseListAct<FamilyHealthVm>()  {
    val binding by lazy { ActFamilyHealthGuardianBinding.inflate(layoutInflater) }

    val adapter by lazy { FamilyHealthGuardianAdapter() }
    override fun initAdapter() {

        setRecycleRefresh(binding.recyclerView, binding.mRefresh, true)
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        binding.recyclerView.adapter = adapter
        adapter.addChildClickViewIds(R.id.cb_frist, R.id.img_delete)
        adapter.setOnItemChildClickListener { adapter, view, position ->
            val familyGuardianBean = adapter.data[position] as FamilyGuardianBean
            when (view.id) {
                R.id.img_delete -> {
                    mViewModel.deleteGuardian(familyGuardianBean.id)
                 //   mViewModel.exitCustody(familyHealthBean.id.toString())
                }
                R.id.cb_frist->{
                    mViewModel.setFirstGuardian(familyGuardianBean.id)
                }
            }
        }
    }


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

    override fun loadData() {
        mViewModel.getFamilyHealthGuardianList()
    }

    override fun liveObserver() {
        mViewModel.familyGuardianLiveData.observe(this){
            adapter.setNewInstance(it.data)
        }

        mViewModel.deleteGuardianLiveData.observe(this){
            page =1
            loadData()
            EventBus.getDefault().post(AddSuccessEvent())
        }

        mViewModel.firstGuardianLiveData.observe(this){
            page =1
            loadData()
        }
    }

    override fun setLayout()= binding.root
}