package com.cheng.blzb.ui.fragment.bid.publish.goods

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

class MyGoodsViewModel: BaseViewModel() {
    val goodsLiveData = MutableLiveData<List<MerchantGoodsEntity>>()
    val deleteLiveData = MutableLiveData<Any>()

    fun getGoodsList(page: Int) {
        launchOnUiTryCatch({
            val params = mutableMapOf<String, String>()
            params["page"] = "$page"
            params["size"] = "20"
            val response = ApiFactory.apiService.getMyGoodsList(params)
            if (response.status) {
                goodsLiveData.postValue(response.data.items)
            } else toast(response.message, true)
        }, {
            setError(it)
            L.d(it)
        })
    }

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