package com.lukouguoji.gjj.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.InputType
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityGjjManifestAddBinding
import com.lukouguoji.gjj.viewModel.GjjManifestAddViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.DetailsPageType
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.setUpperCaseAlphanumericFilter

class GjjManifestAddActivity :
    BaseBindingActivity<ActivityGjjManifestAddBinding, GjjManifestAddViewModel>() {

    override fun layoutId() = R.layout.activity_gjj_manifest_add

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

    override fun initOnCreate(savedInstanceState: Bundle?) {
        binding.viewModel = viewModel
        binding.flightNoInput.et.setUpperCaseAlphanumericFilter()

        // 件数只允许输入整数
        binding.waybillNumInput.inputType = InputType.TYPE_CLASS_NUMBER
        binding.actualNumInput.inputType = InputType.TYPE_CLASS_NUMBER
        // 重量允许输入小数
        binding.actualWeightInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
        binding.billingWeightInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL

        viewModel.initOnCreated(intent)

        // 动态设置标题（必须在 initOnCreated 之后，pageType 已从 Intent 解析）
        val title = when (viewModel.pageType.value) {
            DetailsPageType.Modify -> "国际进港舱单编辑"
            else -> "国际进港舱单新增"
        }
        setBackArrow(title)
    }

    companion object {
        /**
         * 新增舱单
         */
        @JvmStatic
        fun start(
            context: Context,
            fid: String = "",
            dep: String,
            dest: String,
            flightDate: String = "",
            flightNo: String = ""
        ) {
            val starter = Intent(context, GjjManifestAddActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Add.name)
                .putExtra(Constant.Key.ID, fid)
                .putExtra(Constant.Key.DEPARTURE, dep)
                .putExtra(Constant.Key.DESTINATION, dest)
                .putExtra("flightDate", flightDate)
                .putExtra("flightNo", flightNo)
            context.startActivity(starter)
        }

        /**
         * 编辑舱单（通过Bean对象）
         */
        @JvmStatic
        fun startForEdit(
            context: Context,
            bean: com.lukouguoji.module_base.bean.GjjManifest,
            flightDate: String = "",
            flightNo: String = ""
        ) {
            val starter = Intent(context, GjjManifestAddActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Modify.name)
                .putExtra(Constant.Key.BEAN, bean)
                .putExtra("flightDate", flightDate)
                .putExtra("flightNo", flightNo)
            context.startActivity(starter)
        }

        /**
         * 编辑舱单（通过GjjImportManifest对象）
         */
        @JvmStatic
        fun startForEdit(context: Context, bean: com.lukouguoji.module_base.bean.GjjImportManifest) {
            val starter = Intent(context, GjjManifestAddActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Modify.name)
                .putExtra(Constant.Key.BEAN, bean)
            context.startActivity(starter)
        }
    }
}