package com.lukouguoji.gjj.viewModel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.activity.GjjGoodsAddActivity
import com.lukouguoji.gjj.activity.GjjGoodsManifestActivity
import com.lukouguoji.gjj.holder.GjjGoodsListViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjjGoodsBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.interfaces.IOnItemClickListener
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.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.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import java.util.Calendar
import java.util.Date

class GjjGoodsListViewModel : BasePageViewModel(), IOnItemClickListener {

    // 交接开始日期
    val startDate = MutableLiveData(Date().formatDate())

    // 交接结束日期
    val endDate = MutableLiveData(Date().formatDate())

    // 交接单号
    val handoverNo = MutableLiveData<String>()

    // 代理
    val agent = MutableLiveData<String>("")

    // 代理列表
    val agentList = MutableLiveData<List<KeyValue>>(emptyList())

    val itemId = R.layout.item_gjj_goods
    val itemHolder = GjjGoodsListViewHolder::class.java

    // 数量
    val count = MutableLiveData(0)

    init {
        DictUtils.getAgentList {
            agentList.postValue(it)
        }
    }

    override fun getData() {
        launchCollect({
            NetApply.api
                .getGjjGoodsList(
                    mapOf(
                        "handtimeStart" to startDate.value,
                        "handtimeEnd" to endDate.value,
                        "movID" to handoverNo.value,
                        "agent" to agent.value,
                    ).toRequestBody()
                )
        }) {
            onSuccess = {
                if (it.verifySuccess()) {
                    pageModel.handleDataList(it.data)
                    count.value = it.data?.size ?: 0
                } else {
                    showToast(it.msg.noNull("获取数据失败"))
                }
            }
        }
    }

    /**
     * 交接开始日期选择
     */
    fun startDateSelect(view: View) {
        Common.onYearMonthDay(view.context.getActivity(), startDate.value!!) { y, m, d ->
            val calendar = Calendar.getInstance()
            calendar.set(y, m - 1, d)
            startDate.value = calendar.time.formatDate()
        }
    }

    /**
     * 交接结束日期选择
     */
    fun endDateSelect(view: View) {
        Common.onYearMonthDay(view.context.getActivity(), endDate.value!!) { y, m, d ->
            val calendar = Calendar.getInstance()
            calendar.set(y, m - 1, d)
            endDate.value = calendar.time.formatDate()
        }
    }

    /**
     * 交接单号扫描
     */
    fun handoverNoScan(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.CODE)
    }

    /**
     * 代理点击
     */
    fun agentSelected(position: Int) {
        agent.value = agentList.value!![position].value
    }

    /**
     * 查询
     */
    fun searchClick(view: View) {
        refresh()
    }

    /**
     * 添加 点击
     */
    fun addClick(view: View) {
        GjjGoodsAddActivity.start(view.context)
    }

    /**
     * 条目点击事件
     */
    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv!!.commonAdapter()!!.getItem(position) as GjjGoodsBean
        when (type) {
            // 详情
            R.id.tv_details -> {
                GjjGoodsManifestActivity.start(DevUtils.getTopActivity(), bean.movID.noNull())
            }

            else -> {
                showToast("未知点击类型")
            }
        }
    }
}