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

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

class FinancialViewModel : BaseViewModel(){

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

    /**
     * 业务管理列表款项数据
     */
    val paymentList : MutableLiveData<List<FinancialManageData>> = MutableLiveData()
    /**
     * 业务管理列表发票数据
     */
    val invoiceList : MutableLiveData<List<FinancialManageData>> = MutableLiveData()

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

    /**
     * 款项列表请求
     */
    fun queryBusinessKx(){
        val params = mutableMapOf<String, String>().apply {
            put("UserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.financialManager?.queryBusinessKx(params = params){isSuccess, respData, error ->
            if (isSuccess){
                paymentList.postValue(respData)
            }
        }
    }

    /**
     * 发票列表请求
     */
    fun queryBusinessFp(){
        val params = mutableMapOf<String, String>().apply {
            put("UserId", GlobalDataCollection.instance?.loginRsp?.userID.toString())
        }

        MyApplication.getInstance()?.financialManager?.queryBusinessFp(params = params){isSuccess, respData, error ->
            if (isSuccess){
                invoiceList.postValue(respData)
            }
        }
    }

    /***
     * 合同操作请求
     * @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
    ){
        loadingDialogStatus.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!!)))
            }
        }
    }



}