package controllers

import (
	"ColdVerify_server/Nats/NatsServer"
	"ColdVerify_server/conf"
	"ColdVerify_server/lib"
	"ColdVerify_server/models/Account"
	"ColdVerify_server/models/Device"
	"ColdVerify_server/models/System"
	"ColdVerify_server/models/Task"
	beego "github.com/beego/beego/v2/server/web"
	"math"
)

type TaskController struct {
	beego.Controller
}

// 列表 -
func (c *TaskController) 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
	}

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

	UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
	AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())

	var T_admin string
	if User_r.T_power > 2 {
		T_admin = User_r.T_uuid
	}

	var cnt int
	List, cnt := Task.Read_Task_List(T_uuid, T_admin, T_name, UserMap, AdminMap, 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 = cnt

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

// 列表 -
func (c *TaskController) UserTaskList() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification(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")
	UserMap := Account.UserListToMap(Account.Read_User_List_ALL_1())
	AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())

	var cnt int
	List, cnt := Task.Read_UserTask_List(User_r.T_uuid, T_name, UserMap, AdminMap, 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 = cnt

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

// 获取-
func (c *TaskController) 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
	}

	T_task_id := c.GetString("T_task_id")
	r, is := Task.Read_Task(T_task_id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}
	// 添加浏览量
	_ = Task.Add_Task_Visit(r)
	r.T_Visit += 1
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Task.TaskToTask_R(r)}
	c.ServeJSON()
	return
}

// 添加-
func (c *TaskController) 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
	}
	dc := Device.DeviceClass{
		T_uuid:  User_r.T_uuid,
		T_State: 1,
	}
	T_class_id, is := Device.Add_DeviceClass(dc)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加分类失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", dc)

	T_name := c.GetString("T_name")
	T_uuid := c.GetString("T_uuid") // 用户uuid
	T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
	T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
	T_deadline := c.GetString("T_deadline")
	T_scheme := c.GetString("T_scheme")
	T_collection := c.GetString("T_collection")
	T_reporting := c.GetString("T_reporting")
	T_delivery := c.GetString("T_delivery")

	var_ := Task.Task{
		T_class:                int(T_class_id),
		T_uuid:                 T_uuid,
		T_name:                 T_name,
		T_VerifyTemplate_class: T_VerifyTemplate_class,
		T_VerifyTemplate_id:    T_VerifyTemplate_id,
		T_deadline:             T_deadline,
		T_scheme:               T_scheme,
		T_collection:           T_collection,
		T_reporting:            T_reporting,
		T_delivery:             T_delivery,
		T_Show:                 1,
		T_State:                1,
	}

	T_task_id, is := Task.Add_Task(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	NatsServer.Create_Local_Table(T_task_id)
	// 添加任务操作日志
	Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "添加", var_)
	System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "添加", var_)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_task_id}
	c.ServeJSON()
	return
}

// 添加-
func (c *TaskController) AddData_Tool() {
	T_uuid := "3e84dda9-9eec-42b9-9350-0894262fc8a1" // 用户uuid

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

	var_ := Task.Task{
		T_task_id:          T_task_id,
		T_class:            0,
		T_uuid:             T_uuid,
		T_name:             T_name,
		T_Show:             1,
		T_State:            1,
		T_collection_state: 2,
	}

	_, is := Task.Add_Task_Tool(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	// 创建本地表
	NatsServer.Create_Local_Table(T_task_id)
	// 同步1.0数据
	NatsServer.Sync1_TaskData(T_task_id)

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

// 修改-
func (c *TaskController) 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_name := c.GetString("T_name")
	T_Show, T_Show_err := c.GetInt("T_Show")

	T_VerifyTemplate_class := c.GetString("T_VerifyTemplate_class")
	T_VerifyTemplate_id := c.GetString("T_VerifyTemplate_id")
	T_deadline := c.GetString("T_deadline")
	T_scheme := c.GetString("T_scheme")
	T_collection := c.GetString("T_collection")
	T_reporting := c.GetString("T_reporting")
	T_delivery := c.GetString("T_delivery")
	//T_scheme_state, T_scheme_err := c.GetInt("T_scheme_state")
	//T_collection_state, T_collection_err := c.GetInt("T_collection_state")
	//T_reporting_state, T_reporting_err := c.GetInt("T_reporting_state")
	//T_delivery_state, T_delivery_err := c.GetInt("T_delivery_state")
	T_doc1 := c.GetString("T_doc1")
	T_pdf1 := c.GetString("T_pdf1")
	T_doc2 := c.GetString("T_doc2")
	T_pdf2 := c.GetString("T_pdf2")
	T_doc3 := c.GetString("T_doc3")
	T_pdf3 := c.GetString("T_pdf3")

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

	// .......
	clos := make([]string, 0)
	if len(T_name) > 0 {
		r.T_name = T_name
		clos = append(clos, "T_name")
	}
	if T_Show_err == nil {
		r.T_Show = T_Show
		clos = append(clos, "T_Show")
	}
	if len(T_VerifyTemplate_class) > 0 {
		r.T_VerifyTemplate_class = T_VerifyTemplate_class
		clos = append(clos, "T_VerifyTemplate_class")
	}
	if len(T_VerifyTemplate_id) > 0 {
		r.T_VerifyTemplate_id = T_VerifyTemplate_id
		clos = append(clos, "T_VerifyTemplate_id")
	}
	if len(T_deadline) > 0 {
		r.T_deadline = T_deadline
		clos = append(clos, "T_deadline")
	}
	if len(T_scheme) > 0 {
		r.T_scheme = T_scheme
		clos = append(clos, "T_scheme")
	}
	if len(T_collection) > 0 {
		r.T_collection = T_collection
		clos = append(clos, "T_collection")
	}
	if len(T_reporting) > 0 {
		r.T_reporting = T_reporting
		clos = append(clos, "T_reporting")
	}
	if len(T_delivery) > 0 {
		r.T_delivery = T_delivery
		clos = append(clos, "T_delivery")
	}
	//if T_scheme_err == nil {
	//	r.T_scheme_state = T_scheme_state
	//	clos = append(clos, "T_scheme_state")
	//}
	//if T_collection_err == nil {
	//	r.T_collection_state = T_collection_state
	//	clos = append(clos, "T_collection_state")
	//}
	//if T_reporting_err == nil {
	//	r.T_reporting_state = T_reporting_state
	//	clos = append(clos, "T_reporting_state")
	//}
	//if T_delivery_err == nil {
	//	r.T_delivery_state = T_delivery_state
	//	clos = append(clos, "T_delivery_state")
	//}

	if len(T_doc1) > 0 {
		r.T_doc1 = T_doc1
		clos = append(clos, "T_doc1")
	}
	// 验证报告内容T_pdf1 ，上传后将 当前任务 实施方案 标志 为 1
	if len(T_pdf1) > 0 {
		r.T_pdf1 = T_pdf1
		clos = append(clos, "T_pdf1")
		r.T_scheme_state = 1
		clos = append(clos, "T_scheme_state")
	}
	if len(T_doc2) > 0 {
		r.T_doc2 = T_doc2
		clos = append(clos, "T_doc2")
	}
	// 验证报告内容T_pdf2 ，上传后将 当前任务 报告编写 标志 为 1
	if len(T_pdf2) > 0 {
		r.T_pdf2 = T_pdf2
		clos = append(clos, "T_pdf2")
		r.T_reporting_state = 1
		clos = append(clos, "T_reporting_state")
	}
	if len(T_doc3) > 0 {
		r.T_doc3 = T_doc3
		clos = append(clos, "T_doc3")
	}
	if len(T_pdf3) > 0 {
		r.T_pdf3 = T_pdf3
		clos = append(clos, "T_pdf3")
	}

	// .......
	// "T_name", "T_Show", "T_VerifyTemplate_id", "T_deadline",
	//		"T_collection", "T_reporting", "T_delivery",
	//		"T_collection_state", "T_reporting_state", "T_delivery_state",
	//		"T_doc1", "T_pdf1", "T_doc2", "T_pdf2", "T_doc3", "T_pdf3"
	if !Task.Update_Task(r, clos...) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	// 添加任务操作日志
	Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "修改", r)
	System.Add_UserLogs_T(User_r.T_uuid, "任务管理", "修改", r)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}

// 删除-
func (c *TaskController) 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_task_id := c.GetString("T_task_id")
	if r, is := Task.Read_Task(T_task_id); is {
		if !Task.Delete_Task(r) {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
			c.ServeJSON()
			return
		}
		// 添加任务操作日志
		Task.Add_TaskLogs_T(User_r.T_uuid, T_task_id, "任务管理", "删除", r)
		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 *TaskController) Logs_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_task_id := c.GetString("T_task_id")

	AdminMap := Account.AdminListToMap(Account.Read_Admin_List_ALL_1())

	var cnt int
	List, cnt := Task.Read_TaskLogs_List(T_task_id, AdminMap, 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 = cnt

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