package com.lukouguoji.gnc.page.assemble.list

import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.page.assemble.add.GncAssembleAddActivity
import com.lukouguoji.gnc.page.assemble.unload.GncAssembleUnloadListActivity
import com.lukouguoji.gnc.page.fubang.list.GncFuBangListViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.base.BaseViewModel
import com.lukouguoji.module_base.bean.GjcBoxAssembleBean
import com.lukouguoji.module_base.bean.GncAssembleListBean
import com.lukouguoji.module_base.bean.GoodsTransportBean
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.getActivity
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.noNull
import com.lukouguoji.module_base.ktx.showConfirmDialog
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toJson
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ConfirmDialogModel
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.request.GncAssembleListRequest
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class GncAssembleListViewModel : BasePageViewModel() {

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

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

    // 平板车
    val car = MutableLiveData("")

    // 复磅状态
    val status = MutableLiveData("")

    // 截载状态
    val cutStatus = MutableLiveData("")

    // 复磅状态列表
    val statusList = DictUtils.reWeightStatusList

    // 截载状态列表
    val cutStatusList = listOf(
        KeyValue("全部", ""),
        KeyValue("未截载", "0"),
        KeyValue("已截载", "1"),
    )

    val count = MutableLiveData(0)

    val itemLayoutId = R.layout.item_gnc_assemble_list
    val itemViewHolder = GncAssembleListViewHolder::class.java

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

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGncAssembleList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to flightNo.value,
                    "carId" to car.value,
                    "copStatus" to status.value,
                    "activited" to cutStatus.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
            }
        }
    }

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

    fun onAddClick() {
        GncAssembleAddActivity.start(getTopActivity())
    }

    fun onDeleteClick() {
        val list =
            (pageModel.rv?.commonAdapter()?.items?.filter { (it as GncAssembleListBean).checked.get() }
                ?: emptyList()) as List<GncAssembleListBean>
        if (list.isEmpty()) {
            showToast("请选择需要删除的数据")
            return
        }
        getTopActivity().showConfirmDialog("是否确认删除选中数据?") {
            delete(list.map { it.useId })
        }
    }

    private fun delete(list: List<String>) {
        launchLoadingCollect({
            NetApply.api.anyPost("DomExpLoading/delete", list.toRequestBody())
        }) {
            onSuccess = {
                showToast(it.msg.noNull("删除成功"))
                refresh()
            }
        }
    }

    fun onAllSelectClick() {
        val allChecked =
            pageModel.rv?.commonAdapter()?.items
                ?.find { item -> !(item as GncAssembleListBean).checked.get() } == null
        pageModel.rv?.commonAdapter()?.items?.forEach {
            (it as GncAssembleListBean).checked.set(!allChecked)
        }
    }

    fun onUnloadClick() {
        GncAssembleUnloadListActivity.start(getTopActivity(), false)
    }

    /**
     * item 点击
     */
    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as GncAssembleListBean
        when (type) {
            R.id.tv_delete -> {
                ConfirmDialogModel("是否删除该条记录？") {
                    delete(listOf(bean.useId))
                }.show()
            }

            R.id.tv_modify -> {
                GncAssembleAddActivity.start(getTopActivity(), bean)
            }

            else -> {}
        }
    }

    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).noNull()
                }

                else -> {}
            }
        }
    }
}