package admin_api_controllers

import (
	"XT_Admin_Api/controllers"
	"XT_Admin_Api/enums"
	"XT_Admin_Api/models/admin_models"
	"XT_Admin_Api/service"
	"fmt"
)

type AdminBaseAPIController struct {
	controllers.BaseAPIController
}

func (this *AdminBaseAPIController) ErrorLog(format string, a ...interface{}) {
	//beego.Error(fmt.Sprintf("[管理员后台] %v", fmt.Sprintf(format, a...)))
}

func (this *AdminBaseAPIController) WarnLog(format string, a ...interface{}) {
	//beego.Warn(fmt.Sprintf("[管理员后台] %v", fmt.Sprintf(format, a...)))
}

func (this *AdminBaseAPIController) InfoLog(format string, a ...interface{}) {
	//beego.Info(fmt.Sprintf("[管理员后台] %v", fmt.Sprintf(format, a...)))
}

func (this *AdminBaseAPIController) DebugLog(format string, a ...interface{}) {
	//beego.Debug(fmt.Sprintf("[管理员后台] %v", fmt.Sprintf(format, a...)))
}

func (this *AdminBaseAPIController) TraceLog(format string, a ...interface{}) {
	//beego.Trace(fmt.Sprintf("[管理员后台] %v", fmt.Sprintf(format, a...)))
}

func (this *AdminBaseAPIAuthController) GetAdminInfo() *admin_models.AdminAccount {
	mobile := this.GetString("account")
	password := this.GetString("password")
	//adminInfo :=GetAdminInfo(mobile,password)
	admin, getAdminErr := service.GetAdminAccount(mobile, password)
	if getAdminErr != nil {
		this.ErrorLog("获取管理员信息失败:%v", getAdminErr)
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
		return nil
	} else if admin == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)

		//this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
		return nil
	}
	var ads AdminInfo
	ads.Admin = admin
	return admin

}

type AdminInfo struct {
	Admin *admin_models.AdminAccount
}

type AdminBaseAPIAuthController struct {
	AdminBaseAPIController
}

func (this *AdminBaseAPIAuthController) Prepare() {
	this.AdminBaseAPIController.Prepare()
	//name := this.GetString("name")
	adminInfo := this.GetAdminInfo()
	fmt.Println(adminInfo)
	if adminInfo == nil {
		this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeNotLogin)
		this.StopRun()
	}
}
