<?php 
include ('header.php');
?>



<div class="container" style="margin-top:40px;">
    
    
<?php
// Check connection
if ($connect->connect_error) {
    die("Connection failed: " . $connect->connect_error);
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Sanitize inputs
    $category = htmlspecialchars($_POST['category']);
    $name = htmlspecialchars($_POST['name']);
    $description = htmlspecialchars($_POST['description']);

    // Handle image upload with validation
    $image = "";
    if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
        $allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
        $maxSize = 5 * 1024 * 1024; // 5 MB

        $fileType = $_FILES['image']['type'];
        $fileSize = $_FILES['image']['size'];

        if (!in_array($fileType, $allowedTypes)) {
            die('Error: Invalid file type.');
        }

        if ($fileSize > $maxSize) {
            die('Error: File size exceeds the limit.');
        }

        $image = 'uploads/' . basename($_FILES['image']['name']);
        move_uploaded_file($_FILES['image']['tmp_name'], $image);
    }

    // Prepared Statement for database insertion
    $stmt = $connect->prepare("INSERT INTO news (category, image, name, description) VALUES (?, ?, ?, ?)");
    $stmt->bind_param("ssss", $category, $image, $name, $description);

    if ($stmt->execute()) {
         echo "<script type='text/javascript'>
                alert('New news article added successfully!');
                window.location.href = window.location.href; // Reload the page to clear form
              </script>";
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
}

$connect->close();
?>


<div class="container mt-5">
    <h2>Add News Article</h2>
    <form  method="POST" enctype="multipart/form-data">
  <div class="mb-3">
    <label for="category" class="form-label">Category</label>
    <select class="form-control" id="category" name="category" required>
        <option value="" disabled selected>Select Category</option>
        <option value="Politics">Politics</option>
        <option value="Sports">Sports</option>
        <option value="Technology">Technology</option>
        <option value="Health">Health</option>
        <option value="Entertainment">Entertainment</option>
        <option value="Business">Business</option>
        <option value="Education">Education</option>
        <option value="Science">Science</option>
        <option value="Environment">Environment</option>
        <option value="Lifestyle">Lifestyle</option>
        <option value="Travel">Travel</option>
        <option value="Food">Food</option>
        <option value="Culture">Culture</option>
        <option value="Art">Art</option>
        <option value="Music">Music</option>
        <option value="Religion">Religion</option>
        <option value="Social Issues">Social Issues</option>
        <!-- Add more categories as needed -->
    </select>
</div>

        <div class="mb-3">
            <label for="name" class="form-label">News Name</label>
            <input type="text" class="form-control" id="name" name="name" required>
        </div>
        <div class="mb-3">
            <label for="description" class="form-label">Description</label>
            <textarea class="form-control" id="description" name="description" rows="4" required></textarea>
        </div>
        <div class="mb-3">
            <label for="image" class="form-label">Upload Image</label>
            <input type="file" class="form-control" id="image" name="image" accept="image/*">
        </div>
        <button type="submit" class="btn btn-primary">Submit News</button>
    </form>
</div>
</div>