package com.lukouguoji.gjj.holder

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.gjj.databinding.ItemIntImpStorageUseSubBinding
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.GjcMaWb
import com.lukouguoji.module_base.bean.GjcStorageUse

/**
 * 国际进港-仓库 库位明细行 ViewHolder
 */
class IntImpStorageUseSubViewHolder(view: View) :
    BaseViewHolder<GjcStorageUse, ItemIntImpStorageUseSubBinding>(view) {

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

        // 单选框点击切换选择状态（反向联动主列表）
        binding.ivCheckbox.setOnClickListener {
            // 切换子列表项的选择状态
            val newCheckedState = !bean.checked.get()
            bean.checked.set(newCheckedState)
            binding.executePendingBindings()

            // 反向联动主列表项（仅在勾选时联动）
            updateParentCheckState(newCheckedState)
        }
    }

    /**
     * 更新父列表项的选择状态
     */
    private fun updateParentCheckState(newCheckedState: Boolean) {
        val recyclerView = itemView.parent as? RecyclerView ?: return
        val parentBean = recyclerView.tag as? GjcMaWb ?: return

        if (newCheckedState) {
            parentBean.checked.set(true)
        }
    }
}
