package com.sixin.module_p.ui.gnc.cunfang.list

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.ktx.verifyNullOrEmpty
import com.lukouguoji.module_base.util.DictUtils
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogPdaGncCunFangAddBinding
import dev.utils.app.info.KeyValue

class PdaGncCunFangAddDialogModel(
    val carList: String,
    val callBack: (PdaGncCunFangAddDialogModel) -> Unit
) :
    BaseDialogModel<DialogPdaGncCunFangAddBinding>(DIALOG_TYPE_FULL) {

    val area = MutableLiveData("")

    val areaList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getAreaList {
            value = it
        }
    }

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

    override fun layoutId() = R.layout.dialog_pda_gnc_cun_fang_add

    override fun onDialogCreated(context: Context) {
        setBackArrow("分配存放区域")
        binding.model = this
    }

    private fun setBackArrow(title: String) {
        val toolbar: Toolbar = binding.root.findViewById(com.lukouguoji.module_base.R.id.toolbar)
        val titleName: TextView = binding.root.findViewById(com.lukouguoji.module_base.R.id.title_name)
        titleName.text = title
        val toolBack: View = binding.root.findViewById(com.lukouguoji.module_base.R.id.tool_back)
        toolBack.setOnClickListener {
            dismiss()
        }
    }

    fun onAreaSelected(position: Int) {
        area.value = areaList.value?.getOrNull(position)?.value ?: ""
    }

    fun onConfirmClick() {
        if (area.value.verifyNullOrEmpty("请选择区域")) {
            return
        }
        callBack.invoke(this)
        dismiss()
    }

}