package com.lukouguoji.aerologic.page.accident.visa.list

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.accident.visa.details.AccidentVisaDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.AccidentVisaBean
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.formatDate
import com.lukouguoji.module_base.ktx.getActivity
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.lukouguoji.module_base.model.ConfirmDialogModel
import com.lukouguoji.module_base.util.Common
import com.ninetripods.sydialoglib.IDialog
import dev.DevUtils
import dev.utils.common.DateUtils
import java.util.Calendar

class AccidentVisaListViewModel : BasePageViewModel() {

    // 日期
    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())

    // 航班号
    val flightNo = MutableLiveData("")

    // 发运站
    val dep = MutableLiveData("")

    // 单号
    val order = MutableLiveData("")

    // 填表人
    val name = MutableLiveData("")

    val itemLayoutId = R.layout.item_accident_visa_list
    val itemViewHolder = AccidentVisaListViewHolder::class.java

    // 合计数量
    val count = MutableLiveData(0)

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnAccidentVisaList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "opDate" to date.value,
                    "fno" to flightNo.value,
                    "dep" to dep.value,
                    "mawbno" to order.value,
                    "opid" to name.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    fun onAddClick() {
        AccidentVisaDetailsActivity.start(DevUtils.getTopActivity())
    }

    fun onDeleteClick() {
        val adapter = pageModel.rv?.commonAdapter()
        val filter =
            adapter?.items?.filter { (it as AccidentVisaBean).checked.get() }
        if (filter.isNullOrEmpty()) {
            showToast("请选择需要删除的数据")
            return
        }
        ConfirmDialogModel(
            "是否确定删除选中的数据?"
        ) {
            launchLoadingCollect({
                NetApply.api.anyPost(
                    "GnAccidentVisa/deleteList",
                    filter.map { (it as AccidentVisaBean).id }.toRequestBody()
                )
            }) {
                onSuccess = {
                    showToast(it.msg.noNull("删除成功"))
                    refresh()
                }
            }

        }.show()
    }

    fun onAllSelectClick() {
        val allChecked =
            pageModel.rv?.commonAdapter()?.items?.find { item -> !(item as AccidentVisaBean).checked.get() } == null
        pageModel.rv?.commonAdapter()?.items?.forEach {
            (it as AccidentVisaBean).checked.set(!allChecked)
        }
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as AccidentVisaBean
        when (type) {
            R.id.tv_details -> {
                AccidentVisaDetailsActivity.start(DevUtils.getTopActivity(), bean.id, false)
            }

            R.id.tv_modify -> {
                AccidentVisaDetailsActivity.start(DevUtils.getTopActivity(), bean.id, true)
            }

            R.id.tv_delete -> {
                Common.secondConfirmDialog(
                    DevUtils.getTopActivity(), "确定删除吗？"
                ) {
                    launchLoadingCollect({
                        NetApply.api.anyPostForId("/GnAccidentVisa/deleteVisa", bean.id)
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("删除成功"))
                            adapter.removeItem(position)
                        }
                    }
                }
            }

            else -> {}
        }
    }
}