<?php
/**
 * Admin Page - admin.php
 * View all responses from the database
 * Simple password-protected admin panel
 */

require_once 'config.php';

// Simple password protection (change this!)
define('ADMIN_PASSWORD', 'love2024');

$isAuthenticated = false;
$errorMessage = '';

// Handle login
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['password'])) {
    if ($_POST['password'] === ADMIN_PASSWORD) {
        $isAuthenticated = true;
        $_SESSION['admin_authenticated'] = true;
    } else {
        $errorMessage = 'Incorrect password';
    }
}

// Check session
if (isset($_SESSION['admin_authenticated'])) {
    $isAuthenticated = true;
}

// Handle delete
if ($isAuthenticated && isset($_GET['delete'])) {
    try {
        $pdo = getDBConnection();
        $stmt = $pdo->prepare('DELETE FROM responses WHERE id = :id');
        $stmt->execute([':id' => (int)$_GET['delete']]);
        header('Location: admin.php');
        exit;
    } catch (Exception $e) {
        $errorMessage = 'Error deleting response';
    }
}

// Get all responses
$responses = [];
if ($isAuthenticated) {
    try {
        $pdo = getDBConnection();
        $stmt = $pdo->query('SELECT * FROM responses ORDER BY timestamp DESC');
        $responses = $stmt->fetchAll();
    } catch (Exception $e) {
        $errorMessage = 'Error loading responses';
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Panel 💖</title>
    <link rel="stylesheet" href="style.css">
    <style>
        .admin-container {
            max-width: 1000px;
            width: 100%;
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0, 0, 0, 0.1);
        }
        
        .admin-container h1 {
            color: var(--color-rose);
            margin-bottom: 30px;
            font-family: var(--font-display);
        }
        
        .login-form {
            max-width: 300px;
            margin: 50px auto;
            text-align: center;
        }
        
        .login-form input {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 2px solid var(--color-blush);
            border-radius: 8px;
            font-size: 1rem;
        }
        
        .login-form button {
            width: 100%;
        }
        
        .error-message {
            color: #c9184a;
            margin-bottom: 20px;
            padding: 10px;
            background: #ffebee;
            border-radius: 8px;
        }
        
        .responses-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        .responses-table thead {
            background: var(--color-light);
        }
        
        .responses-table th {
            padding: 12px;
            text-align: left;
            color: var(--color-deep-rose);
            font-weight: 600;
            border-bottom: 2px solid var(--color-blush);
        }
        
        .responses-table td {
            padding: 12px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .responses-table tr:hover {
            background: #fafafa;
        }
        
        .delete-btn {
            background: #f44336;
            color: white;
            padding: 6px 12px;
            border-radius: 6px;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
            transition: all 0.3s ease;
        }
        
        .delete-btn:hover {
            background: #da190b;
        }
        
        .success-message {
            color: #4CAF50;
            margin-bottom: 20px;
            padding: 10px;
            background: #e8f5e9;
            border-radius: 8px;
        }
        
        .stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: linear-gradient(135deg, var(--color-rose) 0%, var(--color-deep-rose) 100%);
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
        }
        
        .stat-card h3 {
            margin: 0;
            font-size: 1.2rem;
            opacity: 0.9;
        }
        
        .stat-card .number {
            font-size: 2.5rem;
            font-weight: 700;
            margin: 10px 0;
            font-family: var(--font-display);
        }
        
        .logout-btn {
            background: #999;
            margin-top: 20px;
        }
        
        .logout-btn:hover {
            background: #777;
        }
    </style>
</head>
<body>
    <div class="animated-bg"></div>
    <div class="hearts-container"></div>
    
    <div class="page" style="padding: 40px 20px;">
        <div class="admin-container">
            <h1>💖 Admin Panel</h1>
            
            <?php if (!$isAuthenticated): ?>
                <!-- Login Form -->
                <div class="login-form">
                    <h2 style="color: var(--color-rose); margin-bottom: 20px;">Admin Login</h2>
                    <?php if ($errorMessage): ?>
                        <div class="error-message"><?php echo $errorMessage; ?></div>
                    <?php endif; ?>
                    <form method="POST">
                        <input type="password" name="password" placeholder="Enter admin password" required>
                        <button type="submit" class="btn-primary">Login</button>
                    </form>
                </div>
                
            <?php else: ?>
                <!-- Admin Dashboard -->
                <?php if ($errorMessage): ?>
                    <div class="error-message"><?php echo $errorMessage; ?></div>
                <?php endif; ?>
                
                <!-- Statistics -->
                <div class="stats">
                    <div class="stat-card">
                        <h3>Total Responses</h3>
                        <div class="number"><?php echo count($responses); ?></div>
                    </div>
                    <div class="stat-card">
                        <h3>YES Responses</h3>
                        <div class="number"><?php echo count(array_filter($responses, fn($r) => $r['response'] === 'YES')); ?></div>
                    </div>
                </div>
                
                <!-- Search -->
                <div style="margin-bottom: 20px;">
                    <input type="text" id="searchInput" placeholder="🔍 Search by name..." 
                           style="width: 100%; padding: 10px; border: 2px solid var(--color-blush); border-radius: 8px;">
                </div>
                
                <!-- Responses Table -->
                <?php if (count($responses) > 0): ?>
                    <table class="responses-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Response</th>
                                <th>Timestamp</th>
                                <th>IP Address</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($responses as $resp): ?>
                                <tr>
                                    <td><?php echo $resp['id']; ?></td>
                                    <td><?php echo sanitize($resp['name']); ?></td>
                                    <td>
                                        <strong style="color: #4CAF50;">✓ <?php echo $resp['response']; ?></strong>
                                    </td>
                                    <td><?php echo date('M d, Y H:i', strtotime($resp['timestamp'])); ?></td>
                                    <td><?php echo $resp['ip_address']; ?></td>
                                    <td>
                                        <a href="?delete=<?php echo $resp['id']; ?>" class="delete-btn" 
                                           onclick="return confirm('Delete this response?')">Delete</a>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                <?php else: ?>
                    <p style="text-align: center; color: #999; margin-top: 30px;">
                        No responses yet. She hasn't said YES yet! 💕
                    </p>
                <?php endif; ?>
                
                <form method="POST" style="text-align: center;">
                    <button type="submit" name="logout" class="btn-secondary logout-btn" 
                            onclick="if(confirm('Logout?')) window.location='index.php'">
                        Logout
                    </button>
                </form>
            <?php endif; ?>
        </div>
    </div>

    <script src="script.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', () => {
            if (document.getElementById('searchInput')) {
                initAdminSearch();
            }
        });
    </script>
</body>
</html>
