package controllers

import (
	"ColdVerify_server/conf"
	"ColdVerify_server/lib"
	"ColdVerify_server/models/Account"
	"ColdVerify_server/models/System"
	beego "github.com/beego/beego/v2/server/web"
	"math"
)

type NewsController struct {
	beego.Controller
}

// 列表 -
func (c *NewsController) List() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}
	// 分页参数 初始化
	page, _ := c.GetInt("page")
	if page < 1 {
		page = 1
	}
	page_z, _ := c.GetInt("page_z")
	if page_z < 1 {
		page_z = conf.Page_size
	}

	// 查询
	T_title := c.GetString("T_title")
	T_Tag, _ := c.GetInt("T_Tag")
	List, cnt := System.Read_News_List(User_r.T_uuid, T_title, T_Tag, page, page_z)
	var r_jsons lib.R_JSONS
	page_size := math.Ceil(float64(cnt) / float64(page_z))
	r_jsons.List = List
	r_jsons.Page = page
	r_jsons.Page_size = int(page_size)
	r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
	r_jsons.Num = int(cnt)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}

func (c *NewsController) See() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}
	Id, _ := c.GetInt("Id")

	if Id == 0 {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "Id Err!"}
		c.ServeJSON()
		return
	}

	if err := System.Update_News_Tag(Id, User_r.T_uuid); err != nil {
		c.Data["json"] = lib.JSONS{Code: 200, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return

}
