<?php
session_start();
error_reporting(0);
include('includes/config.php');
                         
if (strlen($_SESSION['alogin']) == 0) {
  header('location:index.php');
  exit;
}

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

  $sql = "INSERT INTO tblcategory(CategoryName, Status) VALUES(:category, :status)";
  $query = $dbh->prepare($sql);
  $query->bindParam(':category', $category, PDO::PARAM_STR);
  $query->bindParam(':status', $status, PDO::PARAM_STR);
  $query->execute();
  $lastInsertId = $dbh->lastInsertId();

  if ($lastInsertId) {
    $_SESSION['msg'] = "Category created successfully";
    header('location:manage-categories.php');
  } else {
    $_SESSION['error'] = "Something went wrong. Please try again";
    header('location:manage-categories.php');
  }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <title>Add 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">
        <h5 class="header-line"><i class="fas fa-folder-plus mr-2"></i>Add Category</h5>
      </div>
    </div>

    <!-- Flash Messages -->
    <div class="row justify-content-center">
      <div class="col-md-8">
        <?php foreach (['error', 'msg'] as $type): ?>
          <?php if(!empty($_SESSION[$type])): ?>
            <div class="alert alert-<?php echo ($type == 'error') ? 'danger' : 'success'; ?> flash-message">
              <strong><?php echo ucfirst($type); ?> :</strong> <?php echo htmlentities($_SESSION[$type]); ?>
              <?php $_SESSION[$type] = ""; ?>
            </div>
          <?php endif; ?>
        <?php endforeach; ?>
      </div>
    </div>

    <!-- Add 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-plus-circle mr-2"></i> New Category Form
          </div>
          <div class="card-body">
            <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" required autocomplete="off">
              </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" 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">
                  <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="create" class="btn btn-success">
                  <i class="fas fa-save"></i> Create Category
                </button>
              </div>
            </form>
          </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>
