package com.lukouguoji.gjc.viewModel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers

class GjcQueryInfoViewModel : ViewModel() {

    private val logName = "GncQueryInfoViewModel"

    var wayBillInfo = JSONObject()
    var wbNo: String? = null

    /**
     * 查询详情
     */
    inner class WaybillParam(val wbId: Int, val wbNo: String?)

    private val wbIdData = MutableLiveData<WaybillParam>()
    val waybillLiveData = Transformations.switchMap(wbIdData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = JSONObject()
                if (param.wbId != -9999) {
                    response = UserNetwork.searchByIdGjcQuery(param.wbId)
                } else if (param.wbNo != null) {
                    response = UserNetwork.searchByWbNoGncQuery(param.wbNo)
                } else {
                }
                emit(response)
            } catch (e: Exception) {
                Log.e(logName, e.stackTraceToString())
            }

        }
    }

    fun queryWaybillById(wbId: Int, wbNo: String?) {
        wbIdData.value = WaybillParam(wbId, wbNo)
    }


}