package com.lukouguoji.aerologic.page.flight.query.list

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.flight.query.details.FlightQueryDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.FlightBean
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.util.DictUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class FlightQueryListViewModel : 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_flight_query_list
    val itemViewHolder = FlightQueryListViewHolder::class.java

    val count = MutableLiveData(0)

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

    override fun getData() {
        val range = if (dep.value.isNullOrEmpty() && dest.value.isNullOrEmpty()) {
            ""
        } else {
            "${dep.value}-${dest.value}"
        }
        launchLoadingCollect({
            NetApply.api.getFlightList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to fNo.value,
                    "range" to range,
                    "fdep" to dep.value,
                    "fdest" to dest.value,
                    "countryType" to addressType.value,
                    "serviceType" to serviceType.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

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

}