package com.lukouguoji.cargo.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.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.String.emptyToNull
import com.lukouguoji.module_base.http.user.UserNetwork
import kotlinx.coroutines.Dispatchers

class CargoTrackingListViewModel : ViewModel() {

    private val searchParam = MutableLiveData<JSONObject>()

    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchCargoStatus(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }
        }
    }

    fun search(
        countryType: String,
        ieFlag: String,
        wbNo: String
    ) {
        val param = JSONObject()
        param["countryType"] = countryType.emptyToNull()
        param["ieFlag"] = ieFlag.emptyToNull()
        param["wbNo"] = wbNo.emptyToNull()
        searchParam.value = param
    }

}