package admin

import (
	. "jy/mongodbutil"
	"net/http"
	qu "qfw/util"
	"time"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func init() {
	Admin.GET("/versionclear", func(c *gin.Context) {
		c.HTML(
			http.StatusOK, "clearversion.html",
			gin.H{},
		)
	})
	//清理版本
	Admin.POST("/clearversion/data", func(c *gin.Context) {
		data, _ := Mgo.Find("clearversion", `{"delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": data})
	})
	Admin.POST("/clearversion/save", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		data := GetPostForm(c)
		if _id != "" {
			Mgo.UpdateById("clearversion", _id, map[string]interface{}{"$set": data})
			c.JSON(200, gin.H{"rep": true})
		} else {
			clearversion, _ := c.GetPostForm("clearversion")
			tmp, _ := Mgo.FindOne("version", `{"clearversion":"`+clearversion+`","delete":false}`)
			if len(*tmp) > 0 {
				c.JSON(200, gin.H{"rep": false})
			} else {
				s_pversionid, _ := c.GetPostForm("s_pversionid")
				data["l_createtime"] = time.Now().Unix()
				s_username := sessions.Default(c).Get("username").(string)
				data["s_username"] = s_username
				data["delete"] = false
				vid := Mgo.Save("clearversion", data)
				if s_pversionid != "" { //克隆属性配置
					list, _ := Mgo.Find("clearversioninfo", `{"vid":"`+s_pversionid+`","delete":false}`, nil, nil, false, -1, -1)
					for _, v := range *list {
						delete(v, "_id")
						v["l_createtime"] = time.Now().Unix()
						v["s_username"] = sessions.Default(c).Get("username")
						v["l_lasttime"] = time.Now().Unix()
						v["vid"] = vid
						v["delete"] = false
						pid := Mgo.Save("clearversioninfo", v)
						s_field := qu.ObjToString(v["s_field"])
						//克隆清理逻辑配置
						copyClearRuleLogic(vid, pid, s_field, s_pversionid, s_username)
					}
				}
				c.JSON(200, gin.H{"rep": true})
			}
		}
	})
	Admin.POST("/clearversion/use", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		isuse, _ := c.GetPostForm("isuse")
		if isuse == "true" { //启用确保只有一个版本
			l, _ := Mgo.Find("clearversion", `{"delete":false,"isuse":true}`, nil, nil, false, -1, -1)
			if len(*l) > 0 {
				c.JSON(200, gin.H{"rep": false})
				return
			}
		}
		bisuse := false
		if isuse == "true" {
			bisuse = true
		}
		set := map[string]interface{}{
			"$set": map[string]interface{}{
				"isuse": bisuse,
			},
		}
		b := Mgo.UpdateById("clearversion", _id, set)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/clearversion/del", func(c *gin.Context) { //删除版本
		_id, _ := c.GetPostForm("_id")
		b := Mgo.UpdateById("clearversion", _id, `{"$set":{"delete":true}}`)
		//级联删除
		//刪除属性配置
		//删除逻辑

		//刪除属性配置
		go DelClearVersionInfo(_id)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.GET("/clearversion/info", func(c *gin.Context) {
		vid := c.Query("vid")
		v, _ := Mgo.Find("clearversion", `{}`, `{"_id":-1}`, `{"version":1}`, false, -1, -1)
		c.HTML(
			http.StatusOK, "clearversioninfo.html",
			gin.H{"vid": vid, "clearversions": v},
		)
	})
	Admin.POST("/clearversion/infodata", func(c *gin.Context) {
		vid, _ := c.GetPostForm("vid")
		list, _ := Mgo.Find("clearversioninfo", `{"vid":"`+vid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
		c.JSON(200, gin.H{"data": list, "vid": vid})
	})
	Admin.POST("/clearversion/infosave", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		data := GetPostForm(c)
		if _id != "" {
			Mgo.UpdateById("clearversioninfo", _id, map[string]interface{}{"$set": data})
			c.JSON(200, gin.H{"rep": true})
		} else {
			s_field, _ := c.GetPostForm("s_field")
			vid, _ := c.GetPostForm("vid")
			tmp, _ := Mgo.FindOne("clearversioninfo", `{"s_field":"`+s_field+`","vid":"`+vid+`","delete":false}`)
			if len(*tmp) > 0 {
				c.JSON(200, gin.H{"rep": false})
			} else {
				data["l_createtime"] = time.Now().Unix()
				data["s_username"] = sessions.Default(c).Get("username")
				data["l_lasttime"] = time.Now().Unix()
				data["delete"] = false
				//Mgo.Save("clearversioninfo", data)
				pid := Mgo.Save("clearversioninfo", data)
				fromvid, _ := data["s_pversionid"].(string)
				if fromvid != "" {
					copyClearRuleLogic(vid, pid, s_field, fromvid, sessions.Default(c).Get("username").(string))
				}
				c.JSON(200, gin.H{"rep": true})
			}
		}
	})
	Admin.POST("/clearversion/infouse", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		isuse, _ := c.GetPostForm("isuse")
		b := Mgo.UpdateById("clearversioninfo", _id, `{"$set":{"isuse":`+isuse+`}}`)
		c.JSON(200, gin.H{"rep": b})
	})
	Admin.POST("/clearversion/infodel", func(c *gin.Context) {
		_id, _ := c.GetPostForm("_id")
		b := Mgo.UpdateById("clearversioninfo", _id, `{"$set":{"delete":true}}`)
		v, _ := Mgo.FindById("clearversioninfo", _id, `{"vid":1}`)
		go DelClearLogic(qu.ObjToString((*v)["vid"]), _id)
		c.JSON(200, gin.H{"rep": b})
	})

}
