package com.lukouguoji.hangban.page.query.list

import androidx.lifecycle.MutableLiveData
import com.alibaba.android.arouter.launcher.ARouter
import com.lukouguoji.hangban.R
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.FlightBean
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.launchLoadingCollect
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.DictUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class HbQueryListViewModel : BasePageViewModel() {

    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())
    val fNo = MutableLiveData("")
    val dep = MutableLiveData("")   // 始发站
    val dest = MutableLiveData("")  // 目的站
    val addressType = MutableLiveData("")
    val serviceType = MutableLiveData("")

    val addressTypeList = MutableLiveData(listOf(KeyValue("全部", ""))).apply {
        DictUtils.getCountryTypeList {
            value = it
        }
    }
    val serviceTypeList = MutableLiveData(
        listOf(
            KeyValue("全部", ""),
            KeyValue("客机", "0"),
            KeyValue("货机", "1"),
            KeyValue("卡车", "2"),
        )
    )

    val itemLayoutId = R.layout.item_hb_query_list
    val itemViewHolder = HbQueryListViewHolder::class.java

    val count = MutableLiveData(0)

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

    fun searchClick() {
        refresh()
    }

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getFlightList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to fNo.value,
                    "range" to buildRange(),
                    "countryType" to addressType.value,
                    "serviceType" to serviceType.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    private fun buildRange(): String? {
        val d = dep.value?.trim()
        val a = dest.value?.trim()
        if (d.isNullOrEmpty() && a.isNullOrEmpty()) return null
        return "${d ?: ""}-${a ?: ""}"
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as FlightBean
        ARouter.getInstance().build(ARouterConstants.ACTIVITY_URL_FLIGHT_QUERY_DETAILS)
            .withString(Constant.Key.ID, bean.fid)
            .navigation()
    }

}
