<?php
/**
 * Lions Club Dashboard - Nieuwe KPI Toevoegen
 *
 * Pagina om nieuwe doelstellingen/KPI's toe te voegen.
 */

session_start();

// Check of gebruiker is ingelogd
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$error = '';
$success = '';

// Nieuwe KPI opslaan
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add_kpi') {
    $title = trim($_POST['title'] ?? '');
    $coordinator = trim($_POST['coordinator'] ?? '');
    $current = intval($_POST['current'] ?? 0);
    $target = intval($_POST['target'] ?? 100);
    $status = trim($_POST['status'] ?? 'niet-gestart');

    // Validatie
    if (empty($title)) {
        $error = 'Titel is verplicht';
    } elseif (empty($coordinator)) {
        $error = 'Coördinator is verplicht';
    } elseif (!in_array($coordinator, ['gmt', 'glt', 'gst'])) {
        $error = 'Ongeldige coördinator';
    } elseif ($current < 0) {
        $error = 'Huidige waarde moet minimaal 0 zijn';
    } elseif ($target <= 0) {
        $error = 'Doelwaarde moet groter dan 0 zijn';
    } else {
        // KPI ID genereren (hoogste ID + 1)
        try {
            $pdo = new PDO("sqlite:" . __DIR__ . "/lions.db");
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

            // Haal hoogste kpi_id op
            $stmt = $pdo->query("SELECT CAST(kpi_id AS INTEGER) as max_id FROM kpi ORDER BY CAST(kpi_id AS INTEGER) DESC LIMIT 1");
            $result = $stmt->fetch(PDO::FETCH_ASSOC);
            $new_id = $result ? ($result['max_id'] + 1) : 1;

            // Percentage berekenen
            $percentage = round(($current / $target) * 100);
            $percentage = min(100, max(0, $percentage)); // Clamp tussen 0 en 100

            // Nieuwe KPI toevoegen - 6 kolommen: kpi_id, title, coordinator, current, target, percentage, status
            $stmt = $pdo->prepare("
                INSERT INTO kpi (kpi_id, title, coordinator, current, target, percentage, status)
                VALUES (?, ?, ?, ?, ?, ?, ?)
            ");
            $stmt->execute([$new_id, $title, $coordinator, $current, $target, $percentage, $status]);

            $success = "Nieuwe KPI #$new_id succesvol toegevoegd!";

        } catch (PDOException $e) {
            $error = "Database fout: " . $e->getMessage();
        }
    }
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nieuwe KPI Toevoegen - Lions Club Dashboard</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            padding: 40px;
            max-width: 600px;
            width: 100%;
        }
        .header {
            text-align: center;
            margin-bottom: 30px;
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 24px;
        }
        .subtitle {
            color: #666;
            font-size: 14px;
        }
        .alert {
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 8px;
            color: #555;
            font-weight: 600;
        }
        input, select, textarea {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 14px;
            font-family: inherit;
        }
        input:focus, select:focus, textarea:focus {
            outline: none;
            border-color: #667eea;
        }
        textarea {
            min-height: 100px;
            resize: vertical;
        }
        button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s;
        }
        button:hover {
            transform: translateY(-2px);
        }
        .btn-secondary {
            background: #6c757d;
            margin-top: 10px;
        }
        .btn-secondary:hover {
            background: #5a6268;
        }
        .info-box {
            background: #e3f2fd;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            border: 1px solid #90caf9;
        }
        .info-box h3 {
            color: #1976d2;
            margin-bottom: 10px;
            font-size: 14px;
        }
        .info-box ul {
            margin-left: 20px;
            color: #424242;
        }
        .info-box li {
            margin: 5px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>📝 Nieuwe KPI Toevoegen</h1>
            <p class="subtitle">Voeg een nieuwe doelstelling toe aan het Lions Club District 110AN dashboard</p>
        </div>

        <?php if ($success): ?>
            <div class="alert alert-success"><?php echo htmlspecialchars($success); ?></div>
        <?php endif; ?>

        <?php if ($error): ?>
            <div class="alert alert-error"><?php echo htmlspecialchars($error); ?></div>
        <?php endif; ?>

        <div class="info-box">
            <h3>💡 Tips voor het opstellen van KPI's:</h3>
            <ul>
                <li>Wees specifiek - beschrijf concreet wat je wilt bereiken</li>
                <li>Maak meetbaar - gebruik nummers waar mogelijk (bijv. "Minimaal 5 clubs")</li>
                <li>Kies een realistisch doel - stel doelen die haalbaar zijn binnen het jaar</li>
                <li>Wijs de juiste coördinator toe voor ondersteuning</li>
            </ul>
        </div>

        <form method="POST">
            <input type="hidden" name="action" value="add_kpi">

            <div class="form-group">
                <label for="title">Doelstelling / Titel *</label>
                <textarea
                    id="title"
                    name="title"
                    required
                    placeholder="Bijvoorbeeld: Organiseren van 2 kabinetsvergaderingen in Q1"
                ><?php echo isset($_POST['title']) ? htmlspecialchars($_POST['title']) : ''; ?></textarea>
            </div>

            <div class="form-group">
                <label for="coordinator">Coördinator *</label>
                <select id="coordinator" name="coordinator" required>
                    <option value="">-- Selecteer coördinator --</option>
                    <option value="gmt" <?php echo (isset($_POST['coordinator']) && $_POST['coordinator'] === 'gmt') ? 'selected' : ''; ?>>GMT - Global Membership Team</option>
                    <option value="glt" <?php echo (isset($_POST['coordinator']) && $_POST['coordinator'] === 'glt') ? 'selected' : ''; ?>>GLT - Global Leadership Team</option>
                    <option value="gst" <?php echo (isset($_POST['coordinator']) && $_POST['coordinator'] === 'gst') ? 'selected' : ''; ?>>GST - Global Service Team</option>
                </select>
            </div>

            <div class="form-group">
                <label for="current">Huidige Waarde *</label>
                <input
                    type="number"
                    id="current"
                    name="current"
                    value="<?php echo isset($_POST['current']) ? intval($_POST['current']) : '0'; ?>"
                    min="0"
                    required
                    placeholder="Hoeveel heb je al bereikt?"
                >
            </div>

            <div class="form-group">
                <label for="target">Doelwaarde *</label>
                <input
                    type="number"
                    id="target"
                    name="target"
                    value="<?php echo isset($_POST['target']) ? intval($_POST['target']) : '100'; ?>"
                    min="1"
                    required
                    placeholder="Wat is je doel?"
                >
            </div>

            <div class="form-group">
                <label for="status">Status *</label>
                <select id="status" name="status" required>
                    <option value="niet-gestart" <?php echo (!isset($_POST['status']) || $_POST['status'] === 'niet-gestart') ? 'selected' : ''; ?>>Niet gestart</option>
                    <option value="behandeling" <?php echo (isset($_POST['status']) && $_POST['status'] === 'behandeling') ? 'selected' : ''; ?>>In behandeling</option>
                    <option value="voltooid" <?php echo (isset($_POST['status']) && $_POST['status'] === 'voltooid') ? 'selected' : ''; ?>>Voltooid</option>
                </select>
            </div>

            <button type="submit">➕ KPI Toevoegen</button>

            <a href="admin.php">
                <button type="button" class="btn-secondary">← Terug naar Admin Panel</button>
            </a>
        </form>
    </div>
</body>
</html>
