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.holder.GjcUnLoadDialogViewHolder
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.GjcWaybillBean
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

class GjcBoxAddSelectDialogModel(
    val list: List<GjcWaybillBean>,
    private val callBack: (GjcWaybillBean) -> Unit
) : BaseDialogModel<DialogBoxAddSelectBinding>(DIALOG_TYPE_CENTER) {

    val itemId = R.layout.item_gjc_un_load_dialog
    val itemHolder = GjcUnLoadDialogViewHolder::class.java

    override fun onBuild(builder: XPopup.Builder) {
        builder.dismissOnBackPressed(false)
        builder.dismissOnTouchOutside(false)
    }

    override fun layoutId(): Int {
        return R.layout.dialog_box_add_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("请选择一个运单")
            return
        }
        callBack.invoke(find)
        dismiss()
    }
}