package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONObject
import com.xty.base.vm.BaseVm
import com.xty.network.model.RespBody
import okhttp3.MultipartBody
import java.io.File

class UploadHealthRecordVm : BaseVm() {

    val imageLive1 by lazy { MutableLiveData<RespBody<MutableList<String>>>() }

    val submitLiveData by lazy { MutableLiveData<RespBody<Any>>() }


    /**
     * 上传图片
     */
    fun uploadImagesFiles(path: List<String>) {
        if (path.isEmpty()) {
            return
        }
        val list = mutableListOf<MultipartBody.Part>()
        for (s in path) {
            if (s.contains("http")) {
                continue
            }
            val f = File(s)
            val rb =
                MultipartBody.Part.createFormData("files", f.name, retrofits.getMultiPartFileRb(f))
            list.add(rb)
        }
        if (list.isEmpty()) {
            return
        }
        startHttp {
            val body = apiInterface().uploadImagesFile(list)
            body.getCodeStatus(imageLive1, nowData)
        }
    }

    fun submitReport(title:String,reportType:String,reportTime:String,pic:String,remark:String){
        startHttp {
            var json = JSONObject()
            json.put("title",title)
            json.put("reportType",reportType)

            json.put("reportTime",reportTime)

            json.put("pic",pic)

            json.put("remark",remark)

            var response = apiInterface().submitHealthReport(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(submitLiveData, nowData)
        }
    }
}