package com.lukouguoji.aerologic.page.gnj.unload.list

import android.app.Activity
import android.content.Intent
import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.car.add.CarDetailsActivity
import com.lukouguoji.aerologic.page.car.chart.CarChartActivity
import com.lukouguoji.aerologic.page.gnj.unload.add.GnjUnloadAddActivity
import com.lukouguoji.aerologic.page.gnj.unload.detail.GnjUnloadDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.CarBean
import com.lukouguoji.module_base.bean.DictBean
import com.lukouguoji.module_base.bean.GnjUnloadListBean
import com.lukouguoji.module_base.bean.MessageBean
import com.lukouguoji.module_base.bean.SimpleCheckBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.ICheck
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.logi
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.CheckUtil
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import kotlin.math.log

class GnjUnloadListViewModel : BasePageViewModel() {

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

    // 车号
    val car = MutableLiveData("")

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

    val onlyMe = MutableLiveData(true)


    val itemLayoutId = R.layout.item_gnj_unload_list
    val itemViewHolder = GnjUnloadListViewHolder::class.java

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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnjUnloadList(
                mapOf(
                    "fdate" to date.value,
                    "fno" to fNo.value,
                    "carId" to car.value,
                    "onlyMe" to onlyMe.value
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

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

    fun onAddClick() {
        GnjUnloadAddActivity.start(getTopActivity(), GnjUnloadListBean())
    }

    fun onDeleteClick() {
            val adapter = pageModel.rv!!.commonAdapter()!!
            val filter: List<GnjUnloadListBean> =
                adapter.items.filter { (it as GnjUnloadListBean).checked.get() } as List<GnjUnloadListBean>
            if (filter.isEmpty()) {
                showToast("请选择需要删除的数据")
            } else {
                getTopActivity().showConfirmDialog("是否删除选择的航班下所有平板车信息?") {
                    filter.forEach {
                        adapter.removeItem(adapter.items.indexOf(it))
                    }
                    launchLoadingCollect({
                        NetApply.api.anyPost(
                            "unloadingAirCraft/deleteListByFidList",
                            filter.map { b -> b.fid }.toRequestBody()
                        )
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("删除成功"))
                            refresh()
                        }
                    }
                }
            }
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as GnjUnloadListBean
        Log.i("onItemClick", "${type}")
        when (type) {
            R.id.ll_content -> {
                GnjUnloadDetailsActivity.start(DevUtils.getTopActivity(), bean)
            }
            R.id.tv_modify -> {
                GnjUnloadAddActivity.start(DevUtils.getTopActivity(), bean)
            }
            R.id.tv_delete -> {
                getTopActivity().showConfirmDialog("是否删除该航班下所有平板车信息??") {
                    launchLoadingCollect({
                        NetApply.api.anyGetForId(
                            "unloadingAirCraft/deleteByFid", bean.fid.noNull()
                        )
                    }) {
                        onSuccess = {
                            showToast(it.msg.noNull("删除成功"))
                            adapter.removeItem(position)
                        }
                    }
                }
            }

            else -> {}
        }
    }

    fun onAllSelectClick() {
        val adapter = pageModel.rv!!.commonAdapter()!!
        CheckUtil.handleAllCheck(adapter.items as List<ICheck>)
    }

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

                else -> {}
            }
        }
    }

}