package com.lukouguoji.aerologic.page.transportLog.list

import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.http.net.NetApply
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.model.PadFilterDialogModel
import com.lukouguoji.module_base.model.PadFilterModel
import com.lukouguoji.module_base.ui.weight.search.layout.SearchLayoutType
import com.lukouguoji.module_base.util.DictUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class TransportLogViewModel : BasePageViewModel() {
    val car = MutableLiveData("")

    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())

    val flightNo = MutableLiveData("")

    val transportType = MutableLiveData("")

    val transportStatus = MutableLiveData("")

    val driver = MutableLiveData("")


    val count = MutableLiveData(0)


    val transportTypeList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getTransTypeListByRole {
            value = listOf(KeyValue("全部", "")) + it
            transportType.value = ""
        }
    }
    val transportStatusList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getTransStatusList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }
    val driverList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getAllDriverList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }


    val itemLayoutId = R.layout.item_transport_log_list
    val itemViewHolder = TransportLogViewHolder::class.java

    private val filterModel by lazy {
        PadFilterDialogModel(
            getTopActivity(), listOf(
                PadFilterModel(driver, "司机", "请选择司机", SearchLayoutType.SPINNER, driverList)
            )
        ) {
            refresh()
        }
    }


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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getTransportLogList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to flightNo.value,
                    "opId" to driver.value,
                    "transType" to transportType.value,
                    "transStatus" to transportStatus.value,
                    "carId" to car.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

    fun onFilterClick() {
        filterModel.show()
    }


}