package com.sixin.module_p.ui.goods.transportLog.dialog

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogPdaTransportLogFilterBinding
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaTransportLogFilterDialogModel(
    private val callBack: () -> Unit
) : BaseDialogModel<DialogPdaTransportLogFilterBinding>(DIALOG_TYPE_DRAWER) {

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

    val flightNo = MutableLiveData("")

    val transportType = MutableLiveData("")
    val transportStatus = MutableLiveData("")
    val driver = MutableLiveData("")

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

    override fun layoutId(): Int {
        return R.layout.dialog_pda_transport_log_filter
    }

    override fun onDialogCreated(context: Context) {
        binding.model = this
    }

    override fun onBuild(builder: XPopup.Builder) {
        builder.popupPosition(PopupPosition.Right)
    }

    fun onDateClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), date.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            date.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }

    fun onTransportTypeSelected(position: Int) {
        transportType.value = transportTypeList.value?.getOrNull(position)?.value.noNull()
    }




    fun onTransportStatusSelected(position: Int) {
        transportStatus.value = transportStatusList.value?.getOrNull(position)?.value.noNull()
    }

    fun onDriverSelected(position: Int) {
        driver.value = driverList.value?.getOrNull(position)?.value.noNull()
    }

    fun onResetClick() {
        date.value = DateUtils.getCurrentTime().formatDate()
        flightNo.value = ""
        callBack.invoke()
        binding.spinnerType.setSelection(0)
        binding.spinnerStatus.setSelection(0)
        binding.spinnerDriver.setSelection(0)
        dismiss()
    }

    fun onConfirmClick() {
        callBack.invoke()
        dismiss()
    }
}