// 实时支付
package controllers

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

	"github.com/astaxie/beego"
)

type RtPayController struct {
	beego.Controller
}

// 主界面
// 通过c.Data[key]返回数据
// ace模板文件通过{{.key}}获取数据
func (c *RtPayController) 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.ModelRealTime))) == 0 {
		c.Ctx.Redirect(302, "/accountlogin")
		return
	}
	serverId := c.GetString("server_id")
	if serverId == "" {
		serverId = "1001"
	}
	minutes, _ := c.GetInt("minutes")
	if minutes == 0 {
		minutes = 5
	}
	series, categories, cur, tol := events.Pay_get_range_day_time_count(0, minutes, serverId)
	filename := events.Save_rtpay_records(series, minutes, userinfo.(*LoginInfo).Username)
	c.Data["Series"], c.Data["Categories"] = series, categories
	c.Data["Server_id"] = serverId
	c.Data["Minutes"] = minutes
	c.Data["file"] = filename
	c.Data["TodayCharge"] = cur
	c.Data["TotalCharge"] = tol
	if c.GetString("querydata") == "rtpay" {
		c.Ctx.Output.Download(filename)
	} else {
		//界面模板文件
		c.TplName = "rt_pay.tpl"
	}
}
