<?php
require_once 'includes/auth_check.php';
require_once 'includes/db_connect.php';

$user_id = $_SESSION['user_id'];
$user_data = null;

$sql = "SELECT username, api_key FROM users WHERE id = ?";
if ($stmt = $mysqli->prepare($sql)) {
    $stmt->bind_param("i", $user_id);
    $stmt->execute();
    $result = $stmt->get_result();
    $user_data = $result->fetch_assoc();
    $stmt->close();
}

require_once 'includes/header.php';
?>

<div class="card header-card mb-4">
    <div class="card-body">
        <h1 class="mb-0">Dein Profil</h1>
        <p class="card-text text-white-50 mt-2">Verwalte hier deine Kontoeinstellungen.</p>
    </div>
</div>

<div class="row">
    <div class="col-lg-6 mb-4">
        <table class="table cazubu-table-frameless">
            <thead class="table-dark"><tr><th>Benutzernamen ändern</th></tr></thead>
            <tbody>
                <tr>
                    <td class="p-3">
                        <form id="change-username-form">
                            <input type="hidden" name="action" value="change_username">
                            <div class="mb-3">
                                <label for="username" class="form-label">Neuer Benutzername</label>
                                <input type="text" class="form-control" name="username" id="username" value="<?php echo htmlspecialchars($user_data['username']); ?>" required>
                            </div>
                            <button type="submit" class="btn btn-cazubu">Namen speichern</button>
                        </form>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div class="col-lg-6 mb-4">
        <table class="table cazubu-table-frameless">
            <thead class="table-dark"><tr><th>Passwort ändern</th></tr></thead>
            <tbody>
                <tr>
                    <td class="p-3">
                        <p>Ändere hier dein Passwort für den Login.</p>
                        <button class="btn btn-cazubu" data-bs-toggle="modal" data-bs-target="#changePasswordModal">Passwort ändern</button>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

<div class="row">
    <div class="col-12">
        <table class="table cazubu-table-frameless">
             <thead class="table-dark"><tr><th>API-Key Management</th></tr></thead>
             <tbody>
                 <tr><td class="p-3"><p class="text-muted small mb-2">Dein API-Key wird benötigt, um Sensor-Daten von externen Geräten (z.B. einem ESP32) an Cazubu zu senden.</p><strong>Dein API-Key:</strong><br><input type="text" class="form-control mt-1" value="<?php echo htmlspecialchars($user_data['api_key'] ?? 'Noch kein Key generiert'); ?>" readonly></td></tr>
                 <tr>
                     <td class="p-3 text-end">
                         <form id="generate-api-key-form">
                             <input type="hidden" name="action" value="generate_api_key">
                             <button type="submit" class="btn btn-cazubu">Neuen API-Key generieren</button>
                         </form>
                     </td>
                 </tr>
             </tbody>
        </table>
    </div>
</div>

<div class="modal fade" id="changePasswordModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header"><h5 class="modal-title">Passwort ändern</h5><button type="button" class="btn-close" data-bs-dismiss="modal"></button></div>
            <div class="modal-body">
                <form id="change-password-form">
                    <input type="hidden" name="action" value="change_password">
                    <div class="mb-3"><label for="current_password" class="form-label">Aktuelles Passwort</label><input type="password" class="form-control" name="current_password" id="current_password" required></div>
                    <div class="mb-3"><label for="new_password" class="form-label">Neues Passwort</label><input type="password" class="form-control" name="new_password" id="new_password" required></div>
                    <div class="mb-3"><label for="confirm_new_password" class="form-label">Neues Passwort bestätigen</label><input type="password" class="form-control" name="confirm_new_password" id="confirm_new_password" required></div>
                </form>
            </div>
            <div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Abbrechen</button><button type="submit" class="btn btn-cazubu" form="change-password-form">Neues Passwort speichern</button></div>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
