package com.lukouguoji.gjc.holder

import android.view.View
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ItemIntExpStorageUseBinding
import com.lukouguoji.module_base.adapter.setCommonAdapter
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.GjcMaWb
import com.lukouguoji.module_base.ktx.refresh

/**
 * 国际出港-仓库 ViewHolder
 */
class IntExpStorageUseViewHolder(view: View) :
    BaseViewHolder<GjcMaWb, ItemIntExpStorageUseBinding>(view) {

    override fun onBind(item: Any?, position: Int) {
        val bean = getItemBean(item) ?: return
        binding.bean = bean
        binding.position = position
        binding.executePendingBindings()

        // 图标点击切换选择状态（联动子列表）
        binding.ivIcon.setOnClickListener {
            // 切换主列表项的选择状态
            val newCheckedState = !bean.checked.get()
            bean.checked.set(newCheckedState)

            // 联动勾选/取消所有子列表项
            bean.storageUseList?.forEach { storageUse ->
                storageUse.checked.set(newCheckedState)
            }

            // 刷新UI
            binding.executePendingBindings()
            binding.rvSub.adapter?.notifyDataSetChanged()
        }

        // 展开按钮点击事件
        binding.ivShow.setOnClickListener {
            bean.showMore.set(!bean.showMore.get())
        }

        // 初始化库位明细子列表 RecyclerView
        setCommonAdapter(
            binding.rvSub,
            IntExpStorageUseSubViewHolder::class.java,
            R.layout.item_int_exp_storage_use_sub
        )

        // 刷新库位明细数据（传递父Bean引用）
        val storageUseList = bean.storageUseList ?: emptyList()
        // 为每个子列表项设置父Bean引用（通过tag传递）
        binding.rvSub.tag = bean
        binding.rvSub.refresh(storageUseList)
    }
}
