package com.sixin.module_p.ui.gnj.xieji.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.getLifecycleOwner
import com.sixin.module_p.R
import com.sixin.module_p.base.BasePdaActivity
import com.sixin.module_p.databinding.ActivityGnjXieJiListBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class PdaGnjXieJiListActivity :
    BasePdaActivity<ActivityGnjXieJiListBinding, PdaGnjXieJiListViewModel>() {

    override fun layoutId(): Int {
        return R.layout.activity_gnj_xie_ji_list
    }

    override fun viewModelClass(): Class<PdaGnjXieJiListViewModel> {
        return PdaGnjXieJiListViewModel::class.java
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国内进港卸机")
        binding.viewModel = viewModel

        viewModel.pageModel.bindSmartRefreshLayout(
            binding.srl,
            binding.rv,
            viewModel,
            getLifecycleOwner()
        )
        binding.rv.addOnItemClickListener(viewModel)

        FlowBus.with<String>(ConstantEvent.SCAN_CODE_RESULT)
            .onEach {
                // 仅当前页面在活跃时监听
                if (lifecycle.currentState == Lifecycle.State.RESUMED) {
                    viewModel.car.value = it
                    viewModel.onCarChange(false)
                }
            }.launchIn(lifecycleScope)
    }

    override fun onResume() {
        super.onResume()
        viewModel.refresh()
    }

    companion object {
        @JvmStatic
        fun start(context: Context) {
            val starter = Intent(context, PdaGnjXieJiListActivity::class.java)
            context.startActivity(starter)
        }
    }
}