<?php
include("config.php");
	if($_SERVER["REQUEST_METHOD"] == "POST") {
		
		$ID = mysqli_real_escape_string($db,$_POST['ID']);
		$code = mysqli_real_escape_string($db,$_POST['code']);
		$profile_pic = mysqli_real_escape_string($db,$_POST['profile_pic']);
		$username = mysqli_real_escape_string($db,$_POST['username']);
		$comments = mysqli_real_escape_string($db,$_POST['comments']);

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

		$status = mysqli_real_escape_string($db,$_POST['status']);
		$sql = "UPDATE tasks Set `File_Status` = '$status' WHERE ID='$ID'"; 
				
		$result = mysqli_query($db,$sql);
			// If result matched atleast one, table row must be 1 row			
			if($result) {
					if($comments != "") {
					$sql2 = "INSERT INTO private_comment (`Code`, `Profile_Picture`, `Username`, `Private_Comments`, `Time`, `Date`)
					VALUES ('$code', '$profile_pic', '$username', '$comments', '$time', '$date')";
				
						$result2 = mysqli_query($db,$sql2);
						// If result matched atleast one, table row must be 1 row			
						if($result2) {
							echo '<script>alert("Document was returned successfully!");</script>';
							echo '<script>document.location = "task_main-page.php"</script>';
							exit();
						}else {
							echo '<script>alert("Failed to submit comments!");</script>';
							echo '<script>document.location = "task_main-page.php"</script>';
						}
					}
				echo '<script>alert("Document was returned successfully!");</script>';
				echo '<script>document.location = "task_main-page.php"</script>';
				exit();
			}
			else {
                echo '<script>alert("Failed to return!");</script>';
				echo '<script>document.location = "task_main-page.php"</script>';
			}
		}    
?>