package web

import (
	"net/http"
	//"qfw/util"
	"time"
	. "tools"
	u "util"
)

func class(w http.ResponseWriter, r *http.Request) {
	defer Catch()
	if CheckSession(w, r) {
		ss, _ := Store.Get(r, SN)
		data := map[string]interface{}{"s_name": ss.Values["s_name"]}
		//查询配置规则
		res, _ := MgoClass.Find(COLL_CLASS, nil, `{"i_order":1}`, nil, false, 0, 200)
		data["rule"] = res
		ExecuteTpl("class.html", &data, w)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}

func classsave(w http.ResponseWriter, r *http.Request) {
	defer Catch()
	res := JSON{}
	if CheckSession(w, r) {
		_id := r.FormValue("_id")
		s_name := r.FormValue("s_name")
		s_fields := r.FormValue("s_fields")
		s_savefield := r.FormValue("s_savefield")
		s_default := r.FormValue("s_default")
		s_pid := r.FormValue("s_pid")
		s_class_prerule := r.FormValue("s_class_prerule")
		if _id == "" {
			session, _ := Store.Get(r, SN)
			ids := MgoClass.Save(COLL_CLASS, map[string]interface{}{
				"s_name":          s_name,
				"s_fields":        s_fields,
				"s_savefield":     s_savefield,
				"s_default":       s_default,
				"l_lasttime":      time.Now().Unix(),
				"l_date":          time.Now().Unix(),
				"s_user":          session.Values["s_name"],
				"s_pid":           s_pid,
				"i_order":         GetOrder("class"),
				"s_class_prerule": s_class_prerule,
			})
			if ids != "" {
				//task.TaskMap = map[string]*task.TTask{}
				//go task.InitRule() //重新初始化任务
				res.Status = true
				res.Msg = "保存成功"
			} else {
				res.Msg = "保存失败"
			}
		} else {
			b := MgoClass.Update(COLL_CLASS, `{"_id":"`+_id+`"}`, map[string]interface{}{
				"$set": map[string]interface{}{
					"s_name":          s_name,
					"s_fields":        s_fields,
					"s_default":       s_default,
					"s_savefield":     s_savefield,
					"l_lasttime":      time.Now().Unix(),
					"s_pid":           s_pid,
					"s_class_prerule": s_class_prerule,
				},
			}, false, false)
			if b {
				//task.TaskMap = map[string]*task.TTask{}
				//go task.InitRule() //重新初始化任务
				res.Status = true
				res.Msg = "保存成功"
			} else {
				res.Msg = "保存失败"
			}
		}
	} else {
		res.Msg = "重新登陆"
	}
	WriteJSON(w, &res)
}

func classdelete(w http.ResponseWriter, r *http.Request) {
	defer Catch()
	res := JSON{}
	if CheckSession(w, r) {
		_id := r.FormValue("_id")
		if _id == "" {
			res.Msg = "id不能为空"
		} else {
			b := MgoClass.Del(COLL_CLASS, `{"_id":"`+_id+`"}`)
			if b {
				//删除子规则
				MgoClass.Del(COLL_RULE, `{"s_classid":"`+_id+`"}`)
				//go task.InitRule() //重新初始化任务
				res.Status = true
				res.Msg = "删除成功"
				/**
				增加删除规则逻辑
				**/
			} else {
				res.Msg = "删除失败"
			}
		}
	} else {
		res.Msg = "重新登陆"
	}
	WriteJSON(w, &res)
}

//获取列表
func getCList(w http.ResponseWriter, r *http.Request) {
	defer Catch()
	res := JSON{}
	if CheckSession(w, r) {
		nid := r.FormValue("nid")
		var q interface{}
		if nid != "" {
			q = map[string]interface{}{
				"_id": map[string]interface{}{
					"$ne": u.StringTOBsonId(nid),
				},
			}
		}
		rule, _ := MgoClass.Find(COLL_CLASS, q, nil, `{"s_name":1}`, false, 0, 200)
		res.Data = rule
		res.Status = true
	} else {
		res.Msg = "重新登陆"
	}
	WriteJSON(w, res)
}
