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_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: "登录失败-请检查账号密码"}
	} 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: "登录失败-请检查账号密码"}
	} 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 len(User_r.T_Distributor_id) > 0 && 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")
	T_Distributor_id := c.GetString("T_Distributor_id")
	if len(T_Distributor_id) == 0 {
		T_Distributor_id = User_r.T_Distributor_id
	}

	var cnt int
	PowerList := Account.Read_Power_List_ALL_1()
	PowerMap := Account.UserPowerListToPowerMap(PowerList)
	List, cnt := Account.Read_Admin_List(T_Distributor_id, 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) Get() {
	// 验证登录 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
	}

	type User struct {
		Account.Admin
		Permission string
	}
	var u User
	u.Admin = User_r
	// 内部管理员
	if len(User_r.T_Distributor_id) == 0 {
		if User_r.T_power <= 2 {
			u.Permission = "NBGLY" //内部管理员
		} else {
			u.Permission = "NBPTYH" //内部普通用户
		}
	} else {
		if User_r.T_power <= 2 {
			u.Permission = "JXSGLY" //经销商管理员
		} else {
			u.Permission = "JXSPTYH" //经销商普通用户
		}
	}

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: u}
	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")
	T_wxname := c.GetString("T_wxname")
	T_Distributor_id := c.GetString("T_Distributor_id")
	if len(T_Distributor_id) == 0 {
		T_Distributor_id = User_r.T_Distributor_id
	}
	if T_power <= 1 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

	var_ := Account.Admin{
		T_uuid:           T_uuid,
		T_power:          T_power,
		T_name:           T_name,
		T_user:           T_user,
		T_pass:           T_pass,
		T_wxname:         T_wxname,
		T_State:          1,
		T_Distributor_id: T_Distributor_id, // 分销商id,内部管理员为空
	}

	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")
	T_wxname := c.GetString("T_wxname")

	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 len(T_wxname) > 0 {
		r.T_wxname = T_wxname
	}

	if !Account.Update_Admin(r, "T_power", "T_name", "T_user", "T_pass", "T_wxname") {
		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(User_r.T_Distributor_id, "", PowerMap)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}

// 修改密码
func (c *AccountController) UpPassword() {
	// 验证登录 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_oldpass := c.GetString("T_oldpass")
	T_pass := c.GetString("T_pass")

	if T_oldpass != user_r.T_pass {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "旧密码错误!"}
		c.ServeJSON()
		return
	}

	if len(T_pass) > 0 {
		user_r.T_pass = T_pass
	}

	if !Account.Update_Admin(user_r, "T_pass") {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	user_r.T_pass = "********"
	System.Add_UserLogs_T(user_r.T_uuid, "管理员管理", "修改密码", user_r)

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