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.R
import com.zj365.health.adapter.ElectronicListAdapter
import com.zj365.health.databinding.ActDataListBinding
import com.zj365.health.vm.ElectronicListVm
import com.xty.network.model.EleTestBean

/**
 * 电子化验单
 */
@Route(path = ARouterUrl.ELECT_LIST)
class ElectronicListAct:BaseListAct<ElectronicListVm>() {
    val binding by lazy{ActDataListBinding.inflate(layoutInflater)}
    val mAdapter by lazy{ ElectronicListAdapter() }
    var clickPosition=0

    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=getString(R.string.ele_test)

    }
    override fun initAdapter() {
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.addItemDecoration(RecycleItem(this,50))
        binding.mRecycle.layoutManager=LinearLayoutManager(this)
        setRecycleRefresh(binding.mRecycle,binding.mRefresh,true)
        mAdapter.addChildClickViewIds(R.id.mDelete)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            clickPosition=position
            var bean=adapter.getItem(position) as EleTestBean
            mViewModel.deleteEle(bean.id)
        }

    }

    override fun loadData() {
        mViewModel.getList(page)

    }

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

    }

}