package controllers

import (
	"XT_New/enums"
	"XT_New/service"
	"XT_New/utils"
	"time"

	"github.com/astaxie/beego"
)

func DialysisBoardAPIControllerRegistRouter() {
	beego.Router("/api/dialysis/board", &DialysisBoardAPIController{}, "get:BoardData")
}

type DialysisBoardAPIController struct {
	BaseAuthAPIController
}

func (c *DialysisBoardAPIController) BoardData() {
	page, _ := c.GetInt64("page", 0)
	if page <= 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	nowTimeUinx := time.Now()
	today := nowTimeUinx.Format("2006-01-02")
	week := nowTimeUinx.Weekday()

	timeLayout := "2006-01-02"
	loc, _ := time.LoadLocation("Local")

	todayTime, err := time.ParseInLocation(timeLayout, today, loc)
	if err != nil {
		utils.ErrorLog("%v", err)
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}
	var limit int64 = 10
	offset := (page - 1) * limit
	todayTimeStamp := todayTime.Unix()

	adminUserInfo := c.GetAdminUserInfo()

	boards, err := service.GetDialysisBoard(adminUserInfo.CurrentOrgId, todayTimeStamp, offset, limit)
	if err != nil {
		utils.ErrorLog("%v", err)
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	c.ServeSuccessJSON(map[string]interface{}{
		"today":  today,
		"week":   week,
		"boards": boards,
	})

}