package com.lukouguoji.gjj.activity

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.core.widget.NestedScrollView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.adapt.GjjWareHouseInfoListAdapter
import com.lukouguoji.gjj.model.GjjWareHouseInfo
import com.lukouguoji.gjj.viewModel.GjjWareHouseInfoViewModel
import com.lukouguoji.module_base.BaseActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants

@Route(path = ARouterConstants.ACTIVITY_URL_GJJ_WARE_HOUSE_INFO)
class GjjWareHouseInfoActivity : BaseActivity(), View.OnClickListener {
    private lateinit var viewModel: GjjWareHouseInfoViewModel

    private lateinit var scrollView: NestedScrollView
    private lateinit var gncQueryInfoList: RecyclerView
    private lateinit var adapter: GjjWareHouseInfoListAdapter
    private val queryInfoAdapterList = arrayListOf<GjjWareHouseInfo>()

    @JvmField
    @Autowired
    var id: Int = -9999

    private fun initView() {
        viewModel = ViewModelProvider(this).get(GjjWareHouseInfoViewModel::class.java)

        scrollView = findViewById(R.id.scrollView)

        val layoutManager = GridLayoutManager(this, 2)
        adapter = GjjWareHouseInfoListAdapter(this, queryInfoAdapterList)
        gncQueryInfoList = findViewById(R.id.gncQueryInfoList)
        gncQueryInfoList.adapter = adapter
        gncQueryInfoList.layoutManager = layoutManager

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_gjj_ware_house_info)
        setBackArrow("仓库管理详情")
        initView()

        viewModel.queryInfoByIdWareHouse(id)
        viewModel.queryInfoByIdWareHouseObserver.observe(this) {
            val status = it.getString("status")
            if (Constant.Result.succ == status) {
                viewModel.wayBillInfo = it.getJSONObject("data")
                //运单号、
                adapter.append(GjjWareHouseInfo(1, "运单号", viewModel.wayBillInfo.getString("wbNo") ?: ""))
                // 件数、
                adapter.append(GjjWareHouseInfo(1, "件数", viewModel.wayBillInfo.getString("pc") ?: ""))
                // 重量、
                adapter.append(GjjWareHouseInfo(1, "重量", "${viewModel.wayBillInfo.getDoubleValue("weight")} KG"))
                // 品名、
                adapter.append(GjjWareHouseInfo(1, "品名(英)", viewModel.wayBillInfo.getString("goods") ?: ""))
                // 特码、
                adapter.append(GjjWareHouseInfo(1, "特码", viewModel.wayBillInfo.getString("spCode") ?: ""))
                // 代理人、
                adapter.append(GjjWareHouseInfo(1, "代理人", viewModel.wayBillInfo.getString("agentName") ?: ""))
                // 承运人、
                adapter.append(GjjWareHouseInfo(1, "承运人", viewModel.wayBillInfo.getString("carrier") ?: ""))
                //始发港、
                adapter.append(GjjWareHouseInfo(1, "目的港", viewModel.wayBillInfo.getString("dest") ?: ""))
                // 航班、
                adapter.append(GjjWareHouseInfo(1, "航班", viewModel.wayBillInfo.getString("flight") ?: ""))
                // 库位
//                adapter.append(GjjWareHouseInfo(1, "库位", viewModel.wayBillInfo.getString("location") ?: ""))
                // 运单类型、
                adapter.append(GjjWareHouseInfo(1, "运单类型", viewModel.wayBillInfo.getString("awbType") ?: ""))
                // 业务类型、
                adapter.append(GjjWareHouseInfo(1, "业务类型", viewModel.wayBillInfo.getString("businessType") ?: ""))
                // 包装类型
                adapter.append(GjjWareHouseInfo(1, "包装类型", viewModel.wayBillInfo.getString("packageName") ?: ""))
                // 状态、
//                adapter.append(GjjWareHouseInfo(1, "状态", viewModel.wayBillInfo.getString("ref") ?: ""))
                // 入库时间
                adapter.append(GjjWareHouseInfo(1, "入库时间", viewModel.wayBillInfo.getString("opDate") ?: ""))

            } else {
                //无数据
            }
        }

    }

    override fun onClick(v: View?) {
        v?.let {
            when (it.id) {
                else -> {
                    Toast.makeText(this, "未找到对应操作", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}