package com.sixin.module_p.ui.goods.transportLog.list

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.bean.GncCunFangBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.launchLoadingCollect
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.sixin.module_p.R
import com.sixin.module_p.ui.gnc.cunfang.add.PdaGncCunFangAddActivity
import com.sixin.module_p.ui.gnc.cunfang.dialog.PdaCunFangFilterDialogModel
import com.sixin.module_p.ui.goods.transportLog.dialog.PdaTransportLogFilterDialogModel
import dev.DevUtils
import dev.utils.common.RandomUtils
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class PdaTransportLogViewModel : BasePageViewModel() {

    val car = MutableLiveData("")

    val index = MutableLiveData(0)

    val count = MutableLiveData(0)

    val itemLayoutId = R.layout.item_pda_transport_log_list
    val itemViewHolder = PdaTransportLogViewHolder::class.java

    private val filterModel = PdaTransportLogFilterDialogModel{
        refresh()
    }

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

    fun initOnCreated(intent: Intent) {
        FlowBus.with<String>(ConstantEvent.SCAN_CODE_RESULT)
            .onEach {
                car.value = it
                onCarChange()
            }.launchIn(viewModelScope)
    }

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getTransportLogList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "fdate" to filterModel.date.value,
                    "fno" to filterModel.flightNo.value,
                    "opId" to filterModel.driver.value,
                    "transType" to filterModel.transportType.value,
                    "transStatus" to filterModel.transportStatus.value,
                    "carId" to car.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
            onComplete = {
                pageModel.finishLoading()
            }
        }
    }


    /**
     * 筛选点击
     */
    fun onFilterClick() {
        filterModel.show()
    }

    fun onCarChange(focused: Boolean = false) {
        if (focused) {
            return
        }
        refresh()
    }


    fun scanClick() {
        ScanModel.startScan(getTopActivity(), Constant.RequestCode.CAR)
    }

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

            else -> {}
        }
    }

}