<?php
require 'inc/auth_check.php';
require 'inc/header.php';
$msg = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $title = $_POST['title'] ?? '';
    $desc = $_POST['description'] ?? '';
    $imgname = '';

    if (!empty($_FILES['image']['name'])) {
        $f = $_FILES['image'];
        $allowed = ['image/png','image/jpeg','image/jpg','image/webp'];
        if ($f['size'] > 3*1024*1024) { $msg = 'Gambar max 3MB'; }
        elseif (!in_array(mime_content_type($f['tmp_name']), $allowed)) { $msg = 'Tipe file tidak diizinkan'; }
        else {
            $ext = pathinfo($f['name'], PATHINFO_EXTENSION);
            $imgname = uniqid() . '.' . $ext;
            move_uploaded_file($f['tmp_name'], __DIR__ . '/../uploads/projects/' . $imgname);
        }
    }

    if ($msg === '') {
        $stmt = $mysqli->prepare("INSERT INTO projects (title, description, image) VALUES (?, ?, ?)");
        $stmt->bind_param('sss', $title, $desc, $imgname);
        $stmt->execute();
        header('Location: projects.php');
        exit;
    }
}
?>
<h2 class="text-xl font-bold mb-4">Tambah Project</h2>
<?php if($msg): ?><div class="text-red-600 mb-3"><?php echo e($msg); ?></div><?php endif; ?>
<form method="post" enctype="multipart/form-data" class="bg-white p-4 shadow rounded">
  <label>Title</label>
  <input name="title" class="w-full p-2 border mb-3" required>
  <label>Description</label>
  <textarea name="description" class="w-full p-2 border mb-3"></textarea>
  <label>Image (jpg/png/webp)</label>
  <input type="file" name="image" class="mb-3">
  <button class="bg-teal-600 text-white py-2 px-4 rounded">Simpan</button>
</form>
</div></body></html>
