package com.cheng.blzb.ui.fragment.merchant.detail

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.MerchantGoodsEntity
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.Companion.toRequestBody

class MerchantGoodsDetailViewModel: BaseViewModel() {
    val infoLiveData = MutableLiveData<MerchantGoodsEntity>()

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

    fun sendAuth() {
        launchOnUiTryCatch({
            val jsonObject = JsonObject()
            jsonObject.addProperty("scene", "info")
            jsonObject.addProperty("count", 1)
            ApiFactory.apiService.sendAuth(jsonObject.toString().toRequestBody())
        }, {
            L.d(it)
        })
    }
}