<?php
// File: competition_submit.php
require_once 'db.php';

if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    header("Location: competition.php");
    exit();
}

$name = trim($_POST['name']);
$email = trim($_POST['email']);
$phone = trim($_POST['phone']);
$message = trim($_POST['message']);
$competition_id = intval($_POST['competition_id']);
$artwork_path = null;

// Handle image upload
if (!empty($_FILES['artwork']['name'])) {
    $upload_dir = __DIR__ . "/uploads/competitions/";
    if (!is_dir($upload_dir)) mkdir($upload_dir, 0755, true);

    $filename = time() . '_' . basename($_FILES['artwork']['name']);
    $target_path = $upload_dir . $filename;

    if (move_uploaded_file($_FILES['artwork']['tmp_name'], $target_path)) {
        $artwork_path = "/uploads/competitions/" . $filename;
    }
}

// Save entry
$stmt = $conn->prepare("INSERT INTO competition_entries (competition_id, name, email, phone, artwork_path, message) VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("isssss", $competition_id, $name, $email, $phone, $artwork_path, $message);
$stmt->execute();
$stmt->close();

// Redirect with thank-you message
header("Location: competition_success.php");
exit();
?>
