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

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.holds.HoldViewModel

/**
 * 成交单
 */
class DealFragment : BaseFragment<DealViewModel>() {

    companion object {
        fun getInstance() = DealFragment()
    }

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


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _view = inflater.inflate(R.layout.deal_fragment, container, false)
        initViews()
        viewModel.dealLiveData.bindOptional(context!!) { data ->
            (idDealList?.adapter as DealViewModel.HoldAdapter).update(data?.toArrayList())
        }
        return _view
    }

    private fun initViews() {
        idDealList = _view?.findViewById(R.id.id_deal_list)

        val layoutManager = LinearLayoutManager(context)
        idDealList?.layoutManager = layoutManager
        idDealList?.adapter = viewModel.getAdapter()
    }

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