<?php
session_start();
if(!isset($_SESSION['admin_id'])){
  header("Location: index.php");
  exit;
}

include "../db.php";

$message = "";

// Folder path
$uploadDir = "../assets/images/categories/";

// Create folders if not exist
if (!is_dir($uploadDir)) {
    mkdir($uploadDir, 0777, true);
}

if (isset($_POST['save_category'])) {

    $category_name = trim($_POST['category_name']);

    if ($category_name == "") {
        $message = "Category name required";
    } else {

        $allowedTypes = ['jpg', 'jpeg', 'png', 'webp'];
        $fileName = $_FILES['image']['name'];
        $fileTmp  = $_FILES['image']['tmp_name'];
        $fileSize = $_FILES['image']['size'];

        $ext = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));

        if (!in_array($ext, $allowedTypes)) {
            $message = "Only JPG, PNG, WEBP allowed";
        } elseif ($fileSize > 2 * 1024 * 1024) {
            $message = "Image size max 2MB";
        } else {

            $newName = time() . "_" . rand(1000,9999) . "." . $ext;
            $targetPath = $uploadDir . $newName;

            if (move_uploaded_file($fileTmp, $targetPath)) {

                $stmt = $pdo->prepare(
                    "INSERT INTO categories (category_name, image) VALUES (?, ?)"
                );
                $stmt->execute([$category_name, $newName]);

                $message = "Category added successfully";
            } else {
                $message = "Image upload failed";
            }
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Category</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<style>
*{box-sizing:border-box}
body{
  margin:0;
  font-family:'Segoe UI', sans-serif;
  background:#f5f6fa;
}
.container{
  max-width:420px;
  margin:60px auto;
  padding:15px;
}
.card{
  background:#fff;
  padding:25px;
  border-radius:12px;
  box-shadow:0 10px 25px rgba(0,0,0,0.08);
}
h2{
  text-align:center;
  margin-bottom:20px;
}
input,button{
  width:100%;
  padding:12px;
  margin-top:12px;
  border-radius:8px;
  font-size:15px;
}
input{
  border:1px solid #ddd;
}
button{
  background:#d3ae3b;
  border:none;
  font-weight:600;
  cursor:pointer;
}
button:hover{opacity:0.9}
.msg{
  margin-bottom:10px;
  text-align:center;
  font-weight:600;
  color:green;
}
.error{color:red}
</style>
</head>

<body>

<div class="container">
  <div class="card">
    <h2>Add Category</h2>

    <?php if($message): ?>
      <div class="msg <?php echo (strpos($message,'failed')!==false || strpos($message,'Only')!==false || strpos($message,'required')!==false) ? 'error':''; ?>">
        <?= $message ?>
      </div>
    <?php endif; ?>

    <form method="post" enctype="multipart/form-data">
      <input type="text" name="category_name" placeholder="Category Name" required>
      <input type="file" name="image" accept="image/*" required>
      <button name="save_category">Save Category</button>
    </form>
  </div>
</div>

</body>
</html>
