package com.lukouguoji.gnj.page.cangdan.add

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.util.Common

class GnjCangDanAddViewModel : BasePageViewModel() {


    // 运单号
    val waybillCode = MutableLiveData<String>()

    //代理人
    var agentCodeList = JSONArray()
    var agentCode = MutableLiveData<String>()
    val agent = MutableLiveData<String>()

    //特码
    var spCodeList = JSONArray()
    var spCodeValue = MutableLiveData<String>()
    val spCode = MutableLiveData<String>()

    //运单件数
    val waybillCount = MutableLiveData("")

    //实到件数
    val actualCount = MutableLiveData("")

    //实到重量
    val actualWeight = MutableLiveData("")

    //计费重量
    val chargingWeight = MutableLiveData("")

    //始发港
    val origin = MutableLiveData("")

    //目的港
    val dest = MutableLiveData("")

    //运单类型
    val waybillTypeList = JSONArray()
    val waybillTypeCode = MutableLiveData<String>()
    val waybillType = MutableLiveData<String>()

    //业务类型
    val businessTypeList = JSONArray()
    val businessTypeCode = MutableLiveData<String>()
    val businessType = MutableLiveData<String>()

    //危险品库位
    val dgrLocationList = JSONArray()
    val dgrLocationCode = MutableLiveData<String>()
    val dgrLocation = MutableLiveData<String>()

    //危险品描述
    val dgrDetailList = JSONArray()
    val dgrDetailCode = MutableLiveData<String>()
    val dgrDetail = MutableLiveData<String>()

    //品名
    val goods = MutableLiveData<String>()

    //备注
    val remark = MutableLiveData<String>()

    //un编号
    val unCode = MutableLiveData<String>()

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////


    fun onSave(focused: Boolean = false) {


    }

    fun onCancel() {

    }

    override fun getData() {
        pageModel.handleDataList((0..3).toList())
    }

    fun onAgentCode(view: View) {
        Common.singleSelect(view.context.getActivity(), "代理", agentCodeList, agentCode.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            agent.value = name
            agentCode.value = code
        }
    }

    fun onSpcode(view: View) {
        Common.singleSelect(view.context.getActivity(), "特码", spCodeList, spCodeValue.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            spCode.value = name
            spCodeValue.value = code
        }
    }

    fun onWaybillType(view: View) {
        Common.singleSelect(view.context.getActivity(), "运单类型", waybillTypeList, waybillTypeCode.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            waybillType.value = name
            waybillTypeCode.value = code
        }
    }

    fun onBusinessType(view: View) {
        Common.singleSelect(view.context.getActivity(), "运单类型", businessTypeList, businessTypeCode.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            businessType.value = name
            businessTypeCode.value = code
        }
    }

    fun onDgrLocation(view: View) {
        Common.singleSelect(view.context.getActivity(), "运单类型", dgrLocationList, dgrLocationCode.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            dgrLocation.value = name
            dgrLocationCode.value = code
        }
    }

    fun onDgrDetail(view: View) {
        Common.singleSelect(view.context.getActivity(), "运单类型", dgrDetailList, dgrDetailCode.value) { _, item ->
            val jsonObject = item as JSONObject
            val name = jsonObject.getString("name")
            val code = jsonObject.getString("code")
            dgrDetail.value = name
            dgrDetailCode.value = code
        }
    }


}