package com.lukouguoji.gnc.page.distribution.handover

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gnc.R
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.FlightBean
import com.lukouguoji.module_base.bean.GncDistributionBean
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.launchCollect
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.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.utils.DevFinal
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class GncDistributionHandoverViewModel : BasePageViewModel() {

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

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

    // 单证类型
    val docType = MutableLiveData("")

    // 交接状态
    val handoverStatus = MutableLiveData("")

    // 司机
    val driver = MutableLiveData("")

    // 数量
    val count = MutableLiveData("0")

    // 单证类型列表
    val docTypeList = DictUtils.docTypeList

    // 交接状态列表
    val handoverStatusList = DictUtils.handoverStatusList

    // 司机列表
    val driverList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getDriverList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }

    val itemLayoutId = R.layout.item_gnc_distribution_handover
    val itemViewHolder = GncDistributionHandoverViewHolder::class.java

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

    fun initOnCreated() {
        date.value = DateUtils.getCurrentTime().formatDate()
    }

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGncDistributionHandoverList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to flightNo.value,
                    "fileType" to docType.value,
                    "handoverStatus" to handoverStatus.value,
                    "driver" to driver.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total.toString()
            }
        }
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as FlightBean
        when (type) {
            R.id.tv_modify -> {
                if (bean.id.isEmpty()) {
                    showToast("该数据没有单证交接记录，无法修改")
                    return
                }
                if (bean.actualTakeOff.isNotEmpty()) {
                    showToast("该航班已起飞，无法修改")
                    return
                }
                GncDistributionHandoverModifyDialogModel(driverList = driverList.value!!.filter { it.key != "全部" },bean) { model ->
                    launchLoadingCollect({
                        NetApply.api.anyPost(
                            "flt/updateHandover",
                            mapOf(
                                "fileType" to model.getFileType(),
                                "driver" to model.driver.value,
                                "id" to bean.id,
                                "fid" to bean.fid
                            ).toRequestBody()
                        )
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("修改成功"))
                            refresh()
                        }
                    }
                }.show()
            }

            else -> {}
        }
    }

    /**
     * 开始交接 点击
     */
    fun onConfirmClick() {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val list = (adapter.items as List<FlightBean>).filter { it.checked.get() }
        if (list.isEmpty()) {
            showToast("请选择数据")
            return
        }
        if (list.any { it.id.isNotEmpty() }) {
            showToast("已存在单证交接记录！")
            return
        }
        if (list.any { it.actualTakeOff.isNotEmpty() }) {
            showToast("航班已起飞，无法修改")
            return
        }
        GncDistributionHandoverModifyDialogModel(driverList = driverList.value!!.filter { it.key != "全部" }) { model ->
            launchLoadingCollect({
                NetApply.api.anyPost(
                    "flt/insertHandoverBatch",
                    mapOf(
                        "fileType" to model.getFileType(),
                        "driver" to model.driver.value,
                        "fidList" to list.map { it.fid },
                        "ieFlag" to "E"
                    ).toRequestBody()
                )
            }) {
                onSuccess = {
                    showToast(it.msg.noNull("单证交接成功"))
                    refresh()
                }
            }
        }.show()
    }

    /**
     * 屏蔽无货邮航班 点击
     */
    fun onHideFlightClick() {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val list = (adapter.items as List<FlightBean>).filter { it.checked.get() }
        if (list.isEmpty()) {
            showToast("请选择数据")
            return
        }
            launchLoadingCollect({
                NetApply.api.anyPost(
                    "flt/updateFlightCMFlag",
                    mapOf(
                        "fidList" to list.map { it.fid },
                    ).toRequestBody()
                )
            }) {
                onSuccess = {
                    showToast("无货邮航班设置成功！")
                    refresh()
                }
            }
    }
}