package com.lukouguoji.gjj.viewModel

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.holder.GjjTallyDetailsViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjjTallyDetailsBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.limit

class GjjTallyDetailsViewModel : BasePageViewModel() {

    // 主单id
    var mfId: String = ""

    // 运单号
    var waybillNo: String = ""

    val itemId = R.layout.item_tally_details
    val itemHolder = GjjTallyDetailsViewHolder::class.java

    // 描述
    val desc = MutableLiveData("")

    override fun getData() {
        showLoading()
        launchCollect({
            NetApply.api
                .getGjjTallyDetail(mfId)
        }) {
            onSuccess = {
                it.data?.forEach { b -> b.initJianList() }
                pageModel.handleDataList(it.data)
                generateDesc(it.data)
            }

            onComplete = {
                dismissLoading()
            }
        }
    }

    private fun generateDesc(list: List<GjjTallyDetailsBean>?) {
        if (list.isNullOrEmpty()) {
            desc.value = "合计：0票，件数：0，重量：0KG"
            return
        }
        val total = list.size
        var totalJ = 0
        var totalW = 0.0

        list.forEach {
            totalJ += it.jianList.size
            totalW += it.weight?.toDouble() ?: 0.0
        }
        desc.value = "合计：${total}票，件数：${totalJ}，重量：${totalW.limit()}KG"
    }
}