package com.grkj.iscs_mc.view.fragment

import com.bumptech.glide.Glide
import com.grkj.iscs_mc.R
import com.grkj.iscs_mc.databinding.FragmentMaterialFetchReturnBinding
import com.grkj.iscs_mc.extentions.navigateUp
import com.grkj.iscs_mc.model.vo.material.MaterialListRespVO
import com.grkj.iscs_mc.view.base.BaseMvpFragment
import com.grkj.iscs_mc.view.iview.IMaterialFetchReturnView
import com.grkj.iscs_mc.view.presenter.MaterialFetchReturnPresenter
import com.grkj.iscs_mc.view.widget.ExpandableTabLayout
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * 物资管理 - 物资取还
 */
class MaterialFetchReturnFragment :
    BaseMvpFragment<IMaterialFetchReturnView, MaterialFetchReturnPresenter, FragmentMaterialFetchReturnBinding>() {

    private var mMaterialList: MutableList<MaterialListRespVO>? = null
    private var mDetailList = mutableListOf<MaterialListRespVO.MaterialsPageVO>()

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

    override fun initView() {
        mBinding?.cbBack?.setOnClickListener { navigateUp() }

        mBinding?.rvFetchReturn?.adapter = object :
            CommonAdapter<MaterialListRespVO.MaterialsPageVO>(requireContext(), R.layout.item_rv_replacement, mDetailList) {
            override fun convert(holder: ViewHolder, vo: MaterialListRespVO.MaterialsPageVO, position: Int) {
                holder.setText(R.id.tv_name, vo.materialsName ?: "")
                holder.setText(R.id.tv_rfid, vo.materialsRfid)
                holder.setVisible(R.id.rv_tip, vo.loanState == "0")
                holder.setVisible(R.id.iv_tip, false)
                holder.setBackgroundColor(R.id.ll_tip, mContext.getColor(R.color.common_btn_red_bg))
                holder.setText(R.id.tv_tip, getString(R.string.to_be_returned))
                Glide.with(this@MaterialFetchReturnFragment).load(vo.materialsTypePicture).into(holder.getView(R.id.iv_icon))
            }
        }

        mBinding?.etlType?.setOnSelectListener(object : ExpandableTabLayout.OnSelectListener {
            override fun onSelect(position: Int) {
                mDetailList.clear()
                mMaterialList?.get(position)?.materials?.let {
                    mDetailList.addAll(it)
                }
                mBinding?.rvFetchReturn?.adapter?.notifyDataSetChanged()
            }
        })

        presenter?.getMaterialList {
            mMaterialList = it
            val list = mutableListOf<ExpandableTabLayout.ItemData>()
            it?.forEach { itData ->
                // TODO icon不对
                list.add(ExpandableTabLayout.ItemData(itData.materialsTypeName, R.mipmap.safety_helmet))
            }
            mBinding?.etlType?.setData(list)

            mDetailList.clear()
            if (mMaterialList?.isNotEmpty() == true) {
                mMaterialList?.get(0)?.materials?.let {
                    mDetailList.addAll(it)
                }
            }
            mBinding?.rvFetchReturn?.adapter?.notifyDataSetChanged()
        }
    }

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