package com.grkj.iscs_mc.view.fragment

import android.view.View
import com.grkj.iscs_mc.databinding.FragmentMaterialHomeBinding
import com.grkj.iscs_mc.extentions.navigateTo
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.iview.IMaterialHomeView
import com.grkj.iscs_mc.view.presenter.MaterialHomePresenter
import com.grkj.iscs_mc.view.widget.ExpandableTabLayout

/**
 * 物资管理 - 首页
 */
class MaterialHomeFragment :
    BaseMvpFragment<IMaterialHomeView, MaterialHomePresenter, FragmentMaterialHomeBinding>() {

    override val viewBinding: FragmentMaterialHomeBinding
        get() = FragmentMaterialHomeBinding.inflate(layoutInflater)

    override fun initView() {
        mBinding?.rlModuleFetchReturn?.setOnClickListener {
            navigateTo(MaterialHomeFragmentDirections.actionHomeFragmentToFetchReturnFragment())
        }

        mBinding?.rlModuleInstruction?.setOnClickListener {
            navigateTo(MaterialHomeFragmentDirections.actionHomeFragmentToInstructionFragment())
        }

        mBinding?.rlModuleInspection?.setOnClickListener {
            navigateTo(MaterialHomeFragmentDirections.actionHomeFragmentToInspectionPlanFragment())
        }

        mBinding?.rlModuleReplacement?.setOnClickListener {
            navigateTo(MaterialHomeFragmentDirections.actionHomeFragmentToReplacementFragment())
        }

        presenter?.getHomeData { itData ->
            itData?.rarNumber?.let {
                mBinding?.tvFetchReturnNum?.text = it.toString()
                mBinding?.rlFetchReturnNum?.visibility = View.VISIBLE
            } ?: let {
                mBinding?.rlFetchReturnNum?.visibility = View.GONE
            }

            itData?.checkNumber?.let {
                mBinding?.tvInspectionNum?.text = it.toString()
                mBinding?.rlInspectionNum?.visibility = View.VISIBLE
            } ?: let {
                mBinding?.rlInspectionNum?.visibility = View.GONE
            }

            itData?.changeNumber?.let {
                mBinding?.tvReplacementNum?.text = it.toString()
                mBinding?.rlReplacementNum?.visibility = View.VISIBLE
            } ?: let {
                mBinding?.rlReplacementNum?.visibility = View.GONE
            }

            val list = mutableListOf<ExpandableTabLayout.ItemData>()
            itData?.list?.forEach {
                list.add(ExpandableTabLayout.ItemData("(${it.borrowableNumber}/${it.allNumber})", iconUrl = it.materialsTypeIcon))
            }
            mBinding?.etlType?.setData(list)
        }
    }

    override fun initPresenter(): MaterialHomePresenter {
        return MaterialHomePresenter()
    }
}