package controllers

import (
	"encoding/json"
	"fmt"
	ywtcenter "hello/models/ywtcenter"
	"hello/services"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

// CMS API
type AdminController struct {
	beego.Controller
}

func (c *AdminController) URLMapping() {
	c.Mapping("GetAdminInfo", c.GetAdminInfo)
	c.Mapping("Login", c.Login)
}

// @router /admin/getAdminInfo [get]
func (c *AdminController) GetAdminInfo() {
	id, err := c.GetInt("id", 0)
	if err != nil {
		logs.Info("代码有问题")
	}
	userId, err := c.GetInt("userId", 0)
	if err != nil {
		logs.Info("代码有问题")
	}
	logs.Info("id:", id)
	logs.Info("userId:", userId)

	// 链接数据库，获取对象

	// 更新userId
	// services.UpdateUserId1(id, userId)
	services.UpdateUserId2(id, userId)

	// 删除
	// services.DeleteMgAdminById(id)

	// 查找列表
	services.QueryList()

	// 通过ID查找对象
	var user ywtcenter.MgAdmin
	user = services.FindOneById(id)
	backValue, err := json.Marshal(user)
	if err != nil {
		fmt.Println(err)
		return
	}
	c.Ctx.WriteString(string(backValue))
	return
}

type Admin struct {
	Username string
	Password string
}

// @router /admin/login [post]
func (c *AdminController) Login() {
	var user Admin
	data := c.Ctx.Input.RequestBody
	logs.Info(data)
	//json数据封装到user对象中
	err := json.Unmarshal(data, &user)
	logs.Info(user)
	if err != nil {
		fmt.Println("json.Unmarshal is err:", err.Error())
	}
	username := user.Username
	password := user.Password
	logs.Info("我来啦Admin～～～～username:", username+"\tpassword:"+password)
	logs.Info(user)
	c.Ctx.WriteString(user.Username)
	// c.Data["Website"] = "beego.me"
	// c.Data["Email"] = "astaxie@gmail.com"
	// c.TplName = "index.tpl"
}
