package com.lukouguoji.gjj.holder

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.lukouguoji.gjj.databinding.ItemIntImpTallySubBinding
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.GjjImportTally

/**
 * 国际进港理货报告 分单子列表 ViewHolder
 */
class IntImpTallySubViewHolder(view: View) :
    BaseViewHolder<GjjImportTally, ItemIntImpTallySubBinding>(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? GjjImportTally ?: return

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