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

    // display all the data from database to input boxes
	if (isset($_GET["ID"])){
        $ID = $_GET["ID"];
        $record = mysqli_query($db, "SELECT * FROM file_request WHERE ID = '$ID'");

        if (count(array($record)) == 1 ) {
            $data = mysqli_fetch_array($record);
                $lname = $data['Last_Name'];
                $fname = $data['First_Name'];
                $m_name = $data['Middle_Name'];
                $email = $data['Email'];
                $contact_num = $data['Contact_Number'];
				$document_type = $data['Document_Type'];
				$narration = $data['Description'];
				$status = $data['Status'];
				$my_file = $data['File_Upload'];

				if($data['Status'] == "Pending"){
					$color = "orange";
				}
				else{
					$color = "gray";
				}

        }
        $error = "";
    }
    if($_SERVER["REQUEST_METHOD"] == "POST") {
		if (isset($_FILES["fileToUpload"])) {
			$targetDir = "files/"; // Directory where uploaded files will be stored
			$targetFile = $targetDir . basename($_FILES["fileToUpload"]["name"]);
			$uploadOk = 1;
			$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
			$fileName = $_FILES["fileToUpload"]["name"];

			if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $targetFile)) {
				echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
			} else {
				echo "Sorry, there was an error uploading your file.";
			}
		}
		else{
			$fileName = $_POST['fileToUpload'];
		}
		
        $status = mysqli_real_escape_string($db,$_POST['status']);
		$sql = "UPDATE file_request Set `Status` = '$status', `File_Upload` = '$fileName' WHERE ID='$ID'"; 

		$result = mysqli_query($db,$sql);
		// If result matched atleast one, table row must be 1 row
		
			if($result) {
				echo "<script>alert('Status updated successfully!');</script>";
				echo "<script>window.location.href = 'file_request_main-page.php';</script>";

				// header("Location: complaints_main-page.php");
			}else {
			$error = "Failed to update!";
			}
	} 


?>

<!DOCTYPE html>
<html lang="en">
<head>
	<title>File Request</title>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->	
	<link rel="icon" type="image/png" href="images/icons/minsu-icon.ico"/>
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animate/animate.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css">
<!--===============================================================================================-->	
	<link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
	<link rel="stylesheet" type="text/css" href="util.css">
	<link rel="stylesheet" type="text/css" href="main.css">
<!--===============================================================================================-->
</head>
<style>
	.modal3 {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 10000000; /* Sit on top */
  padding-top: 100px; /* Location of the box */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
.modal-content3 {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}
.close3 {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close3:hover,
.close3:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
</style>
<body>
	
	<div class="limiter" style="width: 100%;">
		<div class="container-login100" style="width: 100%;">
			<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54" style="width: 100%;">
			<a id="com"><img src="images/private_comment.png" width="38px" height= "38px" style="border: none; border-radius: 5px; float: right; margin-top: -40px; cursor: pointer; border-radius: 5px;" title="Private comments"></a>
                <center><h4 style="font-family: Times New Roman; font-weight: bold; margin-top: -15px;">REQUEST SUMMARY</h4><br></center>
				<form class="login100-form validate-form" action="" method="post" style="width: 100%;" enctype="multipart/form-data">
					<hr>
					<?php
						if($status == "Pending"){
							echo '<center><img src="images/pending_watermark.png" alt="" style="width: 550px; height: 150px; opacity: 50%;"></center>';
						}
						else{
							echo '<center><img src="images/completed_watermark.png" alt="" style="width: 520px; height: 270px; opacity: 50%; transform: rotate(11deg); margin-top: -30px; margin-bottom: -40px;"></center>';
						}
					?>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp REQUEST ID # : <p style="font-family: Times New Roman;  color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;"><?php echo $ID;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp REQUESTER (Full Name) : <p style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;"><?php echo $lname; echo ", "; echo $fname; echo " "; echo $m_name;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp EMAIL : <p style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;"><?php echo $email;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp CONTACT NUMBER : <p style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;"><?php echo $contact_num;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp DOCUMENT TYPE : <p style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;"><?php echo $document_type;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp UPLOADED FILE : </p>
					<?php
					if($my_file == "")
					{
					?>
						<input type="file" name="fileToUpload" style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px;">
					<?php
					}
					else{
					?>
					<input type="text" name="fileToUpload" value="<?php echo $my_file; ?>" style="font-family: Times New Roman; color: white; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px; background-color: #08A045; padding: 7px; border-radius: 5px; width: 60%" readonly>
					<?php
					}
					?>	
					<br><br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp DESCRIPTION : <p style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; text-align: justify; margin-left: 35px; margin-top: 10px;"><?php echo $narration;?></p></p>
					<br>
					<p style="font-family: Times New Roman; color: black; font-weight: bold; font-size: 16px;">&nbsp REQUEST STATUS :
					<br> 
					<select name="status" style="font-family: Times New Roman; color: black; font-size: 17px; font-weight: ; margin-left: 35px; margin-top: 10px; padding: 5px; padding-left: 20px; padding-right: 20px; border-radius: 5px; width: 100%; background-color: #f6f6f6; color: ;">
						
					<?php
						if($status == "Pending")
						{
							echo '<option value="Pending" selected>Pending</option>';
							echo '<option value="Completed">Completed</option>';
						}
						else{
							echo '<option value="Pending">Pending</option>';
							echo '<option value="Completed" selected>Completed</option>';
						}
					?>
					</select>
					</p>

					<div class="container-login100-form-btn m-t-20" style="color:red;">
						<?php echo $error;?> <hr>
					</div>
					
					<button class="login100-form-btn" style="background-color: #24a0ed; width: 150px; border-radius: 5px; height: 40px; float:right">Update</button>
				</form>
				<a href="file_request_main-page.php" style="color: white;"><button style="background-color: gray; width: 150px; margin-right: 5px; border-radius: 5px; height: 40px; color: white; float:right;">CANCEL</button></a>
			</div>
		</div>
	</div>
	

	<div id="dropDownSelect1"></div>

	<!-- ################## PRIVATE COMMENTS ###################### -->

	<!-- The Modal 3 -->
	<div id="myModal3" class="modal3">

	<!-- Modal content 3 -->
	<div class="modal-content3" style="border-radius: 20px; height: 450px; width: 60%;">
	<span class="close3">&times;</span>

	<?php
	// Count the private comments
	require_once "config.php";
	$sql = "SELECT * FROM file_comment WHERE Code='$ID'";
	if ($result=mysqli_query($db,$sql)) {
		$rowcount=mysqli_num_rows($result);
		echo '<h4 style="font-family: Arial; color: #3a3a3a;">' . $rowcount . ' private comments</h4>';
	}
	echo '<br><div style="overflow-y: auto; height: 220px;">';
	$sql2 = "SELECT * FROM file_comment WHERE Code='$ID' ORDER BY ID ASC";
	if($result2 = $db->query($sql2)){
	if($result2->num_rows > 0){
		while($row = $result2->fetch_array()){
		echo "<img src='profile_pic/" . $row['Profile_Picture'] . "'" .  " style='height: 40px; width: 40px; border-radius: 50%; float:left; padding-top: -10px;'></img>";
		echo '<p style="font-weight:bold; font-family: Arial; font-size:15px; color: #3a3a3a; margin-top: 10px; margin-left: 10px; float: left;">'.$row["Username"]. "</p><br><br>
		<p style='font-family: Arial; font-size:12px; color: #373737;'>" .$row['Date']. " " .$row['Time']. '</p>';
		echo '<p style="font-family: Arial; font-size:15px; color: #3a3a3a;">'.$row["Private_Comments"].'<br><hr></p>';
		}
	}
	else{
		echo '<div style="margin-top: 70px; margin-left: 100px; font-size: 18px;">No comments</div>';
	}
	}
	echo '</div>';
	// Close connection
	$db->close();
	?>
	<br>
	<form name="myForm" method="post" action="file_request_send_comments.php">
	<input type="hidden" name="code"  value="<?php echo $ID; ?>">
	<input type="hidden" name="profile_pic"  value="<?php echo "brgy_logo.png"; ?>">
	<input type="hidden" name="username"  value="<?php echo "Administrator"; ?>">
	<textarea type="text-area" name="comments" style="width:100%; height: 70px;  font-family: Arial; font-size: 14; color: #3a3a3a; padding:7px; border-radius: 5px; border-color: black; background-color: #f5f5f5;" placeholder="Enter your comments here..." required></textarea>
	<br><a onclick="document.getElementById('myModal3').style.display='none'" ><input type="submit" value="Send" style="cursor: pointer; float:right; min-width: 70px; padding: 7.2px; color:white; margin-top:5px; text-align: center; background-color: #4c9be1; border-radius: 5px; font-family: Arial; border-color: #4c9be1; font-size: 17px"></a>
	</form>

	</div>
	</div>

	<!--<script> 
	function add(bd){ 
    //calculate month difference from current date in time  
    var month_diff = Date.now() - dob.getTime();  
    //convert the calculated difference in date format  
    var age_dt = new Date(month_diff);   
    //extract year from date      
    var year = age_dt.getUTCFullYear();
	var my_year = dob.getUTCFullYear();  
    //now calculate the age of the user  
    var age = Math.abs(year - my_year);
	
	if(age < 18)
	{
		alert("Invalid birthdate!");
	}
	} 

</script> -->  

<script>
    // Get the modal
    var modal3 = document.getElementById("myModal3");

    // Get the button that opens the modal
    var btn = document.getElementById("com");

    // Get the <span> element that closes the modal
    var span = document.getElementsByClassName("close3")[0];

    // When the user clicks the button, open the modal 
    btn.onclick = function() {
    modal3.style.display = "block";
    }

    // When the user clicks on <span> (x), close the modal
    span.onclick = function() {
      modal3.style.display = "none";
    }

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
    if (event.target == modal3) {
      modal3.style.display = "none";
    }
    }
    </script>

<script>
  document.getElementById("myForm").addEventListener("submit", function() {
    // Clear the input field after form submission
    document.getElementById("myForm").reset();
  });
</script>

<!--===============================================================================================-->
	<script src="vendor/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/bootstrap/js/popper.js"></script>
	<script src="vendor/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/select2/select2.min.js"></script>
<!--===============================================================================================-->
	<script src="vendor/daterangepicker/moment.min.js"></script>
	<script src="vendor/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
	<script src="vendor/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
	<script src="js/main.js"></script>

</body>
</html>