<?php
// Existing includes
include 'includes/session.php';
include 'includes/sessions.php';
include 'includes/header.php';
?>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
  <?php include 'includes/navbar.php'; ?>
  <?php include 'includes/menubar.php'; ?>

  <!-- Content Wrapper -->
  <div class="content-wrapper"> 
    <section class="content-header">
      <h1>
        Login History
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Login History</li>
      </ol>
    </section>

    <section class="content">
      <div class="row">
        <div class="col-xs-12">
          <div class="box">
            <div class="box-body">
              <div class="table-responsive">
                <table id="example1" class="table table-bordered">
                  <thead>
                    <tr>
                      <th>User Name</th>
                      <th>Email</th>
                      <th>Logged In</th>
                      <th>Logged Out</th>
                      <th>Reason</th>
                      <th>Action</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
                    $sql = "SELECT u.name, u.email, l.logged_in, l.logged_out, l.reason 
                            FROM login_history l 
                            JOIN users u ON l.user_id = u.id 
                            ORDER BY l.logged_in DESC";
                    $query = $conn->query($sql);
                    while ($row = $query->fetch_assoc()) {
                      echo "
                        <tr>
                          <td>{$row['name']}</td>
                          <td>{$row['email']}</td>
                          <td>{$row['logged_in']}</td>
                          <td>{$row['logged_out']}</td>
                          <td>{$row['reason']}</td>
                          <td>
                            <a href='download_history.php?id={$row['user_id']}' class='btn btn-primary btn-sm'>Download</a>
                          </td>
                        </tr>
                      ";
                    }
                    ?>
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>   
  </div>

  <?php include 'includes/footer.php'; ?>
  <?php include 'includes/scripts.php'; ?>
</div>
</body>
</html>
