package app

import (
	. "../../../hotime"
	. "../../../hotime/common"
	"../../dri/ddsms"
	"time"
)

var orderCtr = Ctr{
	"count": func(this *Context) {
		if this.Session("id").ToCeilInt() == 0 {
			this.Display(2, "没有登录！")
			return
		}
		re := Map{}
		re["total"] = this.Db.Count("order", Map{"user_id": this.Session("id").ToCeilInt()})
		re["finish"] = this.Db.Count("order", Map{"AND": Map{"user_id": this.Session("id").ToCeilInt(), "status": 2}})
		re["late"] = this.Db.Count("order", Map{"AND": Map{"user_id": this.Session("id").ToCeilInt(), "status": 3}})

		this.Display(0, re)
	},
	"add": func(this *Context) {
		if this.Session("id").ToCeilInt() == 0 {
			this.Display(2, "没有登录！")
			return
		}
		ctgOrderDateId := ObjToInt(this.Req.FormValue("ctg_order_date_id"))
		//ctgId:=ObjToInt(this.Req.FormValue("category_id"))
		ctgOrderDate := this.Db.Get("ctg_order_date", "*", Map{"id": ctgOrderDateId})

		if ctgOrderDate.GetCeilInt64("now_sn")+1 > ctgOrderDate.GetCeilInt64("max_sn") {
			this.Display(5, "当前排号已经用完")
			return
		}

		data := Map{"create_time": time.Now().Unix(),
			"modify_time": time.Now().Unix(),
			"user_id":     this.Session("id").ToCeilInt(),
			"date":        ctgOrderDate.GetString("date"),
			"sn":          ctgOrderDate.GetCeilInt64("now_sn") + 1,
			"category_id": ctgOrderDate.GetCeilInt("category_id"),
			"admin_id":    1,
			"status":      1,
			"name":        time.Unix(ctgOrderDate.GetCeilInt64("date"), 0).Format("2006-01-02 ") + ctgOrderDate.GetString("name"),
		}

		data["id"] = this.Db.Insert("order", data)

		if data.GetCeilInt("id") == 0 {
			this.Display(5, "预约失败")
			return
		}

		this.Db.Update("ctg_order_date", Map{"now_sn": ctgOrderDate.GetCeilInt64("now_sn") + 1, "modify_time": time.Now().Unix()}, Map{"id": ctgOrderDate.GetCeilInt("id")})
		//查询并发送短信
		category := this.Db.Get("category", "`name`,`index`", Map{"id": ctgOrderDate.GetCeilInt("category_id")})
		//categorys := this.Db.Select("category", "org_id", Map{"index[~]": "," + ctgOrderDate.GetString("category_id") + ","})
		categorys := this.Db.Select("category", "org_id", Map{"[#]": "id IN (" + category.GetString("index")[1:len(category.GetString("index"))-1] + ")"})
		orgIDs := ""
		for _, v := range categorys {
			orgs := this.Db.Select("org", "id,`index`", Map{"id": v.GetCeilInt("org_id")})
			for _, orgv := range orgs {
				//orgIDs = append(orgIDs, orgv.GetCeilInt("id"))
				orgIDs = orgIDs + orgv.GetString("index")[1:]
			}
		}
		if len(orgIDs) != 0 {
			orgIDs = orgIDs[0 : len(orgIDs)-1]
			admin := this.Db.Select("admin", "phone,id", Map{"[#]": "org_id IN (" + orgIDs + ")"})
			user := this.Db.Get("user", "name", Map{"id": this.Session("id").ToCeilInt()})
			for _, v := range admin {
				phone := v.GetString("phone")
				if len(phone) == 11 {
					ddsms.DefaultDDY.SendTz([]string{phone}, this.Config.GetString("smsNotice"),
						map[string]string{"date": data.GetString("name"), "ctg": category.GetString("name"),
							"name": user.GetString("name"),
							"sn":   data.GetString("sn"),
						})
				}
			}
		}

		this.Display(0, data)
	},
	"search": func(that *Context) {
		if that.Session("id").ToCeilInt() == 0 {
			that.Display(2, "没有登录！")
			return
		}

		data := that.Db.Select("order", "*", Map{"user_id": that.Session("id").ToCeilInt()})
		for _, v := range data {
			v["category"] = that.Db.Get("category", "*", Map{"id": v.GetCeilInt("category_id")})
			v["parent_category"] = that.Db.Get("category", "id,name", Map{"id": v.GetMap("category").GetCeilInt("parent_id")})
		}
		that.Display(0, data)
	},
}
