package com.lukouguoji.gnc.page.fubang.list

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.page.fubang.details.GncFuBangDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GncFuBangBean
import com.lukouguoji.module_base.bean.GncShouYunBean
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.formatDate
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.util.DictUtils
import com.lukouguoji.module_base.util.PrinterUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import okhttp3.RequestBody

class GncFuBangListViewModel : BasePageViewModel() {

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

    val fno = MutableLiveData("")

    // 目的港
    val dest = MutableLiveData("")
    val car = MutableLiveData("")
    val status = MutableLiveData("")
    val reWeightStatusList = MutableLiveData<List<KeyValue>>(DictUtils.reWeightStatusList)

    //查询条件增加“全部“、”上舱单“、”未上舱单“查询功能，查询条件默认”全部“
    val mftFlag = MutableLiveData("")
    val copStatus = MutableLiveData("")
    val mftFlagList = MutableLiveData<List<KeyValue>>(DictUtils.mftFlagList)



    // 数量
    val count = MutableLiveData("0")
    val totalPc = MutableLiveData("0")
    val totalWeight = MutableLiveData("0")
    val totalCCargoWeight = MutableLiveData("0")

    val itemLayoutId = R.layout.item_gnc_fu_bang
    val itemViewHolder = GncFuBangListViewHolder::class.java

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

    override fun getData() {
        val requestBody = mapOf(
            "pageNum" to pageModel.page,
            "pageSize" to pageModel.limit,
            "fdate" to date.value,
            "fno" to fno.value,
            "fdest" to dest.value,
            "checkFlag" to status.value,
            "mftFlag" to mftFlag.value,
            "copStatus" to copStatus.value,
            "carId" to car.value,
        ).toRequestBody()
        launchLoadingCollect({
            NetApply.api.getGncFuBangList(
                requestBody
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total.toString()
                totalPc.value = it.totalPc.toString()
                totalWeight.value = it.totalWeight.toString()
                totalCCargoWeight.value = it.totalCCargoWeight.toString()
            }
        }
    }

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

    fun onAddClick() {
        GncFuBangDetailsActivity.start(getTopActivity(), "")
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as GncFuBangBean
        GncFuBangDetailsActivity.start(getTopActivity(), bean.carId)
    }

    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 -> {}
            }
        }
    }

}