package admin

import (
	"crypto/md5"
	"encoding/hex"
	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/god-jason/bucket/config"
	"github.com/zgwit/iot-gateway/curd"
)

type loginObj struct {
	Password string `json:"password"`
	Remember bool   `json:"remember"`
}

func md5hash(text string) string {
	h := md5.New()
	h.Write([]byte(text))
	sum := h.Sum(nil)
	return hex.EncodeToString(sum)
}

func login(ctx *gin.Context) {
	session := sessions.Default(ctx)

	var obj loginObj
	if err := ctx.ShouldBind(&obj); err != nil {
		curd.Error(ctx, err)
		return
	}

	password := config.GetString(MODULE, "password")
	if password != obj.Password {
		curd.Fail(ctx, "密码错误")
		return
	}

	//_, _ = db.Engine.InsertOne(&types.UserEvent{UserId: user.id, ModEvent: types.ModEvent{Type: "登录"}})

	//存入session
	session.Set("user", "admin")
	_ = session.Save()

	curd.OK(ctx, gin.H{"id": "admin"})
}
