package com.grkj.iscs.view.presenter

import com.grkj.iscs.R
import com.grkj.iscs.model.vo.machinery.MachineryPageRespVO
import com.grkj.iscs.util.Executor
import com.grkj.iscs.util.NetApi
import com.grkj.iscs.util.ToastUtils
import com.grkj.iscs.view.base.BasePresenter
import com.grkj.iscs.view.iview.ITechnologySopView

class TechnologySopPresenter : BasePresenter<ITechnologySopView>() {

    fun getMachineryPage(workstationId: Long, callBack: (MachineryPageRespVO?) -> Unit) {
        NetApi.getMachineryPage(0, 100, workstationId) {
            Executor.runOnMain {
                callBack(it)
            }
        }
    }

    /**
     * 创建工作票
     */
    fun createTicket(
        machineryIdx: Int,
        sopTypeIdx: Int,
        list: MutableList<MachineryPageRespVO.Record>,
        callBack: (Long?) -> Unit
    ) {
        if (machineryIdx == -1) {
            ToastUtils.tip(R.string.please_select_machinery)
            return
        }
        if (sopTypeIdx == -1) {
            ToastUtils.tip(R.string.please_select_sop_type)
            return
        }
        NetApi.getSopPage(
            0,
            100,
            list[machineryIdx].machineryId!!,
            list[machineryIdx].sysDictDatas?.get(sopTypeIdx)?.dictValue!!.toInt()
        ) {
            it ?: return@getSopPage
            NetApi.createTicket(it.records[0].sopId) { ticketId ->
                Executor.runOnMain {
                    callBack(ticketId)
                }
            }
        }
    }
}