package com.lukouguoji.gjj.viewModel

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.bean.TelegramBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.showToast

/**
 * 国际进港电报详情 ViewModel
 */
class IntArrTelegramDetailsViewModel : BaseViewModel() {

    var id = ""
    var flow = ""

    // 电报内容
    val telegramContent = MutableLiveData<String>("")

    /**
     * 初始化
     */
    fun initOnCreated(intent: Intent) {
        id = intent.getStringExtra(Constant.Key.ID) ?: ""
        flow = intent.getStringExtra(Constant.Key.FLOW) ?: ""
        if (id.isNotEmpty() && flow.isNotEmpty()) {
            getData()
        }
    }

    /**
     * 获取电报详情
     */
    private fun getData() {
        launchLoadingCollect({ NetApply.api.getTelegramDetails(flow, id) }) {
            onSuccess = { result ->
                val data = result.data
                if (data is TelegramBean) {
                    telegramContent.value = data.teleContent
                } else if (data is Map<*, *>) {
                    telegramContent.value = data["teleContent"]?.toString() ?: ""
                }
            }
        }
    }

    /**
     * 修改报文按钮点击
     */
    fun onModifyClick() {
        showToast("修改报文功能待实现")
    }
}
