package com.lukouguoji.gnc.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.http.user.UserNetwork
import com.lukouguoji.gnc.model.GncTransferRef
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.String.emptyToNull
import kotlinx.coroutines.Dispatchers


class GoutTransferViewModel : ViewModel() {

    /**
     * 转运-搜索分页
     */
    private val searchParam = MutableLiveData<JSONObject>()

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

    fun search(
        limit: Int,
        page: Int,
        fdate: String,
        fno: String,
        location: String//板车号
    ) {
        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["fdate"] = fdate.emptyToNull()
        param["fno"] = fno.emptyToNull()
        param["location"] = location.emptyToNull()

        searchParam.value = param
    }

}