package com.sixin.module_p.ui.gnj.accidentVisa.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.util.Common
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogAccidentVisaFilterBinding
import com.sixin.module_p.databinding.DialogPdaGnjXiejiFilterBinding
import dev.DevUtils
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaAccidentVisaFilterDialogModel(
    private val callBack: () -> Unit
) : BaseDialogModel<DialogAccidentVisaFilterBinding>(
    DIALOG_TYPE_DRAWER
) {

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

    val flightNo = MutableLiveData("")

    // 发运站
    val dep = MutableLiveData("")

    // 单号
    val order = MutableLiveData("")

    // 填表人
    val name = MutableLiveData("")

    override fun layoutId(): Int {
        return R.layout.dialog_accident_visa_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 onStartDateClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), date.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            opDateStart.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }

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

    fun onResetClick() {
        date.value = DateUtils.getCurrentTime().formatDate()
        opDateStart.value = DateUtils.getCurrentTime().formatDate()
        opDateEnd.value = DateUtils.getCurrentTime().formatDate()
        flightNo.value = ""
        name.value = ""
        order.value = ""
        dep.value = ""
        callBack.invoke()
        dismiss()
    }

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