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"
	uuid "github.com/satori/go.uuid"
	"math"
	"time"
)

type AccountController struct {
	beego.Controller
}

func (c *AccountController) Login_html() {
	c.TplName = "login.html"

}

// 验证登录
func (c *AccountController) Login_verification() {
	Admin_user := c.GetString("bzd_username")
	Admin_pass := c.GetString("bzd_password")

	println("Login_verification", Admin_user, Admin_pass)
	err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)

	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "E!"}
	} else {
		User_tokey := Account.Add_Tokey(user_r.T_uuid)
		c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
		c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}

		System.Add_UserLogs(user_r.T_uuid, "登陆", "用户登陆", "")

	}

	c.ServeJSON()
	return

}

// 验证登录
func (c *AccountController) Login_Admin_verification() {
	Admin_user := c.GetString("bzd_username")
	Admin_pass := c.GetString("bzd_password")

	println("Login_Admin_verification", Admin_user, Admin_pass)
	err, user_r := Account.Read_Admin_verification(Admin_user, Admin_pass)

	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "E!"}
	} else {
		User_tokey := Account.Add_Tokey(user_r.T_uuid)
		c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
		c.Data["json"] = lib.JSONS{Code: 200, Msg: "OK!", Data: User_tokey}

		System.Add_UserLogs(user_r.T_uuid, "登陆", "管理员登陆", "")

	}

	c.ServeJSON()
	return

}

// --------------------------------------------------------------------------------------------------------------
// 管理员-列表-
func (c *AccountController) 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
	}

	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, 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 int
	PowerList := Account.Read_Power_List_ALL_1()
	PowerMap := Account.UserPowerListToPowerMap(PowerList)
	List, cnt := Account.Read_Admin_List(T_name, page, page_z, PowerMap)
	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 = cnt

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

// 管理员-添加-
func (c *AccountController) 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
	}
	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

	T_uuid := uuid.NewV4().String()
	T_power, _ := c.GetInt("T_power")
	T_name := c.GetString("T_name")
	T_user := c.GetString("T_user")
	T_pass := c.GetString("T_pass")

	var_ := Account.Admin{
		T_uuid:  T_uuid,
		T_power: T_power,
		T_name:  T_name,
		T_user:  T_user,
		T_pass:  T_pass,
		T_State: 1,
	}

	if err, _ := Account.Read_Admin_ByT_user(T_user); err == nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名已存在!"}
		c.ServeJSON()
		return
	}

	Id, err := Account.Add_Admin(var_)
	if err != nil {
		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 *AccountController) 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
	}
	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

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

	err, r := Account.Read_Admin_ByT_uuid(T_uuid)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}
	err, r2 := Account.Read_Admin_ByT_user(T_user)
	if err == nil && T_uuid != r2.T_uuid {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名已存在!"}
		c.ServeJSON()
		return
	}
	if T_power > 0 {
		r.T_power = T_power
	}
	if len(T_name) > 0 {
		r.T_name = T_name
	}
	if len(T_user) > 0 {
		r.T_user = T_user
	}
	if len(T_pass) > 0 {
		r.T_pass = T_pass
	}

	if !Account.Update_Admin(r, "T_power", "T_name", "T_user", "T_pass") {
		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 *AccountController) 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
	}
	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}
	T_uuid := c.GetString("T_uuid")

	if User_r.T_uuid == T_uuid {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除自己！"}
		c.ServeJSON()
		return
	}
	if err, r := Account.Read_Admin_ByT_uuid(T_uuid); err == nil {
		if !Account.Delete_Admin_(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
}

// 用户权限-所有列表-
func (c *AccountController) UserPower_List_All() {
	// 验证登录 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
	}
	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}
	var r_jsons lib.R_JSONS
	r_jsons.List = Account.Read_Power_List_ALL_Admin_Power(User_r.T_power)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}

// 管理员-全部列表-
func (c *AccountController) List_All() {
	// 验证登录 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
	}

	if User_r.T_power > 2 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}
	PowerList := Account.Read_Power_List_ALL_1()
	PowerMap := Account.UserPowerListToPowerMap(PowerList)

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