package com.lukouguoji.aerologic.page.gnj.manifest.add

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.bean.GnjManifestBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.DetailsPageType
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.loge
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.toRequestBody
import com.lukouguoji.module_base.ktx.verifyNullOrEmpty
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue

class GnjManifestAddViewModel : BaseViewModel() {

    var id = ""

    val add = MutableLiveData(true)
    val pageType = MutableLiveData(DetailsPageType.Add)

    val agentList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getAgentList(addAll = false) {
            value = it
        }
    }
    val spCodeList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getSpecialCodeList(addAll = false, flag = 0, ieFlag = "I") {
            value = it
        }
    }
    val waybillTypeList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getWaybillTypeList2(type = "CI", addAll = false, checkedValue = "CICI1") {
            value = it
        }
    }
    val businessTypeList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getBusinessTypeList(type = "CI", addAll = false, checkedValue = "ANR") {
            value = it
        }
    }
    val wxKuList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getWxKuList {
            value = it
        }
    }
    val wxDescList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getWxDescList {
            value = it
        }
    }

    val dataBean = MutableLiveData(GnjManifestBean())

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

    fun initOnCreate(intent: Intent) {
        id = intent.getStringExtra(Constant.Key.ID) ?: ""

        val modify = intent.getBooleanExtra(Constant.Key.MODIFY, false)
        pageType.value = if (id.isEmpty()) DetailsPageType.Add
        else if (modify) DetailsPageType.Modify else DetailsPageType.Details

        if (id.isNotEmpty()) {
            getDetails()
        }
    }

    private fun getDetails() {
        launchLoadingCollect({
            NetApply.api.getGnjManifestDetail(id)
        }) {
            onSuccess = {
                dataBean.value = it.data ?: GnjManifestBean()
            }
        }
    }

    fun waybillChanged() {
        val wbNo = dataBean.value!!.wbNo
        if (wbNo.isEmpty()) {
            return
        }
        if (wbNo.length != 11) {
            getTopActivity().showConfirmDialog("运单号不能小于11位数字") { }
            return
        }
        if (wbNo.substring(3, 10).toInt().mod(7) != wbNo.last().toString().toInt()) {
            getTopActivity().showConfirmDialog("运单号不规范,是否强制使用!") { }
        }
    }

    fun onSaveClick() {
        val bean = dataBean.value!!
        if ((bean.wbNo.verifyNullOrEmpty("请输入运单号")
                    || bean.agentCode.verifyNullOrEmpty("请选择代理人")
                    || bean.spCode.verifyNullOrEmpty("请选择特码")
                    || bean.totalPc.verifyNullOrEmpty("请输入运单件数")
                    || bean.pc.verifyNullOrEmpty("请输入实到件数")
                    || bean.weight.verifyNullOrEmpty("请输入实到重量")
                    || bean.cashWeight.verifyNullOrEmpty("请输入计费重量")
                    || bean.origin.verifyNullOrEmpty("请输入始发港")
                    || bean.dest.verifyNullOrEmpty("请输入目的港")
                    || bean.goods.verifyNullOrEmpty("请输入品名")
                    )
        ) {
            return
        }
        if (bean.origin.length != 3) {
            loge("始发港长度必须为3")
            return
        }
        if (bean.dest.length != 3) {
            loge("目的港长度必须为3")
            return
        }
        launchLoadingCollect({
            NetApply.api.anyPost(
                if (pageType.value!! == DetailsPageType.Add) "DomImpManiFest/saveFest" else "DomImpManiFest/updateFest",
                bean.toRequestBody()
            )
        }) {
            onSuccess = {
                showToast(it.msg.noNull("保存成功"))
                FlowBus.with<Any>(Constant.Key.FNJ_MANIFEST_ADD).tryEmit(1)
                getTopActivity().finish()
            }
        }
    }

    fun onCancelClick() {
        DevUtils.getTopActivity().finish()
    }

}