package com.lukouguoji.gjc.holder

import android.graphics.Color
import android.view.View
import com.lukouguoji.gjc.activity.GjcInspectionDetailsActivity
import com.lukouguoji.gjc.databinding.ItemGjcInspectionBinding
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.bean.GjcInspectionBean
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * 国际出港收运检查列表 ViewHolder
 */
class GjcInspectionViewHolder(view: View) :
    BaseViewHolder<GjcInspectionBean, ItemGjcInspectionBinding>(view) {

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

        // 点击checkbox切换选中状态
        binding.ivIcon.setOnClickListener {
            bean.checked.set(!bean.checked.get())
            // 发送checkbox状态变化事件
            CoroutineScope(Dispatchers.Main).launch {
                FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).emit("check_changed")
            }
        }

        // 整行点击跳转到详情页
        binding.ll.setOnClickListener {
            GjcInspectionDetailsActivity.start(it.context, bean.maWbId)
        }

        // 设置审核状态文本和颜色
        binding.tvStatus.text = bean.getReviewStatusName()
        binding.tvStatus.setTextColor(Color.parseColor(bean.getReviewStatusColor()))
    }
}
