<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

require_once '/home12/fiberule/public_html/kartikthanaweddingdinner/config/db.php';

// 1. HANDLE MANUAL ADDITION
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['manual_add'])) {
    $name = $_POST['name'] ?? '';
    $pax = $_POST['pax'] ?? 1;
    $status = "Manual"; 

    if (!empty($name)) {
        try {
            $stmt = $pdo->prepare("INSERT INTO rsvps (name, status, pax, created_at) VALUES (?, ?, ?, NOW())");
            $stmt->execute([$name, $status, (int)$pax]);
            header("Location: " . $_SERVER['PHP_SELF']);
            exit;
        } catch (PDOException $e) {
            die("Database Error: " . $e->getMessage());
        }
    }
}

// 2. FETCH DATA
$rsvps = $pdo->query("SELECT * FROM rsvps ORDER BY created_at DESC")->fetchAll();
$wishes = $pdo->query("SELECT * FROM wishes ORDER BY created_at DESC")->fetchAll();

// 3. TOTALS
$totalPax = array_sum(array_column($rsvps, 'pax')); 
$totalRsvps = count($rsvps);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wedding Guest Management | Admin</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
    :root {
        --primary: #0b1b33;
        --accent: #d4af37; /* Gold */
        --bg: #f8f9fa;
        --white: #ffffff;
        --text: #333;
        --success: #2e7d32;
    }

    body { margin:0; font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; }
    
    /* Premium Header */
    .header { background: var(--primary); color: var(--white); padding: 30px 40px; display: flex; justify-content: space-between; align-items: center; border-bottom: 4px solid var(--accent); }
    .header h1 { margin:0; font-weight: 300; letter-spacing: 2px; font-size: 24px; text-transform: uppercase; }
    
    .container { padding: 40px; max-width: 1200px; margin: 0 auto; }

    /* Summary Stats */
    .stats-container { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 20px; margin-bottom: 40px; }
    .stat-card { background: var(--white); padding: 25px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); text-align: center; border-bottom: 3px solid var(--accent); transition: transform 0.3s ease; }
    .stat-card:hover { transform: translateY(-5px); }
    .stat-card h4 { margin: 0 0 10px 0; color: #888; text-transform: uppercase; font-size: 12px; letter-spacing: 1px; }
    .stat-card .count { font-size: 36px; font-weight: bold; color: var(--primary); }

    /* Modern Cards */
    .card { background: var(--white); border-radius: 15px; padding: 30px; margin-bottom: 30px; box-shadow: 0 10px 30px rgba(0,0,0,0.04); border: 1px solid #eaeaea; }
    .card h3 { margin-top:0; color: var(--primary); font-weight: 600; font-size: 20px; border-left: 4px solid var(--accent); padding-left: 15px; margin-bottom: 25px; }

    /* Styled Form */
    .manual-form { display: flex; gap: 15px; align-items: flex-end; flex-wrap: wrap; background: #fdfdfd; padding: 20px; border-radius: 10px; border: 1px solid #eee; }
    .form-group { display: flex; flex-direction: column; }
    .form-group label { font-size: 13px; font-weight: 600; margin-bottom: 8px; color: var(--primary); }
    input { padding: 12px; border: 1px solid #ddd; border-radius: 8px; transition: border 0.3s; font-size: 15px; }
    input:focus { outline: none; border-color: var(--accent); }
    .btn-add { background: var(--primary); color: white; border: none; padding: 12px 25px; border-radius: 8px; cursor: pointer; font-weight: bold; transition: 0.3s; }
    .btn-add:hover { background: #152a4a; box-shadow: 0 4px 10px rgba(11,27,51,0.2); }

    /* Premium Table */
    .table-responsive { overflow-x: auto; }
    table { width:100%; border-collapse: collapse; }
    th { text-align: left; padding: 18px; border-bottom: 2px solid #eee; color: #999; font-weight: 600; font-size: 13px; text-transform: uppercase; }
    td { padding: 18px; border-bottom: 1px solid #f1f1f1; font-size: 15px; }
    tr:hover { background: #fafafa; }
    
    .badge { padding: 5px 12px; border-radius: 20px; font-size: 12px; font-weight: bold; text-transform: uppercase; }
    .badge-online { background: #e8f5e9; color: var(--success); }
    .badge-manual { background: #e3f2fd; color: #1976d2; }
    
    .pax-num { background: var(--primary); color: white; padding: 4px 10px; border-radius: 6px; font-weight: bold; }
    .delete-link { color: #ff5252; text-decoration: none; font-size: 13px; font-weight: 600; padding: 8px 15px; border-radius: 6px; border: 1px solid #ff5252; transition: 0.3s; }
    .delete-link:hover { background: #ff5252; color: white; }

    .wish-text { font-style: italic; color: #555; background: #fff9e6; padding: 10px; border-radius: 8px; border-left: 3px solid var(--accent); }
</style>
</head>
<body>

<div class="header">
    <h1>Wedding Guest Concierge</h1>
    <div>Welcome, Admin</div>
</div>

<div class="container">

    <div class="stats-container">
        <div class="stat-card">
            <h4>Total Confirmed Seats</h4>
            <div class="count"><?= $totalPax ?></div>
        </div>
        <div class="stat-card">
            <h4>Guest Groups</h4>
            <div class="count"><?= $totalRsvps ?></div>
        </div>
        <div class="stat-card">
            <h4>Wishes Received</h4>
            <div class="count"><?= count($wishes) ?></div>
        </div>
    </div>

    <div class="card">
        <h3>Add Individual Guest</h3>
        <form method="POST" class="manual-form">
            <div class="form-group">
                <label>Guest Full Name</label>
                <input type="text" name="name" required placeholder="e.g. John Doe">
            </div>
            <div class="form-group">
                <label>Seats (Pax)</label>
                <input type="number" name="pax" value="1" min="1" style="width: 80px;">
            </div>
            <input type="hidden" name="manual_add" value="1">
            <button type="submit" class="btn-add">Confirm Guest</button>
        </form>
    </div>

    <div class="card">
        <h3>RSVP Guest Registry</h3>
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Guest Name</th>
                        <th>Source</th>
                        <th>Pax</th>
                        <th>Added On</th>
                        <th style="text-align: right;">Management</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($rsvps as $r): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($r['name']) ?></strong></td>
                        <td>
                            <span class="badge <?= ($r['status'] == 'Manual' ? 'badge-manual' : 'badge-online') ?>">
                                <?= ($r['status'] == 'Manual' ? 'Manual Entry' : 'Online RSVP') ?>
                            </span>
                        </td>
                        <td><span class="pax-num"><?= $r['pax'] ?></span></td>
                        <td style="color: #999; font-size: 13px;"><?= date('M d, Y', strtotime($r['created_at'])) ?></td>
                        <td style="text-align: right;">
                            <a class="delete-link" href="delete.php?type=rsvp&id=<?= $r['id'] ?>"
                            onclick="return confirm('Remove this guest from the registry?')">Remove</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

    <div class="card">
        <h3>Guest Wishes & Messages</h3>
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Guest</th>
                        <th>Message</th>
                        <th style="text-align: right;">Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($wishes as $w): ?>
                    <tr>
                        <td style="width: 250px;"><strong><?= htmlspecialchars($w['name']) ?></strong></td>
                        <td><div class="wish-text">"<?= htmlspecialchars($w['message']) ?>"</div></td>
                        <td style="text-align: right;">
                            <a class="delete-link" href="delete.php?type=wish&id=<?= $w['id'] ?>"
                            onclick="return confirm('Delete this wish?')">Delete</a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>
</body>
</html>