<?php
session_start();

if ($_SESSION["fidx"] == "" || $_SESSION["fidx"] == NULL) {
	header('Location:facultylogin');
}

$userid = $_SESSION["fidx"];
$fname = $_SESSION["fname"];
?>
<?php include('fhead.php');  ?>
<div class="container">
	<div class="row">
		<div class="col-md-12">

			<h3> Welcome Teacher: <a href="welcomefaculty.php"><span style="color:#FF0004"> <?php echo $fname; ?></span></a> </h3>
			<?PHP
			include("database.php");
			if (isset($_POST['submit'])) {
				$title = $_POST['videotitle'];
				$v_url = $_POST['VideoURL'];
				$v_info = $_POST['Videoinfo'];
				$v_file = $_POST['video_file'];
				$done = "
					<center>
					<div class='alert alert-success fade in __web-inspector-hide-shortcut__'' style='margin-top:10px;'>
					<a href='#' class='close' data-dismiss='alert' aria-label='close' title='close'>&times;</a>
					<strong><h3 style='margin-top: 10px;
					margin-bottom: 10px;'> Video added Sucessfully.</h3>
					</strong>
					</div>
					</center>
					";

				$sql = "INSERT INTO `Video` (`V_Title`, `V_Url`, `V_Remarks`, `V_FilePath`) VALUES ('$title','$v_url','$v_info','$v_file)";
				//close the connection
				mysqli_query($connect, $sql);

				echo $done;
			}

			// Handle video deletion
			// Handle video deletion
			// Handle video deletion
			if (isset($_REQUEST['deleteid'])) {
				$deleteid = $_GET['deleteid'];
				$sql = "DELETE FROM `video` WHERE V_id = $deleteid";
				if (mysqli_query($connect, $sql)) {
					echo "<br><br><div class='alert alert-success fade in'>
			<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
			<strong>Success!</strong> Video details deleted.
			</div>";
				} else {
					echo "<br><Strong>Video Details Deletion Failure. Try Again</strong><br> Error Details: " . mysqli_error($connect);
				}
			}

			// Handle video addition and file upload
			if (isset($_POST['submit'])) {
				$title = mysqli_real_escape_string($connect, $_POST['title']);
				$remarks = mysqli_real_escape_string($connect, $_POST['remarks']);

				// Handle the file upload
				if (isset($_FILES['video_file']) && $_FILES['video_file']['error'] == 0) {
					$file_name = $_FILES['video_file']['name'];
					$file_tmp = $_FILES['video_file']['tmp_name'];
					$file_size = $_FILES['video_file']['size'];
					$file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));

					// Define allowed video formats
					$allowed_extensions = ['mp4', 'avi', 'mov', 'wmv', 'flv'];

					// Check if the uploaded file is a valid video
					if (in_array($file_ext, $allowed_extensions)) {
						// Set the upload directory (make sure this directory is writable)
						$upload_dir = 'uploads/';
						$upload_file = $upload_dir . basename($file_name);

						// Move the uploaded file to the server's directory
						if (move_uploaded_file($file_tmp, $upload_file)) {
							// Insert video details into the database
							$insert_sql = "INSERT INTO video (V_Title, V_Url, V_Remarks, V_FilePath) 
						   VALUES ('$title', '$file_name', '$remarks', '$upload_file')";
							if (mysqli_query($connect, $insert_sql)) {
								echo "<br><br><div class='alert alert-success fade in'>
					<a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>
					<strong>Success!</strong> New video added successfully.
					</div>";
							} else {
								echo "<br><Strong>Error: Could not add the video.</strong><br> Error Details: " . mysqli_error($connect);
							}
						} else {
							echo "<br><strong>Error: File upload failed!</strong>";
						}
					} else {
						echo "<br><strong>Error: Invalid video file format. Only MP4, AVI, MOV, WMV, FLV are allowed.</strong>";
					}
				} else {
					echo "<br><strong>Error: No file selected or there was an error with the upload.</strong>";
				}
			}


			// Close database connection
			mysqli_close($connect);








			?>

			<fieldset>
				<legend>Add Videos</legend>
				<form action="" method="POST" name="AddAssessment" enctype="multipart/form-data">
					<table class=" table table-hover">

						<tr>
							<td><strong>Video Title </strong>
							</td>
							<td>
								<input type="text" class="form-control" name="videotitle">
							</td>

						</tr>
						<tr>
							<td><strong>Video URL</strong> </td>
							<td>
								<textarea name="VideoURL" class="form-control" rows="1" cols="150"></textarea>
							</td>
						</tr>
						<tr>
							<td><strong>Video Description</strong> </td>
							<td>
								<textarea name="Videoinfo" class="form-control" rows="5" cols="150"></textarea>
							</td>
						</tr>
						<tr>
							<div class="form-group">
								<label for="video_file">Upload Video:</label>
								<input type="file" class="form-control" id="video_file" name="video_file" required>
							</div>
						</tr>

						<td><button type="submit" name="submit" class="btn btn-success" style="border-radius:0%">Add Video</button>
						</td>

					</table>
				</form>

			</fieldset>
		</div>
	</div>
	<?php include('allfoot.php');  ?>