package com.zj365.dc.vm

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.xty.base.vm.BaseVm
import com.xty.network.model.ChooseAddressBean
import com.xty.network.model.GoodsRecordsBean
import com.xty.network.model.RespBody
import com.xty.network.model.ShopCategoryBean
import com.xty.network.model.ShopGoodsOrderBean

class ShopMallVm : BaseVm() {

    val categoryLiveDate by lazy { MutableLiveData<RespBody<MutableList<ShopCategoryBean>>>() }

    val goodsLiveData by lazy { MutableLiveData<RespBody<GoodsRecordsBean>>() }

    val createOrderLiveData by lazy { MutableLiveData<RespBody<ShopGoodsOrderBean>>() }

    val defaultAddressLiveData by lazy { MutableLiveData<RespBody<ChooseAddressBean>>() }

    val hasCouponLiveData by lazy { MutableLiveData<RespBody<Int>> ()}

    fun getGoodsCategory(parentId:Long){
        startHttp {
            var code = apiInterface().getGoodsCategory(parentId)
            code.getCodeStatus(categoryLiveDate, nowData)
        }
    }

    fun getGoodsList(prodName:String?,categoryId:Long,isRec:Int,pageSize:Int){
        startHttp {
            var json = JSONObject()
            prodName?.let {
                json.put("prodName",it)
            }
            json.put("categoryId",categoryId)
            json.put("isRec",isRec)
            json.put("pageSize",20)
            json.put("pageNum",pageSize)
            var code = apiInterface().getGoodsList(retrofits.getRequestBody(json.toString()))
            code.getCodeStatus(goodsLiveData, nowData)
        }
    }

    fun createOrder(skuId:String,num:Int,addrId:Long?,couponsId:Long?){
        startHttp {
            var json = JSONObject()

            json.put("skuId",skuId)
            json.put("num",num)
            addrId?.let {
                json.put("addrId",it)
            }
            couponsId?.let {
                json.put("couponsId",it)

            }
            var code = apiInterface().createOrder(retrofits.getRequestBody(json.toString()))
            code.getCodeStatus(createOrderLiveData, nowData)
        }
    }

    fun getDefaultAddress(){
        startHttp(false) {
            var code = apiInterface().getDefaultAddress()
            code.getCodeStatus(defaultAddressLiveData, nowData)
        }
    }

    fun hasCoupon(){
        startHttp(false) {
            var code = apiInterface().hasCoupons()
            code.getCodeStatus(hasCouponLiveData, nowData)
        }
    }

}