package admin

import (
	"fmt"
	"wayne/go-admin/business"
	"wayne/go-admin/models"

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

type Priv struct {
	BaseController
}

func (conn Priv) Login(c *gin.Context) {
	// 取得前端傳過來的帳號密碼
	var body struct {
		Username string `form:"username" json:"username"`
		Password string `form:"password" json:"password"`
	}
	err := c.ShouldBind(&body)
	if err != nil {
		conn.Error(c)
		return
	}

	//檢查帳號密碼是否為空
	if body.Username == "" || body.Password == "" {
		conn.Error(c)
		return
	}

	// 帳號密碼比對
	adminUser := models.AdminUsers{}
	models.DB.Where("username = ?", body.Username).First(&adminUser)

	//判斷帳號是否存在
	if adminUser.Username == "" {
		conn.Error(c)
		return
	}

	//判斷密碼是否正確
	if adminUser.Password != body.Password {
		conn.Error(c)
		return
	}
	fmt.Println("abc")

	fmt.Println("adminUser", adminUser)
	// 產生token
	token, err := business.GenToken()
	// 回傳token
	conn.Success(c, token)
}

func (conn Priv) AdminList(c *gin.Context) {
	adminUser := []models.AdminUsers{}

	models.DB.Find(&adminUser)
	fmt.Print(adminUser)
	// conn.Success(c, adminUser)

	// model.AdminUser{}.GetAdminUserList()
	// conn.Success(c)
}
