package com.lukouguoji.gnc.page.assemble.add

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.databinding.ActivityGncAssembleAddBinding
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GncAssembleListBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.getLifecycleOwner
import com.lukouguoji.module_base.ktx.toJson

class GncAssembleAddActivity :
    BaseBindingActivity<ActivityGncAssembleAddBinding, GncAssembleAddViewModel>() {

    override fun layoutId() = R.layout.activity_gnc_assemble_add

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

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国内出港开始组装")
        binding.viewModel = viewModel
        viewModel.pageModel.bindSmartRefreshLayout(
            binding.srl,
            binding.rv,
            viewModel,
            getLifecycleOwner()
        )
        viewModel.initOnCreated(intent)

        binding.rv.adapter = CommonAdapter(
            requireContext(),
            R.layout.item_gnc_assemble_add,
            GncAssembleAddViewHolder::class.java
        )

        binding.rv.addOnItemClickListener(viewModel)

        viewModel.waybill.observe(this){
            if (it.length == 11) {
                viewModel.onWaybillChange(true)
            }
            if (it.length >= 4 && it.length < 9) {
                viewModel.getWaybillList(it)
            }
        }

        viewModel.refresh()
    }

    companion object {
        @JvmStatic
        fun start(context: Context, bean: GncAssembleListBean? = null) {
            val starter = Intent(context, GncAssembleAddActivity::class.java)
            if (bean != null) {
                starter.putExtra(Constant.Key.DATA, bean.toJson(false))
            }
            context.startActivity(starter)
        }
    }
}