package com.sixin.module_p.ui.gnc.shouyun.un.list

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
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.ActivityGncUnShouYunBinding

class GncUnShouYunActivity :
    BasePdaActivity<ActivityGncUnShouYunBinding, GncUnShouYunViewModel>() {

    private lateinit var editText: EditText
    private var mTextWatcher: TextWatcher? = null


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

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

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国内出港待收运")
        binding.viewModel = viewModel

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

        binding.rv.addOnItemClickListener(viewModel)

        editText = findViewById(R.id.editText)

        mTextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable?) {
                val input = s?.toString()?.trim()
                if (input != null && input.length >= 4 && input.length < 8) {
                    viewModel.getWayBillList(input)
                }
            }
        }
        editText.addTextChangedListener(mTextWatcher)

    }


    override fun onResume() {
        super.onResume()
        viewModel.refresh()
    }
    override fun onStop(){
        super.onStop()
        // 安全移除（自动处理 null 检查）
        mTextWatcher?.let { editText.removeTextChangedListener(it) }
        mTextWatcher = null // 帮助 GC 回收

    }

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

}