<?php
session_name('mini_admin_session');
session_start();
error_reporting(0);
include('includes/dbconnection.php');
require_once 'includes/session_term_helper.php';
$stResult = resolveCurrentSessionTerm($con);
$currentSessionName = $stResult['session_name'];
$currentTermName    = $stResult['term_name'];

if (strlen($_SESSION['sid']) == 0) {
    header('location:logoutmini.php');
}
?>

<!DOCTYPE html>
<html>
<?php @include("includes/head.php"); ?>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">
        <?php @include("includes/header2.php"); ?>
        <?php @include("includes/sidebar2.php"); ?>

        <div class="content-wrapper">
<section class="content-header premium-header">
    <div class="container-fluid">
        <div class="header-glass-card">
            <div class="row align-items-center">
                <div class="col-md-8">
                    <div class="title-section">
                        <div class="icon-box bg-primary-gradient shadow-sm">
                            <i class="fas fa-calendar"></i>
                        </div>
                        <div>
                            <h1 class="premium-title text-primary">
                                Event Management
                                <span class="premium-subtitle text-secondary">Email Logs</span>
                            </h1>
                    <div class="mt-1">
                      <?php if ($currentSessionName): ?>
                        <span class="badge badge-primary mr-1">
                          <i class="fas fa-calendar-alt"></i>
                          <?= htmlspecialchars($currentSessionName) ?>
                        </span>
                      <?php endif; ?>
                      <?php if ($currentTermName): ?>
                        <span class="badge badge-success">
                          <i class="fas fa-bookmark"></i>
                          <?= htmlspecialchars($currentTermName) ?>
                        </span>
                      <?php else: ?>
                        <span class="badge badge-warning">
                          <i class="fas fa-exclamation-triangle"></i> No active term
                        </span>
                      <?php endif; ?>
                    </div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb premium-breadcrumb">
                            <li class="breadcrumb-item"><a href="dashboard1.php" class="text-primary">Home</a></li>
                            <li class="breadcrumb-item active" aria-current="page">Email Logs</li>
                        </ol>
                    </nav>
                </div>
            </div>
        </div>
    </div>
</section> 

            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-12">
                            <div class="card card-primary">
                                <div class="card-header">
                                    <h5 class="card-title font-weight-bold"><i class="fas fa-newspaper"></i> Newsletter Email Logs</h5>
                                </div>
                                <div class="card-body mt-2">
                                    <table id="example1" class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th>#</th>
                                                <th>Recipient</th>
                                                <th>Sender Email</th>
                                                <th>Subject</th>
                                                <th>Status</th>
                                                <th>Timestamp</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $result = mysqli_query($con, "SELECT * FROM email_logs ORDER BY timestamp DESC");
                                            $cnt = 1;
                                            if (mysqli_num_rows($result) > 0) {
                                                while ($row = mysqli_fetch_assoc($result)) {
                                                    echo "<tr>
                                            <td>{$cnt}</td>
                                            <td>{$row['recipient']}</td>
                                            <td>{$row['sender_email']}</td>
                                            <td>{$row['subject']}</td>
                                            <td><span class='badge bg-" . ($row['status'] == 'Success' ? 'success' : 'danger') . "'>{$row['status']}</span></td>
                                            <td>{$row['timestamp']}</td>
                                        </tr>";
                                                    $cnt++;
                                                }
                                            } else {
                                                echo "<tr><td colspan='4' class='text-center'>No logs available.</td></tr>";
                                            }
                                            ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>

        </div>
        <?php @include("includes/footer.php"); ?>
    </div>
    <?php @include("includes/foot.php"); ?>
</body>

</html>