package com.cheng.blzb.ui.fragment.mine.order.list

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.OrderEntity
import com.cheng.blzb.net.ApiFactory
import com.example.base.extensions.toast
import com.example.base.utils.L
import com.example.base.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

class OrderListViewModel : BaseViewModel() {
    val orderLiveData = MutableLiveData<List<OrderEntity>>()
    val updateLiveData = MutableLiveData<Any>()

    fun getOrderList(status: String) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getOrderList(status)
            if (response.status) {
                orderLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun updateOrder(orderId: String) {
        showDialog()
        launchOnUiTryCatch({
            val jsonObject = JsonObject()
            jsonObject.addProperty("order_id", orderId)
            jsonObject.addProperty("invoice_status", "3")
            val response = ApiFactory.apiService.updateOrder(jsonObject.toString().toRequestBody())
            if (response.status) {
                updateLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }
}