// 订单查询
package controllers

import (
	"box-gm/events"
	"box-gm/utils"
	"time"
	"box-gm/models"

	"github.com/astaxie/beego"
)

type OrderQueryController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *OrderQueryController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()
	userPermission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (userPermission & (1 << uint(models.ModelStatment))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	currTime := time.Now()

	filename := ""
	if c.GetString("action") == "query" || c.GetString("querydata") == "orderquery" {
		uid := c.GetString("uid")
		name := c.GetString("name")
		startTime := c.GetString("start_time")
		endTime := c.GetString("end_time")

		// log.Printf("查询玩家支付订单 uid[%s] name[%s] startTime[%s] endTime[%s]", uid, name, startTime, endTime)

		_startTime := utils.GetTime(startTime + " 00:00:00")
		_endTime := utils.GetTime(endTime + " 23:59:59")
		eventList, cur, tol := events.OrderQuery_get_records(name, uid, _startTime, _endTime)
		filename = events.Save_orderquery_records(eventList, userinfo.(*LoginInfo).Username)

		c.Data["Events"] = eventList
		c.Data["Player_id"] = uid
		c.Data["Player_name"] = name
		c.Data["Start_time"] = startTime
		c.Data["End_time"] = endTime
		c.Data["Cur_charge"] = cur
		c.Data["Tol_charge"] = tol
	} else {
		c.Data["Player_id"] = ""
		c.Data["Player_name"] = ""
		c.Data["Start_time"] = currTime.Format("2006-01-02")
		c.Data["End_time"] = currTime.Format("2006-01-02")
		c.Data["Cur_charge"] = 0
		c.Data["Tol_charge"] = 0
	}

	if c.GetString("querydata") == "orderquery" {
		c.Ctx.Output.Download(filename)
	} else {
		//界面模板文件
		c.TplName = "orderquery.tpl"
	}
}
