package controllers

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

type DistributorController struct {
	beego.Controller
}

// 列表 -
func (c *DistributorController) 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 || len(User_r.T_Distributor_id) > 0 {
		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 int64
	List, cnt := Distributor.Read_Distributor_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 *DistributorController) 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 || len(User_r.T_Distributor_id) > 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

	T_name := c.GetString("T_name")

	var_ := Distributor.Distributor{
		T_name:  T_name,
		T_State: 1,
	}

	Id, is := Distributor.Add_Distributor(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 *DistributorController) 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 || len(User_r.T_Distributor_id) > 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

	T_name := c.GetString("T_name")
	T_Distributor_id := c.GetString("T_Distributor_id")

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

	// .......
	if len(T_name) > 0 {
		r.T_name = T_name
	}

	if !Distributor.Update_Distributor(r, "T_name") {
		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 *DistributorController) 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 || len(User_r.T_Distributor_id) > 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}

	T_Distributor_id := c.GetString("T_Distributor_id")

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

	if !Distributor.Delete_Distributor(r) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
		c.ServeJSON()
		return
	}
	// TODO 删除经销商下的用户
	System.Add_UserLogs_T(User_r.T_uuid, "经销商管理", "删除", r)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}

// 管理员-全部列表-
func (c *DistributorController) 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 || len(User_r.T_Distributor_id) > 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权操作!"}
		c.ServeJSON()
		return
	}
	T_name := c.GetString("T_name")

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

// 绑定模板
func (c *DistributorController) BindVerifyTemplate() {
	// 验证登录 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_Distributor_id := c.GetString("T_Distributor_id")
	T_VerifyTemplate_ids := c.GetString("T_VerifyTemplate_ids")
	idList := lib.SplitStringSeparator(T_VerifyTemplate_ids, "|")

	var list []Distributor.DistributorVerifyTemplate
	for _, id := range idList {
		verifyTemplate, is := VerifyTemplate.Read_VerifyTemplate(id)
		if !is {
			continue
		}
		list = append(list, Distributor.DistributorVerifyTemplate{
			T_Distributor_id:         T_Distributor_id,
			T_VerifyTemplate_id:      verifyTemplate.T_VerifyTemplate_id,
			T_VerifyTemplateClass_id: verifyTemplate.T_class,
		})
	}
	// 删除旧模版
	Distributor.Delete_DistributorVerifyTemplate(T_Distributor_id)

	// 绑定新模版
	Id, is := Distributor.Add_DistributorVerifyTemplate(list)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "验证模版", "经销商绑定", list)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
	c.ServeJSON()
	return
}
func (c *DistributorController) ReadVerifyTemplateClass() {
	// 验证登录 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
	}
	T_Distributor_id := c.GetString("T_Distributor_id")

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

// 绑定模板
func (c *DistributorController) ReadVerifyTemplate() {
	// 验证登录 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
	}

	T_Distributor_id := c.GetString("T_Distributor_id")
	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_class, _ := c.GetInt("T_class")
	if T_class <= 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class Err!"}
		c.ServeJSON()
		return
	}

	T_name := c.GetString("T_name")

	var cnt int64
	List, cnt := Distributor.Read_DistributorVerifyTemplate_List(T_Distributor_id, T_class, 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
}
