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.ActivityIntImpPickUpRecordBinding
import com.lukouguoji.gjj.viewModel.IntImpPickUpRecordViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.bean.IntImpPickUpRecordBean
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.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.model.ConfirmDialogModel
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 国际进港-提取记录
 */
@Route(path = ARouterConstants.ACTIVITY_URL_INT_IMP_PICK_UP_RECORD)
class IntImpPickUpRecordActivity :
    BaseBindingActivity<ActivityIntImpPickUpRecordBinding, IntImpPickUpRecordViewModel>() {

    override fun layoutId() = R.layout.activity_int_imp_pick_up_record
    override fun viewModelClass() = IntImpPickUpRecordViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国际进港提取记录")
        binding.viewModel = viewModel
        binding.activity = this

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

        // 设置列表项点击回调（侧滑修改按钮）
        binding.rv.addOnItemClickListener(object : IOnItemClickListener {
            override fun onItemClick(position: Int, type: Int) {
                when (type) {
                    2000 -> {
                        // 侧滑修改操作
                        val list = viewModel.pageModel.rv?.commonAdapter()?.items as? List<*> ?: return
                        val bean = list.getOrNull(position) as? IntImpPickUpRecordBean ?: return
                        IntImpPickUpChargeEditActivity.start(this@IntImpPickUpRecordActivity, bean)
                    }
                }
            }
        })

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

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

        // 运单号自动查询额外参数
        binding.pslWbNo.autoQueryConfig.extraParamsProvider = {
            mapOf(
                "beginDate" to viewModel.paymentDateStart.value,
                "endDate" to viewModel.paymentDateEnd.value,
                "agentCode" to viewModel.agentCode.value,
                "spCode" to viewModel.spCode.value
            )
        }

        // 初始化下拉列表
        viewModel.initAgentList()
        viewModel.initSpecialCodeList()

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

    /**
     * 清除提货操作
     */
    fun clearPickUp() {
        val list = viewModel.pageModel.rv?.commonAdapter()?.items as? List<*> ?: return
        val allItems = list.filterIsInstance<IntImpPickUpRecordBean>()

        val selectedItems = allItems.filter { it.isSelected }

        if (selectedItems.isEmpty()) {
            showToast("请选择要清除提货的记录")
            return
        }

        ConfirmDialogModel(
            message = "确定要清除选中的 ${selectedItems.size} 条提货记录吗？",
            title = "清除提货确认"
        ) {
            viewModel.clearPickUp(selectedItems)
        }.show(this)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Constant.RequestCode.WAYBILL && resultCode == Activity.RESULT_OK) {
            viewModel.wbNo.value = data?.getStringExtra(Constant.Result.CODED_CONTENT)
            viewModel.searchClick()
        }
    }
}
