package com.lukouguoji.gjc.dialog

import android.content.Context
import android.view.View
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.DialogBoxAddSelectBinding
import com.lukouguoji.gjc.databinding.DialogGjcUldSelectBinding
import com.lukouguoji.gjc.holder.GjcUnLoadDialogViewHolder
import com.lukouguoji.gjc.holder.UldViewHolder
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.GjcWaybillBean
import com.lukouguoji.module_base.bean.ULDBean
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.refresh
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.util.CheckUtil
import com.lxj.xpopup.XPopup
import dev.utils.app.ScreenUtils

class GjcUldSelectDialogModel(
    val list: List<ULDBean>,
    private val callBack: (ULDBean) -> Unit
) : BaseDialogModel<DialogGjcUldSelectBinding>(DIALOG_TYPE_CENTER) {

    val itemId = R.layout.item_uld
    val itemHolder = UldViewHolder::class.java

    override fun onBuild(builder: XPopup.Builder) {
        builder.dismissOnBackPressed(false)
            .dismissOnTouchOutside(false)
            .maxHeight((ScreenUtils.getScreenHeight() * 0.7f).toInt())
    }

    override fun layoutId(): Int {
        return R.layout.dialog_gjc_uld_select
    }

    override fun onDialogCreated(context: Context) {
        binding.model = this

        binding.rv.refresh(list)

        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {
                CheckUtil.handleSingleCheck(list, "checked", position, cancelAble = false)
            }
        })
    }

    fun onConfirmClick(view: View) {
        val find = list.find { it.checked.get() }
        if (find == null) {
            showToast("请选择一个ULD")
            return
        }
        callBack.invoke(find)
        dismiss()
    }
}