package com.zj365.mime.vm

import androidx.lifecycle.MutableLiveData
import com.luck.picture.lib.entity.LocalMedia
import com.xty.base.vm.BaseVm
import com.xty.common.Const
import com.xty.common.util.CommonToastUtils
import com.xty.network.model.FeedBackBean
import com.xty.network.model.PaingBean
import com.xty.network.model.RespBody
import okhttp3.MultipartBody
import org.json.JSONArray
import org.json.JSONObject
import java.io.File

class FeedBackVm: BaseVm() {

    val uploadLive by lazy{MutableLiveData<RespBody<String>>()}
    val feedBackListLive by lazy{MutableLiveData<RespBody<PaingBean<FeedBackBean>>>()}
    val delete by lazy{MutableLiveData<RespBody<Any>>()}
    fun uploadFeedBack(video:String,listPhoto:MutableList<LocalMedia>,content:String,id:String,name:String){
        if(content.isEmpty()){
            CommonToastUtils.showToast("请输入意见和建议")
            return
        }
        var listFile= mutableListOf<MultipartBody.Part>()
        if(video.isNotEmpty()){
            var fileVideo=File(video)
            listFile.add(MultipartBody.Part.createFormData("files",fileVideo.name,retrofits.getMultiPartFileRb(fileVideo)))
        }

        listPhoto.forEach {
            if(it.size!=0L){
                var file=File(it.compressPath)
                var part=MultipartBody.Part.createFormData("files",file.name,retrofits.getMultiPartFileRb(file))
                listFile.add(part)
            }
        }
        if(content.isEmpty()){
            CommonToastUtils.showToast("请填写反馈意见")
        }

        startHttp {
            var response=apiInterface().uploadFeed(listFile,retrofits.getRequestBody(content)
                ,retrofits.getRequestBody(id) ,retrofits.getRequestBody(name) ,retrofits.getRequestBody("1"))
            response.getCodeStatus(uploadLive,nowData)
        }
    }


    /**
     * 意见反馈列表
     */
    fun getFeedBackList(page:Int){
        startHttp {
            var json=JSONObject()
            json.put("pageNum",page)
            json.put("pageSize",Const.PAGE_SIZE)
            var body= apiInterface().getFeedBackList(retrofits.getRequestBody(json .toString()))
            body.getCodeStatus(feedBackListLive,nowData)
        }
    }
    /**
     * 删除意见反馈
     */
    fun deleteFeedBack(id:Int){
        startHttp {
            var array=JSONArray()
            array.put(id)
            var body=apiInterface().deleteFeedBack(retrofits.getRequestBody(array.toString()))
            body.getCodeStatus(delete,nowData)
        }
    }
}