package com.lukouguoji.gnc.page.deposit.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.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GncCunFangBean
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.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.lukouguoji.module_base.util.CheckUtil
import com.lukouguoji.module_base.util.DictUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class GncDepositListViewModel : BasePageViewModel() {

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

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

    // 存放区域
    val depositArea = MutableLiveData("")

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

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

    // 存放区域列表
    val depositAreaList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getAreaList {
            value = listOf(KeyValue("全部", "")) + it
        }
    }

    // 复磅状态列表
    val reweighStatusList = MutableLiveData<List<KeyValue>>(
        DictUtils.reWeightStatusList
    )

    val itemLayoutId = R.layout.item_gnc_deposit_list
    val itemViewHolder = GncDepositListViewHolder::class.java

    val count = MutableLiveData("0")
    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGncCunFangList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "fdate" to date.value,
                    "fno" to flightNo.value,
                    "storageArea" to depositArea.value,
                    "copStatus" to reweighStatus.value,
                    "carId" to car.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total.toString()
            }
        }
    }

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

    fun checkAllClick(view: View) {
        CheckUtil.handleAllCheck(pageModel.rv?.commonAdapter()?.items as List<ICheck>)
    }

    fun onConfirmClick() {
        val list = pageModel.rv!!.commonAdapter()!!.items as List<GncCunFangBean>
        val filter = list.filter { it.checked.get() }
        if (filter.isEmpty()) {
            showToast("请选择数据")
            return
        }

        GncDepositDialogModel(
            filter.joinToString("、") { it.carId },
            depositAreaList.value!!
        ) {
            launchLoadingCollect({
                NetApply.api.anyPost(
                    "DomExpLoading/allocationArea", mapOf(
                        "area" to it.area.value,
                        "useIdList" to filter.map { b -> b.useId },
                    ).toRequestBody()
                )
            }) {
                onSuccess = {
                    showToast(it.msg.noNull("分配区域成功"))
                    refresh()
                }
            }
        }.show(getTopActivity())
    }

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

}