<?php
include('config.php');
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{   
    $title = $_POST["title"];
    $instructions = $_POST["instructions"];
    $due_date = $_POST['due_date'];
    $converted_due_date = date("m-d-Y", strtotime($due_date));
    $due_time = $_POST["due_time"];
    $converted_due_time = date("h:i A", strtotime($due_time));
    $fileCount = count($_FILES['file']['name']);
    $dept = $_POST["dept"];

    if($converted_due_date == "01-01-1970" || $converted_due_date == "" || $converted_due_time == "12:00 AM" || $converted_due_time == ""){
        $converted_due_date = "None";
        $converted_due_time = "None";
    }
    if($instructions == ""){
        $instructions = "None";
    }

    // Get the current time adn date
    date_default_timezone_set('Asia/Manila');
    $date = date("m-d-Y");
    $time = date("h:i A");
    $year_now = date("Y");
    
    for($i=0;$i<$fileCount;$i++){
        $fileName = $_FILES['file']['name'][$i];
        if($fileName == ""){
            $fileName = "None";
        }
        $sql = "INSERT INTO requirements (`Title`, `Instructions`, `Due_Date`, `Due_Time`, `Attached_File`, `Date_Posted`, `Time_Posted`, `Department`, `Year`) VALUES ('$title', '$instructions', '$converted_due_date', '$converted_due_time', '$fileName', '$date', '$time', '$dept', '$year_now')";
        if (mysqli_query($db, $sql)){
            // $instructor_name = $_SESSION['my_full_name'];
            $sql_mail = "SELECT * FROM members WHERE Usertype!='admin'";
            if($result_mail = $db->query($sql_mail)){
                if($result_mail->num_rows > 0){
                    while($row_mail = $result_mail->fetch_array()){
                        $member_mail = $row_mail['Email'];
                        $lname = $row_mail['Last_Name'];
                		$fname = $row_mail['First_Name'];
                		$middle_in = $row_mail['Middle_Initial'];
                        
                        // ==========================EMAIL VERIFICATION====================================
					    // $to = $member_mail;
					    // $from = "pupsanjuan@gmail.com";
                        // $subject = "Polytechnic University of the Philippines  - San Juan Campus";
                        // $full_name = "".$fname. " " .$middle_in. " " .$lname."";
                        
                        // $message = 'Good Day! '.$full_name.',<br>';
                        // $message .= "Admin posted a new requirements in PUPSJ Website entitled '.$title.'.  Kindly check the list of your requirements. Thank you.<br><br>";
                        // $message .= "Website link:<br>";
                        // $message .= "localhost/QAC-DMS/login-admin.php<br>";
                        
                        
                        // Always set content-type when sending HTML email
                        // $headers = "";
                        // $headers .= 'From: ' . $from . "\r\n";
                        // $headers .= "MIME-Version: 1.0" . "\r\n";
                        // $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
                        
                        // mail($to,$subject,$message,$headers);
                        
                        // ============================END=================================
                    }
                }
            }
            
            echo '<script>alert("Assigned successfully!");</script>';
            echo '<script>document.location = "assignments.php"</script>';
        }
        else{
            echo mysqli_error($db);
        }
        move_uploaded_file($_FILES['file']['tmp_name'][$i], 'attached_files/'.$fileName);
    }
    
}
?>