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 UserController struct {
	beego.Controller
}

// 获取-
func (c *UserController) Get() {
	// 验证登录 User_is, 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, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	r, is := Account.Read_User_ById(Id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

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

// 列表 -
func (c *UserController) List() {
	// 验证登录 User_is, 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
	}

	var r_jsons lib.R_JSONS
	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_name := c.GetString("T_name")

	var cnt int64
	List, cnt := Account.Read_User_List(T_name, page, page_z)
	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 *UserController) Add() {
	// 验证登录 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
	}

	T_power, _ := c.GetInt("T_power")
	T_name := c.GetString("T_name")
	T_user := c.GetString("T_user")
	T_pass := c.GetString("T_pass")
	T_passstr := c.GetString("T_passstr")

	var_ := Account.User{
		T_power:   T_power,
		T_user:    T_user,
		T_name:    T_name,
		T_pass:    T_pass,
		T_passstr: T_passstr,
		T_Show:    1,
		T_State:   1,
	}

	Id, is := Account.Add_User(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "添加", var_)

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

// 修改-
func (c *UserController) Up() {
	// 验证登录 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
	}

	T_power, T_power_err := c.GetInt("T_power")
	T_name := c.GetString("T_name")
	T_pass := c.GetString("T_pass")
	T_passstr := c.GetString("T_passstr")
	T_Show, T_Show_err := c.GetInt("T_Show")

	T_uuid := c.GetString("T_uuid")
	r, is := Account.Read_User(T_uuid)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	// .......
	if T_power_err == nil {
		r.T_power = T_power
	}
	if len(T_name) > 0 {
		r.T_name = T_name
	}
	if len(T_pass) > 0 {
		r.T_pass = T_pass
	}
	if len(T_passstr) > 0 {
		r.T_passstr = T_passstr
	}
	if T_Show_err == nil {
		r.T_Show = T_Show
	}

	// .......
	if !Account.Update_User(r, "T_power", "T_name", "T_pass", "T_passstr", "T_Show") {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "修改", r)

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

// 删除-
func (c *UserController) Del() {
	// 验证登录 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
	}

	T_uuid := c.GetString("T_uuid")

	if r, is := Account.Read_User(T_uuid); is {
		if !Account.Delete_User_(r) {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
			c.ServeJSON()
			return
		}
		System.Add_UserLogs_T(user_r.T_uuid, "用户管理", "删除", r)
		c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
		c.ServeJSON()

		return
	}
	c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
	c.ServeJSON()
	return
}
