package com.sixin.module_p.ui.enter.gn.document

import android.content.Context
import android.widget.Spinner
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.DialogPdaDocumentHandoverFilterBinding
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaDocumentHandoverFilterDialogModel(
        private val callBack: () -> Unit
) : BaseDialogModel<DialogPdaDocumentHandoverFilterBinding>(DIALOG_TYPE_DRAWER) {

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

    val flightNo = MutableLiveData("")

    val docType = MutableLiveData("")

    val docTypeList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getDocumentHandoverTypeList {
            value = listOf(KeyValue("全部", "")) + it
            docType.value = ""
        }
    }

    val reviewStatus = MutableLiveData("")
    val reviewStatusList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        value = listOf(KeyValue("全部", ""), KeyValue("未复核", "0"), KeyValue("已复核", "1"),)
    }

    val ieFlag = MutableLiveData("")

    val ieFlagList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        value = listOf(KeyValue("国内出港", "E"), KeyValue("国内进港", "I"), KeyValue("全部", ""))
        ieFlag.value = "E"
    }

    override fun layoutId(): Int {
        return R.layout.dialog_pda_document_handover_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 onDocTypeSelected(position: Int) {
        docType.value = docTypeList.value?.getOrNull(position)?.value.noNull()
    }

    fun onIEFlagSelected(position: Int) {
        ieFlag.value = ieFlagList.value?.getOrNull(position)?.value.noNull()
    }

    fun onReviewSelected(position: Int) {
        reviewStatus.value = reviewStatusList.value?.getOrNull(position)?.value.noNull()
    }

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

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