package com.lukouguoji.gnj.page.yiku.details

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gnj.R
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.bean.FileBean
import com.lukouguoji.module_base.bean.GnjYiKuBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.ImageSelectViewHolder
import com.lukouguoji.module_base.ktx.launchLoadingCollect

/**
 * 国内进港移库详情 ViewModel
 */
class GnjYiKuDetailsViewModel : BaseViewModel() {

    var id = ""

    val dataBean = MutableLiveData<GnjYiKuBean>()

    // 图片列表
    val imageList = MutableLiveData<MutableList<FileBean>>(mutableListOf())

    // 图片适配器配置
    val imageItemLayoutId = R.layout.item_image_select
    val imageItemViewHolder = ImageSelectViewHolder::class.java

    fun initOnCreated(intent: Intent) {
        id = intent.getStringExtra(Constant.Key.ID) ?: ""
        getData()
    }

    private fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnjYiKuDetails(id)
        }) {
            onSuccess = {
                val bean = it.data ?: GnjYiKuBean()
                dataBean.value = bean

                // 处理图片列表
                val images = bean.getImageList().map { url ->
                    FileBean(path = url)
                }
                imageList.value = images.toMutableList()
            }
        }
    }
}
