package com.lukouguoji.gjc.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityIntExpTallyBinding
import com.lukouguoji.gjc.viewModel.IntExpTallyViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
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.router.ARouterConstants

/**
 * 国际出港-出港理货
 */
@Route(path = ARouterConstants.ACTIVITY_URL_INT_EXP_TALLY)
class IntExpTallyActivity :
    BaseBindingActivity<ActivityIntExpTallyBinding, IntExpTallyViewModel>() {

    override fun layoutId() = R.layout.activity_int_exp_tally
    override fun viewModelClass() = IntExpTallyViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国际出港理货")
        binding.viewModel = viewModel

        // 绑定分页
        viewModel.pageModel.bindSmartRefreshLayout(binding.srl, binding.rv, viewModel, this)

        // 设置item点击监听
        binding.rv.addOnItemClickListener(viewModel)

        // 监听刷新事件
        FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).observe(this) { viewModel.onItemCheckChanged() }

        FlowBus.with<String>(ConstantEvent.EVENT_REFRESH).observe(this) {
            viewModel.refresh()
        }

        // 初始加载数据
        viewModel.refresh()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val codedContent = data?.getStringExtra(Constant.Result.CODED_CONTENT) ?: return
            when (requestCode) {
                Constant.RequestCode.WAYBILL -> {  // 运单号
                    viewModel.waybillNo.value = codedContent
                    viewModel.searchClick()
                }
                Constant.RequestCode.CODE -> {     // 分单号
                    viewModel.houseWaybillNo.value = codedContent
                    viewModel.searchClick()
                }
            }
        }
    }
}
