package com.sixin.module_p.ui.user.message

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.MessageBean
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.sixin.module_p.R
import dev.utils.common.RandomUtils

class PdaMessageListViewModel : BasePageViewModel() {

    val index = MutableLiveData(0)

    val count = MutableLiveData(0)

    val itemLayoutId = R.layout.item_pda_message
    val itemViewHolder = PdaMessageListViewHolder::class.java

     val filterDialogModel by lazy {
        PdaMessageFilterDialogModel {
            refresh()
        }
    }

    fun onTabChanged(type: Int) {
        if (index.value != type) {
            index.value = type
            refresh()
        }
    }

    override fun getData() {
        val body = mapOf(
            "pageNum" to pageModel.page,
            "pageSize" to pageModel.limit,
            "state" to index.value,
            "beginDate" to filterDialogModel.beginDate.value,
            "endDate" to filterDialogModel.endDate.value,
            "content" to filterDialogModel.keyWord.value,
        ).toRequestBody()
        launchLoadingCollect({
            NetApply.api.getMessageList(
                body
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    fun onConfirmClick() {
        val list = pageModel.rv?.commonAdapter()?.items?.filter {
            (it as MessageBean).checked.get()
        } ?: emptyList()
        if (list.isEmpty()) {
            showToast("请选择需要处理的数据")
            return
        }
        launchLoadingCollect({
            NetApply.api.anyPost(
                "msg/handleList",
                list.map { (it as MessageBean).id }.toRequestBody()
            )
        }) {
            onSuccess = {
                showToast(it.msg.noNull("处理成功"))
                refresh()
            }
        }
    }

    fun onFilterClick() {
        filterDialogModel.show()
    }

}