package com.lukouguoji.aerologic.page.gnj.move.stash.list

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.aerologic.R
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.MoveStashBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
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.showConfirmDialog
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.ScanModel
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 okhttp3.RequestBody

class GnjMoveStashListViewModel : BasePageViewModel() {

    val businessType = MutableLiveData("")
    val carrier = MutableLiveData("")
    val waybill = MutableLiveData("")

    val wbNoList = MutableLiveData<List<String>>()

    val businessTypeList = MutableLiveData(
        listOf(
            KeyValue("全部", ""),
            KeyValue("转国内出港", "CICO"),
            KeyValue("转国际进港", "CIII"),
            KeyValue("转国际出港", "CIIO"),
        )
    )

    val itemViewHolder = GnjMoveStashListViewHolder::class.java
    val itemLayoutId = R.layout.item_gnj_move_stash_list

    val count = MutableLiveData(0)
    val number = MutableLiveData("0")
    val weight = MutableLiveData("0")

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

    fun waybillScanClick() {
        ScanModel.startScan(getTopActivity(), Constant.RequestCode.WAYBILL)
    }


    fun getWayBillList() {
        val requestBody = mapOf(
                "page" to pageModel.page,
            "limit" to pageModel.limit,
            "awbType" to businessType.value!!.ifEmpty { null },
            "wbNo" to waybill.value!!.ifEmpty { null },
            "carrier" to carrier.value!!.ifEmpty { null },
        ).toRequestBody()
        launchCollect({
            NetApply.api.getGnjMoveStashWbNoList(
                requestBody
            )
        }) {
            onSuccess = {
                val results = it.data!!
                when {
                    results.size == 1 -> {
                        waybill.value = results[0]
                        wbNoList.value = emptyList()
                    }
                    results.size > 1 -> {
                        wbNoList.value = results
                        showWbNoListSelect()
                    }
                    else -> {
                        wbNoList.value = emptyList()
                    }
                }
            }
            onFailed = { i: Int, s: String ->
                wbNoList.value = emptyList()
            }
        }

    }


    override fun getData() {
        val body = mapOf(
            "page" to pageModel.page,
            "limit" to pageModel.limit,
            "awbType" to businessType.value!!.ifEmpty { null },
            "wbNo" to waybill.value!!.ifEmpty { null },
            "carrier" to carrier.value!!.ifEmpty { null },
        ).toRequestBody()
        launchLoadingCollect({
            NetApply.api.getGnjMoveStashList(
                body
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
        getCountData(body)
    }

    private fun getCountData(body: RequestBody) {
        launchCollect({
            NetApply.api.simplePost("DomImpMove/searchTotal", body)
        }) {
            onSuccess = {
                number.value = it.data?.totalPc.noNull("0")
                weight.value = it.data?.totalWeight.noNull("0")
            }
        }
    }

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

    fun moveStashClick() {
        val list = pageModel.rv!!.commonAdapter()!!.items as List<MoveStashBean>
        val filter = list.filter { it.checked.get() }
        if (filter.isEmpty()) {
            showToast("请选择数据")
            return
        }
        getTopActivity().showConfirmDialog("是否移库选中数据") {
            launchLoadingCollect({
                NetApply.api.anyPost(
                    "DomImpMove/move", mapOf(
                        "fid" to "0",
                        "ids" to filter.map { it.mawbId },
                    ).toRequestBody()
                )
            }) {
                onSuccess = {
                    showToast(it.msg.noNull("移库成功"))
                    refresh()
                }
            }
        }
    }

    fun checkAllClick() {
        val list = pageModel.rv!!.commonAdapter()!!.items as List<MoveStashBean>
        CheckUtil.handleAllCheck(list)
    }

    private fun showWbNoListSelect() {
        Common.singleSelect(
            DevUtils.getTopActivity(),
            "选择运单号",
            JSONArray.parseArray(wbNoList.value!!.map {
                mapOf(
                    "name" to it,
                    "code" to it,
                )
            }.toList().toJson(false)),
            null
        ) { position, _ ->
            waybill.value = wbNoList.value!![position]
        }
    }

}