package com.dlc.eboard.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.dlc.eboard.LabApp
import com.dlc.eboard.adapter.BlankAdapter
import com.dlc.eboard.databinding.FragmentBlankBinding
import com.rc.core.ui.fragment.RcBaseFragment
import com.rc.httpcore.client.ApiRepository


class BlankFragment : RcBaseFragment<FragmentBlankBinding>() {
    private val mAdapter by lazy { BlankAdapter(requireActivity()) }

    companion object {
        const val TAG = "BlankFragment"
        fun newInstance(gasName: String,remark:String?,flg:String?): BlankFragment {
            val args = Bundle()
            args.putString("gasName", gasName)
            args.putString("remark", remark)
            args.putString("flg", flg)
            val fragment = BlankFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun createViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentBlankBinding {
        return FragmentBlankBinding.inflate(inflater, container, false)
    }

    @SuppressLint("WrongConstant")
    override fun initViews(savedInstanceState: Bundle?) {
        val smaller = requireArguments().getString("gasName")
        val remark = requireArguments().getString("remark")
        val flg = requireArguments().getString("flg")

        val recyclerView = viewBinding.rvViews
        val layoutManager = GridLayoutManager(requireActivity(), 4) //第二个参数为网格的列数
        recyclerView.layoutManager = layoutManager
        recyclerView.isNestedScrollingEnabled = false //防止滑动冲突
        recyclerView.adapter = mAdapter
        getListByName(smaller!!,remark,flg)
    }

    private fun getListByName(gasName: String,remark:String?,flg:String?) {
        val disposable = ApiRepository.getListByName(LabApp.sLabConfig!!.labId, gasName,remark,flg)
            .subscribe({ response ->
                mAdapter.setNewInstance(response.toMutableList())
            }, { throwable ->
                throwable.printStackTrace()
//                showNetError(throwable)
            })
        addDisposable(disposable)
    }
    // 这里是一个示updateData方法
    fun updateData(gasName:String,remark:String?,flg:String?) {
        getListByName(gasName,remark,flg)
    }


}