<?php
include('config.php');
if($_SERVER["REQUEST_METHOD"] == "POST")
{   
    $ID = $_POST["ID"];
    $code = $_POST["code"];
    $fileCount = count($_FILES['file']['name']);

    // Get the current time adn date
    date_default_timezone_set('Asia/Manila');
    $date = date("m-d-Y");
    $time = date("h:i A");

    for($i=0;$i<$fileCount;$i++){
        $fileName = $_FILES['file']['name'][$i];
        $sql = "UPDATE tasks SET `File_Link`='$fileName', `File_Status`='pending', `Date_Submitted`='$date', `Time`='$time', NOtif_Status='unread' WHERE `Code`='$code'";
        if (mysqli_query($db, $sql)){
            echo '<script>alert("Submitted successfully!");</script>';
            echo '<script>document.location = "view_assignment.php?ID='.$ID.'"</script>';
        }else{
            echo mysqli_error($db);
        }
        move_uploaded_file($_FILES['file']['tmp_name'][$i], '../submitted_files/'.$fileName);
    }
}
?>