package com.lukouguoji.gjc.holder

import android.view.View
import com.lukouguoji.gjc.databinding.ItemIntExpMoveBinding
import com.lukouguoji.gjc.page.move.IntExpMoveDetailActivity
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.GjcMove
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 国际出港移库列表项ViewHolder
 */
class IntExpMoveViewHolder(view: View) :
    BaseViewHolder<GjcMove, ItemIntExpMoveBinding>(view) {

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

        // 手机版「查看详情」→ 运单详情页（平板变体无此控件，为 null 时跳过）
        binding.llDetail?.setOnClickListener {
            IntExpMoveDetailActivity.start(itemView.context, bean)
        }

        // 添加图标点击事件 - 切换选择状态
        binding.ivIcon.setOnClickListener {
            bean.checked.set(!bean.checked.get())  // 反转选择状态
            binding.executePendingBindings()        // 立即刷新UI

            // 发送事件通知Activity更新全选状态
            GlobalScope.launch {
                FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).emit("check_changed")
            }
        }

        binding.executePendingBindings()
    }
}
