package com.lukouguoji.gnj.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.gnj.R
import com.lukouguoji.gnj.adapt.GnjWareHouseInfoListAdapter
import com.lukouguoji.gnj.model.GnjWareHouseInfo
import com.lukouguoji.gnj.viewModel.GnjWareHouseInfoViewModel
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_GNJ_WARE_HOUSE_INFO)
class GnjWareHouseInfoActivity : BaseActivity(), View.OnClickListener {
    private lateinit var viewModel: GnjWareHouseInfoViewModel

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

    @JvmField
    @Autowired
    var id: Int = -9999

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

        scrollView = findViewById(R.id.scrollView)

        val layoutManager = GridLayoutManager(this, 2)
        adapter = GnjWareHouseInfoListAdapter(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_gnj_ware_house_info)
        setBackArrow("仓库管理详情")
        initView()

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

            } else {
                //无数据
            }
        }

    }

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