package controllers

import (
	"bzd_server/lib"
	"bzd_server/models/System"
	"bzd_server/models/Task"
	"bzd_server/models/VerifyTemplate"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
)

type VerifyTemplateController struct {
	beego.Controller
}

// 列表 -
func (c *VerifyTemplateController) List() {
	// 验证登录 User_is, User_r
	_, User_is := lib.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_name := c.GetString("T_name")

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

// 添加-
func (c *VerifyTemplateController) Add() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_name := c.GetString("T_name")
	T_sort, _ := c.GetInt("T_sort")

	var_ := VerifyTemplate.VerifyTemplate{
		T_name: T_name,
		T_sort: T_sort,
	}

	Id, is := VerifyTemplate.Add_VerifyTemplate(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 *VerifyTemplateController) Up() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_name := c.GetString("T_name")
	T_sort, T_sort_err := c.GetInt("T_sort")

	T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")

	r, is := VerifyTemplate.Read_VerifyTemplate(T_VerifyTemplate_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 T_sort_err == nil {
		r.T_sort = T_sort
	}

	// .......
	if !VerifyTemplate.Update_VerifyTemplate(r, "T_name", "T_sort") {
		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 *VerifyTemplateController) Del() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")

	if r, is := VerifyTemplate.Read_VerifyTemplate(T_VerifyTemplate_id); is {
		if !VerifyTemplate.Delete_VerifyTemplate(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 *VerifyTemplateController) Map_List() {
	// 验证登录 User_is, User_r
	_, User_is := lib.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_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")

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

// 标签添加-
func (c *VerifyTemplateController) Map_Add() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
	T_name := c.GetString("T_name")
	T_field := c.GetString("T_field")
	T_text := c.GetString("T_text")
	T_label, _ := c.GetInt("T_label")
	T_source, _ := c.GetInt("T_source")
	T_sort, _ := c.GetInt("T_sort")

	var_ := VerifyTemplate.VerifyTemplateMap{
		T_VerifyTemplate_id: T_VerifyTemplate_id,
		T_name:              T_name,
		T_field:             T_field,
		T_text:              T_text,
		T_label:             T_label,
		T_source:            T_source,
		T_sort:              T_sort,
	}

	Id, is := VerifyTemplate.Add_VerifyTemplateMap(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 *VerifyTemplateController) Map_Up() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_name := c.GetString("T_name")
	T_field := c.GetString("T_field")
	T_text := c.GetString("T_text")
	T_label, T_label_err := c.GetInt("T_label")
	T_sort, T_sort_err := c.GetInt("T_sort")

	T_id := c.GetString("T_id")

	r, is := VerifyTemplate.Read_VerifyTemplateMap(T_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 len(T_field) > 0 {
		r.T_field = T_field
	}
	if len(T_text) > 0 {
		r.T_text = T_text
	}
	if T_label_err == nil {
		r.T_label = T_label
	}
	if T_sort_err == nil {
		r.T_sort = T_sort
	}

	// .......
	if !VerifyTemplate.Update_VerifyTemplateMap(r, "T_name", "T_label", "T_text", "T_field", "T_sort") {
		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 *VerifyTemplateController) Map_Del() {
	// 验证登录 User_is, User_r
	User_r, User_is := lib.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_id := c.GetString("T_id")

	if r, is := VerifyTemplate.Read_VerifyTemplateMap(T_id); is {
		if !VerifyTemplate.Delete_VerifyTemplateMap(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 *VerifyTemplateController) Map_Data_List() {
	//验证登录 User_is, User_r
	_, User_is := lib.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_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
	T_source, T_source_err := c.GetInt("T_source")
	if T_source_err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_source 错误!"}
		c.ServeJSON()
		return
	}

	T_task_id := c.GetString("T_task_id")
	_, is := Task.Read_Task(T_task_id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
		c.ServeJSON()
		return
	}

	Map_List := VerifyTemplate.Read_VerifyTemplateMap_List_For_Data(T_VerifyTemplate_id, T_source)
	Data := VerifyTemplate.Read_VerifyTemplateMapData_List(T_source, T_task_id, T_VerifyTemplate_id, Map_List)

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

// 添加标签数据
func (c *VerifyTemplateController) Map_Data_Pu() {
	//验证登录 User_is, User_r
	User_r, User_is := lib.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 RequestBody struct {
		T_source              int
		T_task_id             string
		T_VerifyTemplate_id   string
		VerifyTemplateMapData []VerifyTemplate.VerifyTemplateMapData_R
	}

	var body RequestBody
	data := c.Ctx.Input.RequestBody
	err := json.Unmarshal(data, &body)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "json.Unmarshal is err:" + err.Error()}
		c.ServeJSON()
	}

	_, is := Task.Read_Task(body.T_task_id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_task_id 错误!"}
		c.ServeJSON()
		return
	}

	MapDataList := make([]VerifyTemplate.VerifyTemplateMapData, 0)
	for _, v := range body.VerifyTemplateMapData {
		if len(v.T_value) == 0 {
			continue
		}
		val := VerifyTemplate.VerifyTemplateMapData{
			T_source:               body.T_source,
			T_task_id:              body.T_task_id,
			T_VerifyTemplate_id:    body.T_VerifyTemplate_id,
			T_VerifyTemplateMap_id: v.T_VerifyTemplateMap_id,
			T_value:                v.T_value,
		}
		MapDataList = append(MapDataList, val)
	}
	ids, is := VerifyTemplate.AddOrUpdate_VerifyTemplateMapData(MapDataList)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "保存失败"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "验证模版标签数据", "保存", body)

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