package com.sixin.module_p.ui.gnj.accidentVisa.list

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.AccidentVisaBean
import com.lukouguoji.module_base.bean.GnjUnloadListBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showConfirmDialog
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 com.sixin.module_p.R
import com.sixin.module_p.ui.gnj.accidentVisa.add.PdaAccidentVisaAddActivity
import com.sixin.module_p.ui.gnj.accidentVisa.dialog.PdaAccidentVisaFilterDialogModel
import com.sixin.module_p.ui.gnj.xieji.add.PdaGnjXieJiAddActivity
import dev.DevUtils
import dev.utils.common.DateUtils

class PdaAccidentVisaListViewModel : BasePageViewModel() {



    // 日期
    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())

    // 航班号
    val flightNo = MutableLiveData("")

    // 发运站
    val dep = MutableLiveData("")

    // 单号
    val order = MutableLiveData("")

    // 填表人
    val name = MutableLiveData("")

    // 合计数量
    val count = MutableLiveData(0)




    val itemLayoutId = R.layout.item_pda_accident_visa_list
    val itemViewHolder = PdaAccidentVisaListViewHolder::class.java

    private val dialogModel = PdaAccidentVisaFilterDialogModel {
        refresh()
    }

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



    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnAccidentVisaList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
//                    "opDate" to dialogModel.date.value,
                    "opDateStart" to dialogModel.opDateStart.value,
                    "opDateEnd" to dialogModel.opDateEnd.value,
                    "fno" to flightNo.value,
                    "dep" to dialogModel.dep.value,
                    "mawbno" to dialogModel.order.value,
                    "opid" to dialogModel.name.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }
    fun onCarChange(focus: Boolean = false) {
        if (focus) {
            return
        }
        getData()
    }

    fun onFilterClick() {
        dialogModel.show()
    }

    fun onAddClick() {
        PdaAccidentVisaAddActivity.start(DevUtils.getTopActivity())
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as AccidentVisaBean
        when (type) {
            R.id.ll_content -> {
                PdaAccidentVisaAddActivity.start(DevUtils.getTopActivity(), bean.id, false)
            }
            R.id.tv_modify -> {
                PdaAccidentVisaAddActivity.start(DevUtils.getTopActivity(), bean.id, true)
            }

            R.id.tv_delete -> {
                Common.secondConfirmDialog(
                    DevUtils.getTopActivity(), "确定删除吗？"
                ) {
                    launchLoadingCollect({
                        NetApply.api.anyPostForId("/GnAccidentVisa/deleteVisa", bean.id)
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("删除成功"))
                            adapter.removeItem(position)
                            count.value = count.value!!.minus(1)

                        }
                    }
                }
            }

            else -> {}
        }
    }

    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 -> {}
        }
    }
}