<?php
session_start();
error_reporting(0);
include('includes/config.php');

if (strlen($_SESSION['staff_login']) == 0) {
    header('location:index.php');
    exit;
}

$staff_id = $_SESSION['staffid']; // Staff ID

// Filters
$bookname = $_GET['bookname'] ?? '';
$from = $_GET['from'] ?? '';
$to = $_GET['to'] ?? '';
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Staff - Not Returned Books | Library System</title>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="../<?php echo $_SESSION['logo']; ?>">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
  <link href="https://cdn.datatables.net/1.13.6/css/dataTables.bootstrap4.min.css" rel="stylesheet">
  <link href="https://cdn.datatables.net/buttons/2.4.1/css/buttons.bootstrap4.min.css" rel="stylesheet">

  <style>
    .square-card { background-color: #f8f9fa; border-radius: 8px; border: 1px solid #dee2e6; box-shadow: 0 4px 12px rgba(0,0,0,0.05); }
    .header-line { font-size: 24px; font-weight: 600; color:   #0d6efd; margin-bottom: 20px; }
    .badge-overdue { background-color: #dc3545; color: #fff; padding: 5px 10px; border-radius: 15px; font-size: 0.85rem; }
    .badge-due-soon { background-color: #ffc107; color: #212529; padding: 5px 10px; border-radius: 15px; font-size: 0.85rem; }
    .badge-not-returned { background-color: #6c757d; color: #fff; padding: 5px 10px; border-radius: 15px; font-size: 0.85rem; }
    .table-wrapper { overflow-x: auto; padding: 10px; }
    .table thead th { background-color: #dee2e6; font-weight: 600; }
  </style>
</head>

<body>
<div class="d-flex flex-column min-vh-100">
<?php include('includes/header.php'); ?>

<div class="main-content flex-grow-1">
  <div class="row mb-3">
    <div class="col-md-12">
      <h5 class="header-line" style="color:#0d6efd; font-size:20px;"><i class="fa fa-book mr-2"></i>NOT RETURNED BOOKS</h5>
    </div>
  </div>

  <!-- Filter Form -->
  <form method="get" class="row mb-4">
    <div class="col-md-4 mb-2">
      <input type="text" name="bookname" class="form-control" placeholder="Book Name" value="<?= htmlentities($bookname) ?>">
    </div>
    <div class="col-md-3 mb-2">
      <input type="date" name="from" class="form-control" value="<?= htmlentities($from) ?>">
    </div>
    <div class="col-md-3 mb-2">
      <input type="date" name="to" class="form-control" value="<?= htmlentities($to) ?>">
    </div>
    <div class="col-md-2 mb-2">
      <button class="btn btn-primary btn-block" type="submit"><i class="fa fa-filter"></i> Filter</button>
    </div>
  </form>

  <!-- Not Returned Books Table -->
  <div class="card square-card mb-5">
    <div class="card-header bg-secondary text-white">
      <i class="fa fa-book-reader mr-2"></i> Books Not Yet Returned
    </div>
    <div class="card-body table-wrapper">
      <table id="notReturnedBooksTable" class="table table-bordered table-hover table-sm text-dark">
        <thead>
          <tr>
            <th>#</th>
            <th>Book</th>
            <th>Copy No</th>
            <th>ISBN</th>
            <th>Issued Date</th>
            <th>Due Date</th>
            <th>Status</th>
            <th>Fine (₹)</th>
          </tr>
        </thead>
        <tbody>
<?php
$sql = "SELECT ib.BookId, ib.CopyId, ib.IssuesDate, ib.DueDate, ib.fine,
               b.BookName, b.ISBNNumber, bc.book_no
        FROM tblissuedbookdetails ib
        JOIN tblbooks b ON b.id = ib.BookId
        LEFT JOIN tblbookcopies bc ON bc.id = ib.CopyId
        WHERE ib.BorrowerID = :staff_id AND ib.BorrowerType='staff' AND ib.ReturnDate IS NULL";

if($bookname) $sql .= " AND b.BookName LIKE :bookname";
if($from) $sql .= " AND ib.IssuesDate >= :from";
if($to) $sql .= " AND ib.IssuesDate <= :to";

$sql .= " ORDER BY ib.id DESC";

$query = $dbh->prepare($sql);
$query->bindParam(':staff_id', $staff_id, PDO::PARAM_STR);
if($bookname) { $bk = "%$bookname%"; $query->bindParam(':bookname', $bk, PDO::PARAM_STR); }
if($from) $query->bindParam(':from', $from, PDO::PARAM_STR);
if($to) $query->bindParam(':to', $to, PDO::PARAM_STR);

$query->execute();
$results = $query->fetchAll(PDO::FETCH_OBJ);
$cnt = 1;
$today = new DateTime();

// if($results):
    foreach($results as $row):
        $due = new DateTime($row->DueDate);
?>
<tr>
  <td><?= $cnt++; ?></td>
  <td><?= htmlentities($row->BookName); ?></td>
  <td><?= htmlentities($row->book_no ?? '-'); ?></td>
  <td><?= htmlentities($row->ISBNNumber); ?></td>
  <td><?= htmlentities($row->IssuesDate); ?></td>
  <td>
    <?php
      if($today > $due) echo "<span class='badge badge-overdue'>Overdue</span>";
      elseif($today->diff($due)->days <= 2) echo "<span class='badge badge-due-soon'>Due Soon</span>";
      else echo htmlentities($row->DueDate);
    ?>
  </td>
  <td><span class="badge badge-not-returned">Not Returned</span></td>
  <td><?= htmlentities($row->fine ?? 0); ?></td>
</tr>
<?php
    endforeach;
// else:
//     echo "<tr><td colspan='8' class='text-center text-muted'>No not returned books.</td></tr>";
// endif;
?>
        </tbody>
      </table>
    </div>
  </div>
</div>

<?php include('includes/footer.php'); ?>
</div>

<script src="https://code.jquery.com/jquery-3.7.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/dataTables.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.bootstrap4.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>

<script>
$(document).ready(function () {
  $('#notReturnedBooksTable').DataTable({
    dom: 'Bfrtip',
    buttons: [
      { extend: 'excelHtml5', className: 'btn btn-success', title: 'Staff_Not_Returned_Books', text: '<i class="fa fa-file-excel"></i> Excel' },
      { extend: 'print', className: 'btn btn-info', title: 'Staff - Not Returned Books', text: '<i class="fa fa-print"></i> Print' }
    ],
    pageLength: 10,
    responsive: true,
    language: { search: "🔍 Search books:" }
  });
});
</script>
</body>
</html>
