package com.yzx.ebrand.presenter

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

class HomePresenter(view: HomeView) : BasePresenter<HomeView>(view) {


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


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

                override fun onError(response: Response<YzxResponse<StuLeave>>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.onLeaveListError(response?.body()?.msg ?: "")
                }
            })
    }
    fun getLeaveShowIds() {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_GET_SHOW_BOX_LIST}"
        OkGo.post<YzxResponse<LeaveID>>(url)
            .tag(this)
            .execute(object : JsonCallBack<YzxResponse<LeaveID>>() {
                override fun onSuccess(response: Response<YzxResponse<LeaveID>>?) {
                    if (response?.isSuccessful == true) {
                        mView?.onLeaveIDsSuccess(response.body().list)
                        LogUtils.d("getLeaveShowIds",response.body().list)
                    }
                }

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

    fun getDetail(id: Int) {
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_GET_STU_LEAVE_INFO_DETAIL}"
        OkGo.post<YzxResponse<StuLeave>>(url)
            .tag(this)
            .params("id", id)
            .execute(object : JsonCallBack<YzxResponse<StuLeave>>() {


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

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

    fun postVacation(data:Vacation){
        if(User.getUser().token.isEmpty()){
            return
        }
        val url = "${Config.BASE_URL}${YzxInterface.INTERFACE_STUDENT_POST_LEAVE_VACATION}"
        OkGo.post<SimpleResponse>(url)
            .tag(this)
            .params("id", data.id)
            .params("vacation_type", data.vacation_type)
            .params("vacation_time", data.vacation_time)
            .params("vacation_remark", data.vacation_remark)
            .params("vacation_attachment", data.vacation_attachment)
            .execute(object : JsonCallBack<SimpleResponse>() {


                override fun onSuccess(response: Response<SimpleResponse>?) {
                    LogUtils.d(response)
                    if (response?.isSuccessful == true) {
                        mView?.postVacationSuccess("销假成功！")
                    }
                }

                override fun onError(response: Response<SimpleResponse>?) {
                    super.onError(response)
                    LogUtils.d(response)
                    mView?.postVacationError(response?.body()?.msg ?: "销假失败，请重试！")
                }
            })
    }

}

interface HomeView : IView {
    fun onLeaveListSuccess(list: MutableList<StuLeave>, pager: Pager)
    fun onLeaveListError(error: String)
    fun onLeaveDetailSuccess(detail: StuLeave)
    fun onLeaveDetailError(error: String)
    fun onLeaveIDsSuccess(list: MutableList<LeaveID>)
    fun postVacationSuccess(msg: String)
    fun postVacationError(msg: String)
}