package com.lukouguoji.gjj.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityIntArrAirManifestBinding
import com.lukouguoji.gjj.viewModel.IntArrAirManifestViewModel
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_ARR_AIR_MANIFEST)
class IntArrAirManifestActivity :
    BaseBindingActivity<ActivityIntArrAirManifestBinding, IntArrAirManifestViewModel>() {

    override fun layoutId() = R.layout.activity_int_arr_air_manifest
    override fun viewModelClass() = IntArrAirManifestViewModel::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()
        }

        // 设置运单号自动查询的额外参数（航班日期、航班号）
        binding.pslWaybillNo.autoQueryConfig.extraParamsProvider = {
            mapOf(
                "fdate" to viewModel.flightDate.value,
                "fno" to viewModel.flightNo.value
            )
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                Constant.RequestCode.WAYBILL -> {
                    viewModel.waybillNo.value = data?.getStringExtra(Constant.Result.CODED_CONTENT)
                    viewModel.searchClick()
                }
                Constant.RequestCode.HNO -> {
                    viewModel.houseNo.value = data?.getStringExtra(Constant.Result.CODED_CONTENT)
                    viewModel.searchClick()
                }
            }
        }
    }
}
