package com.zj365.health.act

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.recycleUtils.RecycleItem
import com.zj365.health.adapter.MedicalHistoryAdapter
import com.zj365.health.databinding.ActDataListBinding
import com.zj365.health.vm.ElectronicListVm

/**
 * 用药历史数据
 */
@Route(path = ARouterUrl.MEDIC_LIST)
class MedicHistoryListAct : BaseListAct<ElectronicListVm>() {
    val binding by lazy{ ActDataListBinding.inflate(layoutInflater)}
    val mAdapter by lazy{ MedicalHistoryAdapter() }


    var deletePosition=-1
    override fun setLayout()=binding.root
    override fun initData() {
        super.initData()
        isDarkBar=true
    }

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mIvBack.setOnClickListener { finish() }
        binding.title.mTvTitle.text="用药"

    }
    override fun initAdapter() {
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.addItemDecoration(RecycleItem(this,50))
        binding.mRecycle.layoutManager= LinearLayoutManager(this)
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
    }

    override fun loadData() {
        mViewModel.getMedicList(page)
    }

    override fun liveObserver() {
        mViewModel.medicalLive.observe(this){
               setDate(mAdapter,it.data)
        }
        mViewModel.deleteLive.observe(this){
            if(mAdapter.data.size==1){
                setDate(mAdapter, mutableListOf())
            }else{
                mAdapter.data.removeAt(deletePosition)
                mAdapter.notifyItemRemoved(deletePosition)
            }
        }
    }

}