package handlers
import (
"html/template"
"log"
"net/http"
"synlotto-website/helpers"
"synlotto-website/models"
"github.com/gorilla/csrf"
)
func Home(w http.ResponseWriter, r *http.Request) {
log.Println("✅ Home hit")
tmpl := template.Must(template.ParseFiles(
"templates/layout.html",
"templates/index.html",
))
err := tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
"Data": Draws,
})
if err != nil {
log.Println("❌ Template error:", err)
http.Error(w, "Error rendering homepage", http.StatusInternalServerError)
}
}
func NewDraw(w http.ResponseWriter, r *http.Request) {
log.Println("➡️ New draw form opened")
tmpl := template.Must(template.ParseFiles(
"templates/layout.html",
"templates/new_draw.html",
))
err := tmpl.ExecuteTemplate(w, "layout", map[string]interface{}{
"csrfField": csrf.TemplateField(r),
"Page": "new_draw",
"Data": nil,
})
if err != nil {
log.Println("❌ Template error:", err)
http.Error(w, "Error rendering form", http.StatusInternalServerError)
}
}
func Submit(w http.ResponseWriter, r *http.Request) {
log.Println("📝 Form submission received")
r.ParseForm()
draw := models.ThunderballResult{
DrawDate: r.FormValue("date"),
Machine: r.FormValue("machine"),
Ballset: helpers.Atoi(r.FormValue("ballset")),
Ball1: helpers.Atoi(r.FormValue("ball1")),
Ball2: helpers.Atoi(r.FormValue("ball2")),
Ball3: helpers.Atoi(r.FormValue("ball3")),
Ball4: helpers.Atoi(r.FormValue("ball4")),
Ball5: helpers.Atoi(r.FormValue("ball5")),
Thunderball: helpers.Atoi(r.FormValue("thunderball")),
}
Draws = append(Draws, draw)
log.Printf("📅 %s | 🛠 %s | 🎱 %d | 🔢 %d,%d,%d,%d,%d | ⚡ %d\n",
draw.DrawDate, draw.Machine, draw.Ballset,
draw.Ball1, draw.Ball2, draw.Ball3, draw.Ball4, draw.Ball5, draw.Thunderball)
http.Redirect(w, r, "/", http.StatusSeeOther)
}