package com.example.chemical.ui.inquiry


import android.os.Bundle
import android.view.LayoutInflater
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.chemical.ChemicalApp
import com.example.chemical.databinding.ActivityQueryDetailsBinding
import com.example.chemical.ui.adapter.CabinetDoorAdapter
import com.example.chemical.ui.adapter.CabinetDoorRighAdapter
import com.example.chemical.ui.common.BaseCountDownActivity
import com.rc.httpcore.client.ApiRepository

//查询 详情
class QueryDetailsActivity : BaseCountDownActivity<ActivityQueryDetailsBinding>() {

    val mutableMap = mutableMapOf<String, String>()
    private val mAdapterLeft by lazy { CabinetDoorAdapter(this) }  //左边柜子
    private val mAdapterRigh by lazy { CabinetDoorRighAdapter(this) }  //右边柜子

    override fun createViewBinding() =
        ActivityQueryDetailsBinding.inflate(LayoutInflater.from(this))

    override fun initViews(savedInstanceState: Bundle?) {
        super.initViews(savedInstanceState)
        inAdapter()
        viewBinding.tvReturn.text = "返回${ChemicalApp.confs!!.backTime}s"

    }

    override fun initData() {
        super.initData()
        val stockId = intent.getStringExtra("stockId")
        if (stockId != null) {
            getList(stockId)
        }
    }

    private fun getList(stockId: String) {
//        showLoading("查询中...")
//        mutableMap["stockId"] = stockId
//        val disposable = ApiRepository.indexDetailsList(mutableMap)
//            .subscribe({ data ->
//                dismissLoading()
//            }, { throwable ->
//                dismissLoading()
//                showNetError(throwable)
//
//            })
//        addDisposable(disposable)
    }


    private fun inAdapter() {
        viewBinding.recyclerViewListLeft?.layoutManager = LinearLayoutManager(this)
        viewBinding.recyclerViewListLeft?.adapter = mAdapterLeft
        mAdapterLeft.setOnItemClickListener { adapter, view, position ->
            mAdapterLeft.data.forEach { it.isType = false }
            mAdapterLeft.data[position].isType = true
            mAdapterRigh.data.forEach { it.isType = false }  //柜门初始化
            mAdapterLeft.notifyDataSetChanged()
            if (mAdapterLeft.data[position].cabinetDoorVoList != null) {
                mAdapterLeft.data[position].isType=true
                mAdapterRigh.setNewInstance(mAdapterLeft.data[position].cabinetDoorVoList.toMutableList())
            }
        }
        viewBinding.recyclerViewListRight?.layoutManager = LinearLayoutManager(this)
        viewBinding.recyclerViewListRight?.adapter = mAdapterRigh
        getCabinet(ChemicalApp.subjectId!!)
    }

    //查询存储位置  默认第一个  柜子信息
    private fun getCabinet(subId: String) {
        showLoading("查询中...")
        val disposable = ApiRepository.getCabinetList(subId)
            .subscribe({ data ->
                dismissLoading()
//                mListData = data
                mAdapterLeft.setNewInstance(data.toMutableList())
                mAdapterRigh.setNewInstance(data[0].cabinetDoorVoList.toMutableList())

            }, { throwable ->
                dismissLoading()
                showNetError(throwable)

            })
        addDisposable(disposable)
    }
}