// rule
package admin

import (
	"encoding/json"
	. "jy/mongodbutil"
	"time"

	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
)

func init() {
	//tag
	Admin.GET("/tag", func(c *gin.Context) {
		version := c.Query("version")
		c.HTML(200, "taglist.html", gin.H{"version": version})
	})
	Admin.POST("/tag/data", TagData)
	Admin.POST("/tag/save", TagSave)
	Admin.POST("/tag/del", TagDel)

	//onetag
	Admin.GET("/onetag", func(c *gin.Context) {
		version := c.Query("version")
		tagname := c.Query("tagname")
		tp := c.Query("tp")
		parentid := c.Query("parentid")
		c.HTML(200, "onetag.html", gin.H{"version": version, "tagname": tagname, "tp": tp, "parentid": parentid})
	})
	Admin.POST("/onetag/data", OneTagData)
	Admin.POST("/onetag/create", OneTagCreate)
	Admin.POST("/onetag/del", OneTagDel)
	Admin.POST("/onetag/editsave", OneTagEditSave)
	Admin.POST("/onetag/searchone", OneTagSearch)
}

//标签库列表
func TagData(c *gin.Context) {
	version, _ := c.GetPostForm("version")
	data, _ := Mgo.Find("tag", `{"s_version":"`+version+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	for _, d := range *data {
		timeStr := time.Unix(d["l_intime"].(int64), 0).Format(Date_Short_Layout)
		d["l_intime"] = timeStr
	}
	c.JSON(200, gin.H{"data": data})
}

//添加标签
func TagSave(c *gin.Context) {
	tagname, _ := c.GetPostForm("tagname")
	version, _ := c.GetPostForm("version")
	username, _ := c.GetPostForm("username")
	tp, _ := c.GetPostForm("tp")
	data, _ := Mgo.FindOne("tag", `{"s_tagname":"`+tagname+`","s_version":"`+version+`","delete":false}`)
	if len(*data) > 0 { //判重，防止添加name相同的标签
		c.JSON(200, gin.H{"rep": false})
		return
	}
	save := map[string]interface{}{
		"s_tagname": tagname,
		"s_version": version,
		"s_creater": username,
		"s_type":    tp,
		"l_intime":  time.Now().Unix(),
		"delete":    false,
	}
	b := Mgo.Save("tag", save)
	if b != "" {
		c.JSON(200, gin.H{"rep": true})
	} else {
		c.JSON(200, gin.H{"rep": false})
	}
}

//删除标签
func TagDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	b := Mgo.Update("tag", `{"_id":"`+_id+`"}`, map[string]interface{}{
		"$set": map[string]interface{}{"delete": true},
	}, false, false)
	data, _ := Mgo.Find("tagdetailinfo", `{"s_parentid":"`+_id+`"}`, `{"_id":-1}`, nil, false, -1, -1)
	if len(*data) > 0 { //删除父标签下所有详细标签
		for _, d := range *data {
			cid := d["_id"].(bson.ObjectId).Hex()
			//Mgo.Del("tagdetailinfo", `{"_id":"`+cid+`"}`)
			Mgo.Update("tagdetailinfo", `{"_id":"`+cid+`"}`, map[string]interface{}{
				"$set": map[string]interface{}{"delete": true},
			}, false, false)
		}
	}
	if b {
		c.JSON(200, gin.H{"rep": true})
	} else {
		c.JSON(200, gin.H{"rep": false})
	}
}

//查找某个tag的信息
func OneTagData(c *gin.Context) {
	version, _ := c.GetPostForm("version")
	parentid, _ := c.GetPostForm("parentid")
	data, _ := Mgo.Find("tagdetailinfo", `{"s_version":"`+version+`","s_parentid":"`+parentid+`","delete":false}`, `{"_id":-1}`, nil, false, -1, -1)
	for _, d := range *data {
		timeStr := time.Unix(d["l_intime"].(int64), 0).Format(Date_Short_Layout)
		d["l_intime"] = timeStr
	}
	c.JSON(200, gin.H{"data": data})
}

//添加详细标签
func OneTagCreate(c *gin.Context) {
	name, _ := c.GetPostForm("name")
	version, _ := c.GetPostForm("version")
	parentid, _ := c.GetPostForm("parentid")
	data, _ := Mgo.FindOne("tagdetailinfo", `{"s_name":"`+name+`","s_parentid":"`+parentid+`","s_version":"`+version+`","delete":false}`)
	if len(*data) > 0 { //判重
		c.JSON(200, gin.H{"rep": false})
		return
	}
	tp, _ := c.GetPostForm("tp")
	field, _ := c.GetPostForm("field")
	content, _ := c.GetPostForm("content")
	username, _ := c.GetPostForm("username")
	pratagname, _ := c.GetPostForm("pratagname")
	var jsondata interface{}
	err := json.Unmarshal([]byte(content), &jsondata)
	if err != nil { //不是json格式
		c.JSON(200, gin.H{"rep": false})
		return
	}
	save := make(map[string]interface{})
	save = map[string]interface{}{
		"s_pratagname": pratagname,
		"s_name":       name,
		"s_field":      field,
		"s_version":    version,
		"s_creater":    username,
		"s_type":       tp,
		"l_intime":     time.Now().Unix(),
		"content":      jsondata,
		"s_parentid":   parentid,
		"delete":       false,
	}
	b := Mgo.Save("tagdetailinfo", save)
	if b != "" {
		c.JSON(200, gin.H{"rep": true})
	} else {
		c.JSON(200, gin.H{"rep": false})
	}
}

//删除某个详细标签
func OneTagDel(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	b := Mgo.Update("tagdetailinfo", `{"_id":"`+_id+`"}`, map[string]interface{}{
		"$set": map[string]interface{}{"delete": true},
	}, false, false)
	if b {
		c.JSON(200, gin.H{"rep": true})
	} else {
		c.JSON(200, gin.H{"rep": false})
	}
}

//修改某个详细标签
func OneTagEditSave(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	content, _ := c.GetPostForm("content")
	var jsondata interface{}
	err := json.Unmarshal([]byte(content), &jsondata)
	if err != nil { //不是json格式
		c.JSON(200, gin.H{"rep": false})
		return
	}
	set := make(map[string]interface{})
	set["content"] = jsondata
	if jsondata != nil {
		b := Mgo.Update("tagdetailinfo", `{"_id":"`+_id+`"}`, map[string]interface{}{"$set": set}, false, false)
		if b {
			c.JSON(200, gin.H{"rep": true})
		} else {
			c.JSON(200, gin.H{"rep": false})
		}
	} else {
		c.JSON(200, gin.H{"rep": false})
	}
}

//根据id查找某个标签
func OneTagSearch(c *gin.Context) {
	_id, _ := c.GetPostForm("_id")
	data, _ := Mgo.FindOne("tagdetailinfo", `{"_id":"`+_id+`"}`)
	c.JSON(200, gin.H{"data": data})
}
