package com.zj365.health.vm

import androidx.lifecycle.MutableLiveData
import com.xty.base.vm.BaseVm
import com.xty.network.model.FamilyBean
import com.xty.network.model.RelationList
import com.xty.network.model.RespBody
import org.json.JSONArray
import org.json.JSONObject

class FamilyVm : BaseVm() {

    val familyLive by lazy { MutableLiveData<RespBody<MutableList<FamilyBean>>>() }
    val cancelLive by lazy { MutableLiveData<RespBody<String>>() }
    val relationLive by lazy { MutableLiveData<RespBody<MutableList<RelationList>>>() }
    val sortLive by lazy { MutableLiveData<RespBody<String>>() }
    val changeOk by lazy { MutableLiveData<RespBody<String>>() }

    fun getMyFamilyList() {
        startHttp {
            val json = JSONObject()
            /* json.put("pageNum","1")
             json.put("pageSize",Const.PAGE_SIZE)*/
            val request = apiInterface().getMyFamily(retrofits.getRequestBody(json.toString()))
            request.getCodeStatus(familyLive, nowData)
        }
    }

    //解绑
    fun familyCancel(id: String) {
        startHttp {
            var response = apiInterface().familyCancel(id)
            response.getCodeStatus(cancelLive, nowData)
        }
    }

    //获取关系列表
    fun getFamilyRelationList() {
        startHttp {
            var response = apiInterface().getFamilyRelationList()
            response.getCodeStatus(relationLive, nowData)
        }

    }

    //修改昵称
    fun changeFamilyNickName(bean: FamilyBean, nickName: String, typeName: String) {
        startHttp {
            var json = JSONObject()
            json.put("id", bean.id)
            json.put("userJointId", bean.userJointId)
            json.put("fCall", nickName)
            json.put("type", typeName)
            var response =
                apiInterface().changeFamilyNickName(retrofits.getRequestBody(json.toString()))
            response.getCodeStatus(changeOk, nowData)
        }
    }


    //修改排序
    fun changeFamilySort(list: MutableList<FamilyBean>) {
        startHttp {
            var array = JSONArray()
            list.forEachIndexed { index, familyBean ->
                var json = JSONObject()
                json.put("id", familyBean.id)
                json.put("sort", index)
                array.put(json)
            }
            var response =
                apiInterface().changeFamilyNickName(retrofits.getRequestBody(array.toString()))
            response.getCodeStatus(sortLive, nowData)
        }
    }
}