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

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.EditText
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.lukouguoji.module_base.ktx.setTextAllCaps
import com.sixin.module_p.R
import com.sixin.module_p.base.BasePdaActivity
import com.sixin.module_p.databinding.ActivityPdaAccidentVisaListBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class PdaAccidentVisaListActivity :
    BasePdaActivity<ActivityPdaAccidentVisaListBinding, PdaAccidentVisaListViewModel>() {
    override fun layoutId(): Int {
        return R.layout.activity_pda_accident_visa_list
    }

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

    private lateinit var flightNo: EditText

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国内事故签证")
        binding.viewModel = viewModel

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

        flightNo = findViewById(R.id.flightNoEditText)
        setTextAllCaps(flightNo, true)

        FlowBus.with<String>(ConstantEvent.SCAN_CODE_RESULT)
            .onEach {
                // 仅当前页面在活跃时监听
                if (lifecycle.currentState == Lifecycle.State.RESUMED) {
                    viewModel.flightNo.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, PdaAccidentVisaListActivity::class.java)
            context.startActivity(starter)
        }
    }
}