package cn.muchinfo.rma.view.base.future.costs

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.muchinfo.rma.R
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.toArrayList
import cn.muchinfo.rma.view.base.BaseFragment
import cn.muchinfo.rma.view.base.future.deals.DealViewModel

/**
 * 资金流水
 */
class CostListFragment(type: String?) : BaseFragment<CostViewModel>() {

    private var type: String? = type

    companion object {
        fun getInstance(type: String?) = CostListFragment(type)
    }

    private var _view: View? = null
    private var idCostList: RecyclerView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _view = inflater.inflate(R.layout.cost_list, container, false)
        initViews()
        return _view
    }

    private fun initViews() {
        idCostList = _view?.findViewById(R.id.id_cost_list)
         val adapter = viewModel.getCostListAdapter()
        val layoutManager = LinearLayoutManager(context)
        idCostList?.layoutManager = layoutManager
        viewModel.costList.bindOptional(context!!) { data ->
            (idCostList?.adapter as CostViewModel.CostListAdapter).update(data?.toArrayList())
        }
        idCostList?.adapter = adapter
    }

    override fun onResume() {
        super.onResume()
        viewModel.queryAmountLog()
    }
    //主动更新数据
    fun onRefresh(){
        viewModel.queryAmountLog()
    }
}