package com.cheng.blzb.ui.fragment.mine.seat

import androidx.lifecycle.MutableLiveData
import com.cheng.blzb.bean.CorpEntity
import com.cheng.blzb.bean.SeatItemEntity
import com.cheng.blzb.bean.VipGoodsEntity
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 okhttp3.RequestBody

class SeatManageViewModel: BaseViewModel() {
    val goodsLiveData = MutableLiveData<List<VipGoodsEntity>>()
    val corpInfoLiveData = MutableLiveData<CorpEntity>()
    val seatListLiveData = MutableLiveData<List<SeatItemEntity>>()
    val addLiveData = MutableLiveData<Any>()
    val deleteLiveData = MutableLiveData<Any>()

    fun getGoodsList() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getGoodsList("corp_add")
            if (response.status) {
                goodsLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getCorpInfo() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getSeatInfo()
            if (response.status) {
                corpInfoLiveData.postValue(response.data)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun getSeatList() {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.getSeatList()
            if (response.status) {
                seatListLiveData.postValue(response.data.items)
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

    fun addSeat(requestBody: RequestBody) {
        showDialog()
        launchOnUiTryCatch({
            val response = ApiFactory.apiService.addSeat(requestBody)
            if (response.status) {
                addLiveData.postValue(Any())
            } else toast(response.message, true)
            dismissDialog()
        }, {
            dismissDialog()
            setError(it)
            L.d(it)
        })
    }

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