package com.zj365.mime.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.arouter.RouteManager
import com.zj365.mime.R
import com.zj365.mime.adapter.FeedBackAdapter
import com.zj365.mime.databinding.ActFeedBackListBinding
import com.zj365.mime.vm.FeedBackVm
import com.xty.network.model.FeedBackBean

/**
 * 意见建议列表
 */
@Route(path = ARouterUrl.FEED_LIST)
class FeedBackListAct:BaseListAct<FeedBackVm>() {
    val binding by lazy{ActFeedBackListBinding.inflate(layoutInflater)}
    val mAdapter by lazy{ FeedBackAdapter() }
    var clickPosition=0
    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() {
        setRecycleRefresh(binding.mRecycle,binding.mRefresh)
        binding.mRecycle.adapter=mAdapter
        binding.mRecycle.layoutManager=LinearLayoutManager(this)
        mAdapter.addChildClickViewIds(R.id.mContent,R.id.mDelete)
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            clickPosition=position
            var bean=adapter.getItem(position) as FeedBackBean
            when(view.id){
                 R.id.mDelete->mViewModel.deleteFeedBack(bean.id)
                R.id.mContent->{
                    bundle.clear()
                    bundle.putSerializable("bean",bean)
                    RouteManager.goAct( ARouterUrl.FEED_BACK_DETAIL,bundle)
                }
            }
        }
    }

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

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

        }
    }

    override fun setLayout()=binding.root

}