<?php

include("connection.php");

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Capture form data
    $title = $_POST["title"];
    $platform = $_POST["platfrom"];
    $url = $_POST["url"];
    $description = $_POST["discription"];
    $currentDateTime = date('Y-m-d H:i:s');
    $group = $_POST["group"];

    // File Uploads
    $videoFile = $_FILES["video_file"];
    $imageFile = $_FILES["image_file"];
    $audioFile = $_FILES["audio_file"];
    $pdfFile = $_FILES["pdf_file"];

    // File Paths for Uploads
    $videoPath = "uploads/" . $videoFile["name"];
    $imagePath = "uploads/" . $imageFile["name"];
    $audioPath = "uploads/" . $audioFile["name"];
    $pdfPath = "uploads/" . $pdfFile["name"];

    // Move uploaded files to a specific directory
    move_uploaded_file($videoFile["tmp_name"], $videoPath);
    move_uploaded_file($imageFile["tmp_name"], $imagePath);
    move_uploaded_file($audioFile["tmp_name"], $audioPath);
    move_uploaded_file($pdfFile["tmp_name"], $pdfPath);

    // SQL query to insert data into the database
    $sql = "INSERT INTO group_add_task(title, platform, url, task_discription, video, image, audio, pdf,group_id,datetime)
            VALUES ('$title', '$platform', '$url', '$description',  '$videoPath', '$imagePath', '$audioPath', '$pdfPath','$group','$currentDateTime')";

    // Execute the query
    if (mysqli_query($conn, $sql)) {

        // echo "Task created successfully!";
        header("Location: add-task.php?success=Task created successfully!!!!");
        exit(); // Exit to prevent further script execution


    } else {
        echo "Error: " . mysqli_error($conn);
    }

    // Close the database connection
    mysqli_close($conn);
}
?>