package cn.muchinfo.rma.view.base.home.business

import androidx.lifecycle.MutableLiveData
import cn.muchinfo.rma.global.GlobalDataCollection
import cn.muchinfo.rma.global.data.BusinessData
import cn.muchinfo.rma.netManage.base.InteractiveException
import cn.muchinfo.rma.view.MyApplication
import cn.muchinfo.rma.view.autoWidget.toArrayList
import cn.muchinfo.rma.view.base.BaseViewModel
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import mtp.polymer.com.autowidget.utils.TaskUiModel

class BusinessManagementViewModel : BaseViewModel() {

    /**
     * 加载状态控制
     */
    val loadingDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()

    /**
     * 加载状态控制
     */
    val loadingDataDialogStatus: MutableLiveData<TaskUiModel> = MutableLiveData()

    /**
     * 业务管理列表点价数据
     */
    val somePriceList: MutableLiveData<List<BusinessData>> = MutableLiveData()

    /**
     * 业务管理列表结算数据
     */
    val settlementList: MutableLiveData<List<BusinessData>> = MutableLiveData()

    /**
     * 结算审核详情
     */
    val settlementDetails : MutableLiveData<BusinessData> = MutableLiveData()


    /**
     * 对于列表点击展开后的数据处理
     * @param index Int 点击的item数据在列表的index
     * @param type String 点击的列表类型
     *
     */
    fun resetDataList(index: Int, type: String) {
        val datanewlist = arrayListOf<BusinessData>()
        when (type) {
            "1" -> {
                somePriceList.value?.forEach {
                    if (index == somePriceList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                somePriceList.postValue(datanewlist)
            }
            "2" -> {
                settlementList.value?.forEach {
                    if (index == settlementList.value?.indexOf(it)) {
                        if (it.indexSelect == 0) {
                            datanewlist.add(it.copy(indexSelect = 1))
                        } else {
                            datanewlist.add(it.copy(indexSelect = 0))
                        }
                    } else {
                        datanewlist.add(it)
                    }
                }
                settlementList.postValue(datanewlist)
            }
        }
    }

    /**
     * 请求业务管理点价列表
     */
    fun queryBusinessDJ() {
        val params = mutableMapOf<String, String>().apply {
            put("UserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }
        MyApplication.getInstance()?.businessManager?.queryBusinessDJ(params) { isSuccess, respData, _ ->
            if (isSuccess){
                respData?.toArrayList()?.sortWith(Comparator { t1, t2 ->
                    (TimeUtils.string2Millis(t2.applytime) - TimeUtils.string2Millis(t1.applytime)).toInt()
                })
                somePriceList.postValue(respData)
            }
        }
    }


    /**
     * 请求业务管理结算列表
     */
    fun queryBusinessJS() {
        val params = mutableMapOf<String, String>().apply {
            put("UserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.businessManager?.queryBusinessJSEx(params) { isSuccess, respData, _ ->
            if (isSuccess){
                respData?.toArrayList()?.sortWith(Comparator { t1, t2 ->
                    (TimeUtils.string2Millis(t2.applytime) - TimeUtils.string2Millis(t1.applytime)).toInt()
                })
                settlementList.postValue(respData)
            }
        }
    }

    /**
     * 结算审核请求详情
     */
    fun queryBusinessDetails(relatedid : String){
        loadingDataDialogStatus.value = TaskUiModel.inFlight()
        val params = mutableMapOf<String, String>().apply {
            put("UserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
            put("relatedid",relatedid)
        }
        MyApplication.getInstance()?.businessManager?.queryBusinessJSDetail(params = params){ isSuccess, respData, err ->
            if (isSuccess){
                loadingDataDialogStatus.value = TaskUiModel.success(msg = "数据请求成功")
                settlementDetails.postValue(respData?.get(0))
            }else{
                loadingDataDialogStatus.postValue(TaskUiModel.failed(InteractiveException(errorMessage = err?.message!!)))
            }
        }
    }

    /***
     * 合同操作请求
     * @param info ContractOperateApplyInfo 操作信息
     * @param operateType Int 操作类型
     * @param remark String 备注
     * @param isSuccess Function1<[@kotlin.ParameterName] Boolean, Unit>
     */
    fun contractOperaOrder(
        operateApplyID : Long = 0,
        operateType: Int = 0,
        remark: String = "",
        isSuccess: (isCompleted: Boolean) -> Unit
    ){
        loadingDataDialogStatus.value = TaskUiModel.inFlight()
        MyApplication.getInstance()?.contractManager?.orderPerationContract(operateApplyID = operateApplyID,operateType = operateType,remark = remark){isCompleted, err ->
            if (isCompleted){
                ToastUtils.showLong("请求成功")
                loadingDialogStatus.postValue(TaskUiModel.success(msg = "请求成功"))
                isSuccess(true)
            }else{
                loadingDialogStatus.postValue(TaskUiModel.failed(InteractiveException(errorMessage = err?.message!!)))
            }
        }
    }
}