package com.yzx.escreen.presenter

import com.blankj.utilcode.util.LogUtils
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.yzx.escreen.config.Config
import com.yzx.escreen.model.*
import com.yzx.escreen.presenter.base.*
import com.yzx.escreen.config.YzxInterface

class ChangePresenter(view: ChangeView) : BasePresenter<ChangeView>(view) {


    fun getList(pageNo: Int, keyword: String, status: String) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_GET_CHANGE_LIST}"
        OkGo.post<YzxResponse<StuChange>>(url)
            .tag(this)
            .params("keyword", keyword)
            .params("status", status)
            .params("page_size", 6)
            .params("page_no", pageNo)
            .execute(object : JsonCallBack<YzxResponse<StuChange>>() {


                override fun onSuccess(response: Response<YzxResponse<StuChange>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onListSuccess(response.body().list, response.body().pager)
                    }
                }

                override fun onError(response: Response<YzxResponse<StuChange>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onListError(response?.body()?.msg ?: "")
                }
            })
    }
    fun getDetail(id: Int) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_GET_CHANGE_DETAIL}"
        OkGo.post<YzxResponse<StuChange>>(url)
            .tag(this)
            .params("id", id)
            .execute(object : JsonCallBack<YzxResponse<StuChange>>() {


                override fun onSuccess(response: Response<YzxResponse<StuChange>>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.onDetailSuccess(response.body().data)
                    }
                }

                override fun onError(response: Response<YzxResponse<StuChange>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onDetailError(response?.body()?.msg ?: "")
                }
            })
    }

    

}

interface ChangeView : IView {
    fun onListSuccess(list: MutableList<StuChange>, pager: Pager)
    fun onListError(error: String)
    fun onDetailSuccess(detail: StuChange)
    fun onDetailError(error: String)
}