package app

import (
	. "code.hoteas.com/golang/hotime"
	. "code.hoteas.com/golang/hotime/common"
)

var CtgCtr = Ctr{
	"info": func(that *Context) {
		sn := that.Req.FormValue("sn")
		ctg := that.Db.Get("ctg", "*", Map{"sn": sn})
		parents := []Map{}
		parentId := ctg.GetCeilInt64("parent_id")
		ctg["tongji"] = that.Db.Select("ctg", "sn,name,img,parent_id", Map{"parent_id": parentId})
		for true {

			if parentId == 0 {
				break
			}

			parent := that.Db.Get("ctg", "sn,name,img,parent_id", Map{"id": parentId})
			if parent == nil {
				break
			}

			parents = append(parents, parent)
			parentId = parent.GetCeilInt64("parent_id")

		}

		if ctg.GetCeilInt64("article_id") != 0 {
			ctg["article"] = that.Db.Get("article", "*", Map{"id": ctg.GetCeilInt64("article_id")})
		}

		ctg["parents"] = parents
		that.Display(0, ctg)
	},
	"list": func(that *Context) {
		sn := that.Req.FormValue("sn") //ctgsn
		page := ObjToInt(that.Req.FormValue("page"))
		pageSize := ObjToInt(that.Req.FormValue("pageSize"))

		if page == 0 {
			page = 1
		}
		if pageSize == 0 {
			pageSize = 50
		}

		keywords := that.Req.FormValue("keywords")

		//sort:=that.Req.FormValue("sort")

		where := Map{"state": 0}
		if sn != "" {
			ctg := that.Db.Get("ctg", "id", Map{"sn": sn})
			if ctg != nil {
				where["parent_id"] = ctg.GetCeilInt("id")
			}
		}

		if keywords != "" {
			where["OR"] = Map{"name[~]": keywords, "url[~]": keywords, "sn[~]": keywords}
		}

		if len(where) > 1 {
			where = Map{"AND": where}
		}

		where["ORDER"] = Slice{"sort DESC", "id DESC"}

		article := that.Db.Page(page, pageSize).PageSelect("ctg", "name,sn,sort,url,img", where)

		that.Display(0, article)

	},
}
