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

class GjcYiKu2ListViewModel : ViewModel() {

    /**
     * 分页搜索
     */
    private val searchParam = MutableLiveData<JSONObject>()
    val searchParamLive = Transformations.switchMap(searchParam) { param ->
        liveData(Dispatchers.IO) {
            try {
                val response = UserNetwork.searchMovedGjcYiKu(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutCollectionViewModel", e.stackTraceToString())
            }

        }
    }

    fun search(
        limit: Int,
        page: Int,
        beginDate: String,
        endDate: String,
        awbType: String,
        id: String,
        wbNo: String
    ) {

        val param = JSONObject()
        param["limit"] = limit
        param["page"] = page
        param["beginDate"] = beginDate.emptyToNull()
        param["endDate"] = endDate.emptyToNull()
        param["awbType"] = awbType.emptyToNull()
        param["id"] = id.emptyToNull()
        param["wbNo"] = wbNo.emptyToNull()

        searchParam.value = param
    }


    /**
     * 运单类型
     */
    var awbList = JSONArray()
    private val awbLiveData = MutableLiveData<String>()
    fun awb(type: String) {
        awbLiveData.value = type
    }

    val awbObserver = Transformations.switchMap(awbLiveData) { param ->
        liveData(Dispatchers.IO) {
            try {
                var response = UserNetwork.awb(param)
                emit(response)
            } catch (e: Exception) {
                Log.e("GoutStartViewModel", e.stackTraceToString())
            }

        }
    }

}