package com.lukouguoji.gjc.viewModel

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.activity.GjcGoodsAddActivity
import com.lukouguoji.gjc.activity.GjcGoodsManifestActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjcGoodsBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.util.Common
import dev.DevUtils
import java.util.Calendar
import java.util.Date

class GjcGoodsListViewModel : BasePageViewModel(), IOnItemClickListener {

    // 航班日期
    var flightDate = MutableLiveData(Date().formatDate())

    // 航班号
    var flightNo = MutableLiveData<String>()

    // 板车号
    var truckNo = MutableLiveData<String>()

    // ULD编号
    var uldNo = MutableLiveData<String>()

    var count = MutableLiveData(0)

    override fun getData() {
        launchCollect({
            NetApply.api
                .gjcHandoverList(
                    mapOf(
                        "carid" to truckNo.value,
                        "fdate" to flightDate.value,
                        "fno" to flightNo.value,
                        "uld" to uldNo.value,
                    ).toRequestBody()
                )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    pageModel.handleDataList(it.data ?: emptyList())
                    count.postValue(it.data?.size)
                } else {
                    showToast(it.msg.noNull("获取数据失败"))
                    count.postValue(0)
                }
            }
        }
    }


    /**
     * 航班日期点击
     */
    fun flightDateClick(view: View) {
        Common.onYearMonthDay(view.context.getActivity(), flightDate.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            flightDate.value = calendar.time.formatDate()
        }
    }

    /**
     * 板车号 扫描
     */
    fun truckNoScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.CAR)
    }

    /**
     * ULD编号 扫描
     */
    fun uldNoScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.ULD)
    }

    /**
     * 搜索点击
     */
    fun searchClick(view: View) {
        refresh()
    }

    /**
     * 添加点击
     */
    fun addClick(view: View) {
        GjcGoodsAddActivity.start(view.context)
    }

    /**
     * item点击
     */
    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as GjcGoodsBean
        when (type) {
            R.id.tv_details -> {
                GjcGoodsManifestActivity.start(DevUtils.getTopActivity(), bean.movID.noNull())
            }

            else -> {}
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                Constant.RequestCode.CAR -> {
                    val id = data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                    truckNo.value = id
                }

                Constant.RequestCode.ULD -> {
                    val id = data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                    uldNo.value = id
                }

                else -> {}
            }
        }
    }

}