package com.lukouguoji.gnc.page.assemble.add

import android.content.Context
import android.view.View
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.databinding.DialogGncAssembleBatchBinding
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.bean.GjjHandoverRecordBean
import com.lukouguoji.module_base.bean.WaybillBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.ILoading
import com.lukouguoji.module_base.ktx.getLifecycleOwner
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.ktx.tryCatch
import com.lukouguoji.module_base.util.Arith
import com.lxj.xpopup.XPopup
import dev.DevUtils
import dev.utils.app.ScreenUtils
import kotlin.math.roundToInt

class GncAssembleBatchDialogModel(
    val bean: WaybillBean,
    val callBack: () -> Unit
) : BaseDialogModel<DialogGncAssembleBatchBinding>(DIALOG_TYPE_CENTER) {

    // 剩余件数
    val remainingCount = MutableLiveData(bean.pc.noNull())

    // 剩余重量
    val remainingWeight = MutableLiveData(bean.weight.noNull())

    // 件数
    val count = MutableLiveData("")

    // 重量
    val weight = MutableLiveData("")

    override fun onBuild(builder: XPopup.Builder) {
        builder.maxWidth((ScreenUtils.getScreenWidth() * 0.5).roundToInt())
    }

    override fun layoutId() = R.layout.dialog_gnc_assemble_batch

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

        // 件数改变监听
        count.observe(context.getLifecycleOwner()) {
            tryCatch({ _ ->
                val old = bean.pc.noNull().toInt()
                val new = it.toInt()
                if (old < new) {
                    count.value = bean.pc.noNull()
                    remainingCount.postValue("0")
                } else {
                    val c = (old - new).toString()
                    remainingCount.postValue(c)
                }
            }) {
                remainingCount.postValue(bean.pc.noNull())
            }
        }

        // 重量改变监听
        weight.observe(context.getLifecycleOwner()) {
            tryCatch({ _ ->
                val old = bean.weight.noNull().toDouble()
                val new = it.toDouble()
                if (old < new) {
                    weight.value = bean.weight.noNull()
                    remainingWeight.postValue("0")
                } else {
                    val w =
                        Arith.round((old - new), 2).toString()
                    remainingWeight.postValue(w)
                }
            }) {
                remainingWeight.postValue(bean.weight.noNull())
            }
        }
    }

    /**
     * 保存
     */
    fun save(view: View) {
        launchCollect({
            NetApply.api.anyPost(
                "DomExpLoading/gncWarehouseBatch", mapOf(
                    "pc" to count.value,
                    "weight" to weight.value,
                    "whid" to bean.whId,
                    "location" to bean.location,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                showToast(it.msg.noNull("分批成功"))
                dismiss()
                callBack()
            }
        }
    }

}