package com.lukouguoji.gjc.activity

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcBoxAssembleAddBinding
import com.lukouguoji.gjc.holder.BoxAddListViewHolder
import com.lukouguoji.gjc.viewModel.GjcBoxAssembleAddViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GjcWaybillBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.impl.observe
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.noNull

class GjcBoxAssembleAddActivity :
    BaseBindingActivity<ActivityGjcBoxAssembleAddBinding, GjcBoxAssembleAddViewModel>() {

    override fun layoutId() = R.layout.activity_gjc_box_assemble_add

    override fun viewModelClass() = GjcBoxAssembleAddViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国际出港开始组装")
        val locId = intent.getStringExtra(Constant.Key.DATA)
        // 获取传入的数据
        viewModel.insertId = locId.noNull()
        viewModel.isAdd.value = locId.isNullOrEmpty()

        binding.viewModel = viewModel

        binding.rv.adapter = CommonAdapter(
            requireContext(),
            R.layout.item_box_add,
            BoxAddListViewHolder::class.java
        )
        binding.rv.addOnItemClickListener(viewModel)

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

        viewModel.getData()

        // 监听待装运单选中事件
        FlowBus.with<List<GjcWaybillBean>>(ConstantEvent.GJC_UNLOAD_LIST_CHECKED)
            .observe(this) {
                viewModel.onWaybillListSelected(requireContext(), it)
            }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, locId: String = "") {
            val starter = Intent(context, GjcBoxAssembleAddActivity::class.java)
                .putExtra(Constant.Key.DATA, locId)
            context.startActivity(starter)
        }
    }
}