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

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

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

if (isset($_POST['update'])) {
  $rack_no = $_POST['rack_no'];
  $shelf_no = $_POST['shelf_no'];
  $floor = $_POST['floor'];

  $sql = "UPDATE tbl_racks SET rack_no = :rack_no, shelf_no = :shelf_no, floor = :floor WHERE id = :rackid";
  $query = $dbh->prepare($sql);
  $query->bindParam(':rack_no', $rack_no, PDO::PARAM_STR);
  $query->bindParam(':shelf_no', $shelf_no, PDO::PARAM_STR);
  $query->bindParam(':floor', $floor, PDO::PARAM_STR);
  $query->bindParam(':rackid', $rackid, PDO::PARAM_INT);
  $query->execute();

  $_SESSION['updatemsg'] = "Rack updated successfully";
  header('location:manage-racks.php');
  exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Edit Rack | 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']; ?>">

  <!-- Bootstrap 4.6 -->
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <!-- FontAwesome -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .square-card {
      background-color: #f8f9fa;
      border: 1px solid #dee2e6;
      border-radius: 6px;
      box-shadow: 0 4px 12px rgba(0,0,0,0.05);
    }
    .header-line {
      font-size: 22px;
      font-weight: 600;
      color: #2258d5;
    }
    .main-content {
      margin-left: 180px;
      margin-top: 50px;
      padding: 20px;
    }
  </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-4">
      <div class="col-md-12 d-flex justify-content-between align-items-center">
        <h5 class="header-line"><i class="fas fa-warehouse mr-2"></i>Edit Rack</h5>
      </div>
    </div>

    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card square-card mb-4">
          <div class="card-header bg-secondary text-white">
            <i class="fa fa-pencil-alt mr-2"></i> Update Rack Info
          </div>
          <div class="card-body">
            <?php
            $sql = "SELECT * FROM tbl_racks WHERE id = :rackid";
            $query = $dbh->prepare($sql);
            $query->bindParam(':rackid', $rackid, PDO::PARAM_INT);
            $query->execute();
            $result = $query->fetch(PDO::FETCH_OBJ);

            if ($result):
            ?>
              <form method="post">
                <div class="form-group">
                  <label for="rack_no" class="font-weight-bold">Rack Number</label>
                  <input type="text" name="rack_no" id="rack_no" class="form-control" value="<?= htmlentities($result->rack_no); ?>" required>
                </div>

                <div class="form-group">
                  <label for="shelf_no" class="font-weight-bold">Shelf Number</label>
                  <input type="text" name="shelf_no" id="shelf_no" class="form-control" value="<?= htmlentities($result->shelf_no); ?>" required>
                </div>

                <div class="form-group">
                  <label for="floor" class="font-weight-bold">Floor</label>
                  <input type="text" name="floor" id="floor" class="form-control" value="<?= htmlentities($result->floor); ?>" required>
                </div>

                <div class="d-flex justify-content-between">
                  <a href="manage-racks.php" class="btn btn-secondary">
                    <i class="fas fa-arrow-left"></i> Back to List
                  </a>
                  <button type="submit" name="update" class="btn btn-primary">
                    <i class="fas fa-save"></i> Update Rack
                  </button>
                </div>
              </form>
            <?php else: ?>
              <p class="text-danger">Invalid Rack ID.</p>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

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

<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
