<?php
session_start();
include 'db.php';

if (!isset($_SESSION['admin'])) {
    header("Location: login.php");
    exit;
}

// Fetch registration users
$users = $conn->query("SELECT * FROM usermaster ORDER BY id DESC");

// Fetch login logs
$logins = $conn->query("SELECT email, fullname, ip_address, login_time FROM login_logs ORDER BY id DESC");
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Admin Dashboard</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-dark bg-dark">
  <div class="container-fluid">
    <span class="navbar-brand mb-0 h1">Admin Dashboard</span>
    <a href="logout.php" class="btn btn-outline-light btn-sm">Logout</a>
  </div>
</nav>

<div class="container mt-4">
  <h3 class="mb-4 text-center">Admin Dashboard</h3>

  <!-- Tabs Navigation -->
  <ul class="nav nav-tabs" id="dashboardTabs" role="tablist">
    <li class="nav-item" role="presentation">
      <button class="nav-link active" id="register-tab" data-bs-toggle="tab" data-bs-target="#register" type="button" role="tab">Registration Details</button>
    </li>
    <li class="nav-item" role="presentation">
      <button class="nav-link" id="loginlog-tab" data-bs-toggle="tab" data-bs-target="#loginlog" type="button" role="tab">Login Log Details</button>
    </li>
  </ul>


  

  <!-- Tabs Content -->
  <div class="tab-content mt-3" id="dashboardTabsContent">

    <!-- Registration Details -->
    <div class="tab-pane fade show active" id="register" role="tabpanel">
      <div class="d-flex justify-content-end mb-2">
        <a href="registration_report.php" class="btn btn-success btn-sm">
          📊 Download Registration Excel
        </a>
      </div>
      <div class="table-responsive">
        <table class="table table-bordered table-striped align-middle">
          <thead class="table-dark">
            <tr>
              <th>Full Name</th>
              <th>year</th>
              <th>degree</th>
              <th>Mobile</th>
              <th>Email</th>
              <th>Place</th>
            </tr>
          </thead>
          <tbody>
            <?php while ($row = $users->fetch_assoc()): ?>
            <tr>
              <td><?= htmlspecialchars($row['fullname']) ?></td>
              <td><?= htmlspecialchars($row['year']) ?></td>
              <td><?= htmlspecialchars($row['degree']) ?></td>
              <td><?= htmlspecialchars($row['mobileno']) ?></td>
              <td><?= htmlspecialchars($row['email']) ?></td>
              <td><?= htmlspecialchars($row['place']) ?></td>
            </tr>
            <?php endwhile; ?>
          </tbody>
        </table>
      </div>
    </div>

    <!-- Login Log Details -->
    <div class="tab-pane fade" id="loginlog" role="tabpanel">
      <div class="d-flex justify-content-end mb-2">
        <a href="login_report.php" class="btn btn-primary btn-sm">
          📘 Download Login Excel
        </a>
      </div>
      <div class="table-responsive">
        <table class="table table-bordered table-striped align-middle">
          <thead class="table-dark">
            <tr>
              <th>Full Name</th>
              <th>Email</th>
              <th>IP Address</th>
              <th>Login Time</th>
            </tr>
          </thead>
          <tbody>
            <?php while ($log = $logins->fetch_assoc()): ?>
            <tr>
              <td><?= htmlspecialchars($log['fullname']) ?></td>
              <td><?= htmlspecialchars($log['email']) ?></td>
              <td><?= htmlspecialchars($log['ip_address']) ?></td>
              <td><?= htmlspecialchars($log['login_time']) ?></td>
            </tr>
            <?php endwhile; ?>
          </tbody>
        </table>
      </div>
    </div>

  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
