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

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

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

if (isset($_POST['update'])) {
  $category = $_POST['category'];
  $status = $_POST['status'];

  $sql = "UPDATE tblcategory SET CategoryName = :category, Status = :status WHERE id = :catid";
  $query = $dbh->prepare($sql);
  $query->bindParam(':category', $category, PDO::PARAM_STR);
  $query->bindParam(':status', $status, PDO::PARAM_STR);
  $query->bindParam(':catid', $catid, PDO::PARAM_STR);
  $query->execute();

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

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Edit Category | 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 6 -->
  <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

  <style>
    .square-card {
      width: 100%;
      background-color: #f8f9fa;
      border: 1px solid #dee2e6;
      box-shadow: 0 4px 8px rgba(0,0,0,0.05);
      border-radius: 5px;
    }
    .form-label {
      font-weight: 600;
    }
    .flash-message {
      max-width: 600px;
      margin: 0 auto 15px;
    }
    .main-content {
      margin-left: 180px;
      margin-top: 50px;
      padding: 20px;
    }
    .header-line {
      color: #2258d5;
      font-size: 20px;
      font-weight: 600;
      margin-bottom: 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-edit mr-2"></i>Edit Category</h5>
        <!-- <a href="manage-categories.php" class="btn btn-secondary btn-sm">
          <i class="fas fa-arrow-left"></i> Back to List
        </a> -->
      </div>
    </div>

    <!-- Flash Messages -->
    <div class="row justify-content-center">
      <div class="col-md-8">
        <?php if (!empty($_SESSION['error'])): ?>
          <div class="alert alert-danger flash-message">
            <strong>Error:</strong> <?= htmlentities($_SESSION['error']); ?>
            <?php $_SESSION['error'] = ""; ?>
          </div>
        <?php endif; ?>
      </div>
    </div>

    <!-- Edit Category Form -->
    <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 Category
          </div>
          <div class="card-body">
            <?php
            $sql = "SELECT * FROM tblcategory WHERE id = :catid";
            $query = $dbh->prepare($sql);
            $query->bindParam(':catid', $catid, PDO::PARAM_STR);
            $query->execute();
            $result = $query->fetch(PDO::FETCH_OBJ);
            if ($result):
            ?>
            <form method="post">
              <div class="form-group">
                <label for="category" class="form-label">Category Name</label>
                <input type="text" name="category" id="category" class="form-control" value="<?= htmlentities($result->CategoryName); ?>" required>
              </div>

              <div class="form-group">
                <label class="form-label d-block">Status</label>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="status" id="active" value="1" <?= ($result->Status == 1) ? 'checked' : ''; ?>>
                  <label class="form-check-label" for="active">Active</label>
                </div>
                <div class="form-check form-check-inline">
                  <input class="form-check-input" type="radio" name="status" id="inactive" value="0" <?= ($result->Status == 0) ? 'checked' : ''; ?>>
                  <label class="form-check-label" for="inactive">Inactive</label>
                </div>
              </div>

              <div class="d-flex justify-content-between">
                <a href="manage-categories.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 Category
                </button>
              </div>
            </form>
            <?php else: ?>
              <p class="text-danger">Invalid category ID.</p>
            <?php endif; ?>
          </div>
        </div>
      </div>
    </div>

  </div>
</div>

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

<!-- JS -->
<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>
