package com.lukouguoji.aerologic.page.gnj.jiaojie

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.gnj.query.details.GnjQueryDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GncAssembleListBean
import com.lukouguoji.module_base.bean.GnjQueryBean
import com.lukouguoji.module_base.bean.GnjUnloadListBean
import com.lukouguoji.module_base.bean.GoodsTransportBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.ICheck
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.formatDate
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.toJson
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.PadFilterDialogModel
import com.lukouguoji.module_base.model.PadFilterModel
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.ui.weight.search.layout.SearchLayoutType
import com.lukouguoji.module_base.util.CheckUtil
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class GnjHandoverListViewModel : BasePageViewModel() {

    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())

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

    // 始发站
    val fdep = MutableLiveData("")

    // 平板车
    val car = MutableLiveData("")

    // 仓管交接
    val whsOpName = MutableLiveData("")

    // 危险品描述
    val type = MutableLiveData("")

    // 危险品库
    val status = MutableLiveData("")


    val whsOpNameList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getWHSUserList { value = it }
    }
    // 类型列表
    val typeList = MutableLiveData<List<KeyValue>>(emptyList()).apply {
        DictUtils.getTransTypeList {
            value = listOf(KeyValue("全部", "")) + it.filter { i ->  listOf("3","4").contains(i.value)}
        }
    }

    // 状态列表
    val statusList = MutableLiveData<List<KeyValue>>(emptyList()).apply {
        DictUtils.getTransStatusList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }

    val itemViewHolder = GnjHanoverListViewHolder::class.java
    val itemLayoutId = R.layout.item_gnj_handover_list

    val count = MutableLiveData(0)

    val handoverCount = MutableLiveData(0)

    private val filterModel by lazy {
        PadFilterDialogModel(
            getTopActivity(), listOf(
                PadFilterModel(
                    type,
                    "转运类型",
                    "请选择转运类型",
                    type = SearchLayoutType.SPINNER,
                    typeList
                ),
                PadFilterModel(
                    status,
                    "运输状态",
                    "请选择运输状态",
                    type = SearchLayoutType.SPINNER,
                    statusList
                ),
            )
        ) {
            refresh()
        }
    }

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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnjUnloadCarWithFileList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to fNo.value,
                    "fdep" to fdep.value,
                    "carId" to car.value,
                    "whsOpName" to whsOpName.value,
                    "transType" to type.value,
                    "transStatus" to status.value
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
                handoverCount.value = it.totalCar
            }
        }

    }


    fun handover() {
        val list = pageModel.rv!!.commonAdapter()!!.items as List<GnjUnloadListBean>
        val filter = list.filter { it.checked.get() }
        if (filter.isEmpty()) {
            showToast("请选择数据")
            return
        }
        if (filter.any { it.whsOpName.isNotEmpty() }) {
            showToast("包含已交接数据")
            return
        }
        launchLoadingCollect({
            NetApply.api.anyPost(
                "unloadingAirCraft/handover",
                mapOf(
                    "idList" to filter.map { it.id },
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                showToast(it.msg.noNull("单证交接成功"))
                refresh()
            }
        }
    }

    fun filterClick() {
        filterModel.show()
    }

    fun onCarScanClick() {
        ScanModel.startScan(getTopActivity(), Constant.RequestCode.CAR)
    }

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

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                Constant.RequestCode.CAR -> {
                    car.value = data.getStringExtra(Constant.Result.CODED_CONTENT)
                }

                else -> {}
            }
        }
    }
}