package admin
import (
"encoding/json"
"html/template"
"net/http"
"strings"
)
type GridCard struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Icon string `json:"icon"`
Link string `json:"link"`
Color string `json:"color"`
Order int `json:"order"`
Enabled bool `json:"enabled"`
}
// In-memory storage for grid cards (replace with database in production)
var gridCards = []GridCard{
{
ID: "evidence-aut",
Title: "Evidence aut",
Description: "Záznam o jízdách služebním autem",
Icon: "🚗",
Link: "/evidence-aut",
Color: "#004990",
Order: 1,
Enabled: true,
},
{
ID: "kontakt",
Title: "Kontakt",
Description: "Kontaktní formulář",
Icon: "📧",
Link: "/kontakt",
Color: "#0072b0",
Order: 2,
Enabled: true,
},
}
func HandleAdmin(w http.ResponseWriter, r *http.Request) {
user := GetCurrentUser(r)
if user == nil {
http.Redirect(w, r, "/login", http.StatusFound)
return
}
tmpl := `
Administrace
Správa karet hlavní stránky
`
t, err := template.New("admin").Parse(tmpl)
if err != nil {
http.Error(w, "Template error", http.StatusInternalServerError)
return
}
data := struct {
Username string
Role string
}{
Username: user.Username,
Role: user.Role,
}
w.Header().Set("Content-Type", "text/html")
t.Execute(w, data)
}
func HandleAdminCards(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case "GET":
json.NewEncoder(w).Encode(gridCards)
case "POST":
var card GridCard
if err := json.NewDecoder(r.Body).Decode(&card); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid JSON"})
return
}
// Check if updating existing card
found := false
for i, existingCard := range gridCards {
if existingCard.ID == card.ID {
gridCards[i] = card
found = true
break
}
}
if !found {
gridCards = append(gridCards, card)
}
json.NewEncoder(w).Encode(map[string]string{"message": "Card saved successfully"})
default:
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
}
}
func HandleAdminCardToggle(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != "POST" {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
return
}
// Extract card ID from URL path
path := r.URL.Path
parts := strings.Split(path, "/")
if len(parts) < 4 {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid card ID"})
return
}
cardID := parts[3] // /admin/cards/{id}/toggle
for i, card := range gridCards {
if card.ID == cardID {
gridCards[i].Enabled = !gridCards[i].Enabled
json.NewEncoder(w).Encode(map[string]string{"message": "Card toggled successfully"})
return
}
}
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "Card not found"})
}
func HandleAdminCardDelete(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != "DELETE" {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]string{"error": "Method not allowed"})
return
}
// Extract card ID from URL path
path := r.URL.Path
parts := strings.Split(path, "/")
if len(parts) < 4 {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"error": "Invalid card ID"})
return
}
cardID := parts[3] // /admin/cards/{id}
for i, card := range gridCards {
if card.ID == cardID {
// Remove card from slice
gridCards = append(gridCards[:i], gridCards[i+1:]...)
json.NewEncoder(w).Encode(map[string]string{"message": "Card deleted successfully"})
return
}
}
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(map[string]string{"error": "Card not found"})
}
func HandleGetCards(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Filter only enabled cards and sort by order
var enabledCards []GridCard
for _, card := range gridCards {
if card.Enabled {
enabledCards = append(enabledCards, card)
}
}
// Sort by order
for i := 0; i < len(enabledCards)-1; i++ {
for j := i + 1; j < len(enabledCards); j++ {
if enabledCards[i].Order > enabledCards[j].Order {
enabledCards[i], enabledCards[j] = enabledCards[j], enabledCards[i]
}
}
}
json.NewEncoder(w).Encode(enabledCards)
}