package app

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

var ctg_order_dateCtr = Ctr{

	"info": func(that *Context) {

		//today:=time.Now().Weekday()
		id := ObjToInt(that.Req.FormValue("id"))
		category := that.Db.Get("category", "*", Map{"id": id})
		if category == nil {
			that.Display(4, "找不到该类别！")
			return
		}

		todayPMTime, _ := time.ParseInLocation("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 14:00", time.Local)
		todayAMTime, _ := time.ParseInLocation("2006-01-02 15:04", time.Now().Format("2006-01-02")+" 09:00", time.Local)

		weekDay := 1

		switch time.Now().Weekday().String() {
		case "Monday":
			weekDay = 1
		case "Tuesday":
			weekDay = 2
		case "Wednesday":
			weekDay = 3
		case "Thursday":
			weekDay = 4
		case "Friday":
			weekDay = 5
		case "Saturday":
			weekDay = 6
		case "Sunday":
			weekDay = 7

		}

		////future:=that.Db.Select("ctg_order_date","*",Map{"category_id":that.RouterString[2],"date[>]":time})
		date := Slice{}
		for i := 0; i < 7; i++ {
			day := weekDay + i + 1

			if day > 7 {
				day = day - 7
			}
			if day == 6 || day == 7 {
				continue
			}
			//fmt.Println(todayAMTime.Unix() + int64(24*60*60*(i+1)))
			dayAM := that.Db.Get("ctg_order_date", "*", Map{"AND": Map{"category_id": category.GetCeilInt("id"),
				"date": todayAMTime.Unix() + int64(24*60*60*(i+1))}})
			if dayAM == nil {
				dayAM = Map{"name": "9:00-11:30",
					"date":        todayAMTime.Unix() + int64(24*60*60*(i+1)),
					"create_time": time.Now().Unix(),
					"modify_time": time.Now().Unix(),
					"start_sn":    category.GetCeilInt("start_sn"),
					"max_sn":      category.GetCeilInt("start_sn") + category.GetCeilInt("am"+ObjToStr(day)),
					"now_sn":      category.GetCeilInt("start_sn"),
					"category_id": category.GetCeilInt("id"),
				}
				dayAM["id"] = that.Db.Insert("ctg_order_date", dayAM)
				if dayAM.GetCeilInt64("id") == 0 {
					that.Display(4, "内部错误！")
					return
				}
			}

			dayPM := that.Db.Get("ctg_order_date", "*", Map{"AND": Map{"category_id": category.GetCeilInt("id"), "date": todayPMTime.Unix() + int64(24*60*60*(i+1))}})
			//fmt.Println(that.Db.LastQuery, that.Db.LastData, dayPM, that.Db.LastErr)
			if dayPM == nil {
				//fmt.Println("dasdasdasda")
				dayPM = Map{"name": "13:30-16:30",
					"date":        todayPMTime.Unix() + int64(24*60*60*(i+1)),
					"create_time": time.Now().Unix(),
					"modify_time": time.Now().Unix(),
					"start_sn":    category.GetCeilInt("start_sn"),
					"max_sn":      category.GetCeilInt("start_sn") + category.GetCeilInt("pm"+ObjToStr(day)),
					"now_sn":      category.GetCeilInt("start_sn"),
					"category_id": category.GetCeilInt("id"),
				}
				dayPM["id"] = that.Db.Insert("ctg_order_date", dayPM)
				if dayPM.GetCeilInt64("id") == 0 {
					that.Display(4, "内部错误！")
					return
				}
			}

			date = append(date, Map{"name": "星期" + ObjToStr(day) + "(" + time.Unix(todayPMTime.Unix()+int64(24*60*60*(i+1)), 0).Format("01-02") + ")",
				"am": dayAM,
				"pm": dayPM,
			})
		}

		that.Display(0, date)
	},
}
