package com.lukouguoji.gjj.model

import android.content.Context
import android.view.View
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.DialogTallyAddSelectBinding
import com.lukouguoji.gjj.holder.GjcTallyDialogViewHolder
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.GjcWaybillBean
import com.lukouguoji.module_base.bean.GjjHandoverRecordBean
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 GjjTallyAddSelectDialogModel(
    val list: List<GjjHandoverRecordBean>,
    private val callBack: (GjjHandoverRecordBean) -> Unit
) : BaseDialogModel<DialogTallyAddSelectBinding>(DIALOG_TYPE_CENTER) {

    val itemId = R.layout.item_gjj_tally_dialog
    val itemHolder = GjcTallyDialogViewHolder::class.java

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

    override fun layoutId(): Int {
        return R.layout.dialog_tally_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()
    }
}