<?php
session_start();
include('includes/config.php');
function toRoman($number)
{
  $map = [
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1
  ];

  $result = '';
  foreach ($map as $roman => $value) {
    while ($number >= $value) {
      $result .= $roman;
      $number -= $value;
    }
  }
  return $result;
}

if (!isset($_GET['bookid']) || !is_numeric($_GET['bookid'])) {
  echo "Invalid Book ID";
  exit;
}

$bookid = intval($_GET['bookid']);

$sql = "SELECT b.*, c.CategoryName, p.PublisherName,
        r.rack_no, r.shelf_no, r.floor,
        d.department_name, g.degree_name
        FROM tblbooks b
        LEFT JOIN tblbookcopies bc ON bc.book_id = b.id
        LEFT JOIN tblcategory c ON c.id = b.CatId
        LEFT JOIN tblpublishers p ON p.id = b.Publisher
        LEFT JOIN tbl_racks r ON r.id = bc.rack_id
        LEFT JOIN tbldepartment d ON d.department_id = b.department_id
        LEFT JOIN tbldegree g ON g.degree_id = b.degree_id
        WHERE b.id = :bookid";

$query = $dbh->prepare($sql);
$query->bindParam(':bookid', $bookid, PDO::PARAM_INT);
$query->execute();
$book = $query->fetch(PDO::FETCH_OBJ);

if (!$book) {
  echo "Book not found";
  exit;
}

function getAuthorNames($dbh, $authorIds)
{
  if (empty($authorIds))
    return '-';
  $ids = implode(',', array_map('intval', explode(',', $authorIds)));
  $stmt = $dbh->query("SELECT AuthorName FROM tblauthors WHERE id IN ($ids)");
  return implode(', ', $stmt->fetchAll(PDO::FETCH_COLUMN));
}

$authors = getAuthorNames($dbh, $book->AuthorIds);
?>

<table class="table table-bordered table-striped">
  <tr>
    <th>Book Name</th>
    <td><?= htmlentities($book->BookName); ?></td>
  </tr>
  <tr>
    <th>Authors</th>
    <td><?= htmlentities($authors); ?></td>
  </tr>
  <tr>
    <th>Category</th>
    <td><?= htmlentities($book->CategoryName ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Department</th>
    <td><?= htmlentities($book->department_name ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Degree</th>
    <td><?= htmlentities($book->degree_name ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Edition</th>
    <td><?= is_numeric($book->Edition)
      ? toRoman((int) $book->Edition)
      : htmlentities($book->Edition); ?></td>

  </tr>
  <tr>
    <th>Volume</th>
    <td><?= htmlentities($book->Volume ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Publication Year</th>
    <td><?= htmlentities($book->PublicationYear ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Publisher</th>
    <td><?= htmlentities($book->PublisherName ?? '-'); ?></td>
  </tr>
  <tr>
    <th>ISBN</th>
    <td><?= htmlentities($book->ISBNNumber ?? '-'); ?></td>
  </tr>
  <tr>
    <th>Cupboard Info</th>
    <td>
      Cupboard: <?= htmlentities($book->rack_no ?? '-'); ?><br>
      Rack: <?= htmlentities($book->shelf_no ?? '-'); ?><br>
      Book No: <?= htmlentities($book->floor ?? '-'); ?>
    </td>
  </tr>
</table>