package api

import (
	"fmt"

	"github.com/gin-gonic/gin"
)

// RegRouters 注册路由
func RegRouters(svc *Service) {

	crrouter := svc.NewGinRouter("/" + svc.Conf.Name)
	crrouter.group.Use(Logger())
	// 用户登录
	crrouter.POST("/user/login/password", UserLoginPassword)
	// 用户管理
	crrouter.POSTJWT("/admin/user/create", CreateUser)
	crrouter.POSTJWT("/admin/user/delete/:id", DeleteUser)
	crrouter.POSTJWT("/admin/user/delete/batch", BatchDeleteUser)
	crrouter.GETJWT("/admin/user/list", UserList)
	// crrouter.GETJWT("/admin/user/detail/:id", UserDetail)
	crrouter.POSTJWT("/admin/user/update", UpdateUser)

	crrouter.POSTJWT("/admin/user/import", ImportUser)
	crrouter.GETJWT("/admin/user/export", ExportUser)
	crrouter.GETJWT("/admin/user/tmplate", UserTmplate)

	// 获取自己的详情信息
	crrouter.GETJWT("/user/profile", UserProfile)
	// 增加用户学习时长
	// crrouter.GETJWT("/user/learn/inc", UserLearnInc)
	// 考核历史
	crrouter.POSTJWT("/exam/history/create", ExamHistoryCreate)
	crrouter.GETJWT("/exam/history/list", ExamHistoryList)

}

func Logger() gin.HandlerFunc {

	return func(c *gin.Context) {
		// 开始时间
		// start := time.Now()
		// 处理请求
		c.Next()
		// 结束时间
		// end := time.Now()
		//执行时间
		// latency := end.Sub(start)

		path := c.Request.URL.Path

		clientIP := c.ClientIP()
		// method := c.Request.Method
		// statusCode := c.Writer.Status()

		out := fmt.Sprintf("%15s=> %s", clientIP, path)
		fmt.Println(out)
	}
}
