<?php
declare(strict_types=1);

require_once '../includes/auth.php';
require_once '../includes/security.php';
require_once '../includes/functions.php';
require_once '../includes/csrf.php';
require_once '../config/database.php';

$pageTitle = 'Add Entry';

require_once '../includes/header.php';

$stmt = $pdo->query("
SELECT
    id,
    university_name,
    video_url
FROM universities
ORDER BY university_name ASC
");

$universities = $stmt->fetchAll();
?>

<section class="content-header">

<div class="container-fluid">

<h1>Add New Entry</h1>

</div>

</section>

<section class="content">

<div class="container-fluid">

<?php if(isset($_SESSION['success'])): ?>

<div class="alert alert-success alert-dismissible fade show">

<?= e($_SESSION['success']); ?>

<button class="close" data-dismiss="alert">
<span>&times;</span>
</button>

</div>

<?php unset($_SESSION['success']); endif; ?>


<?php if(isset($_SESSION['error'])): ?>

<div class="alert alert-danger alert-dismissible fade show">

<?= e($_SESSION['error']); ?>

<button class="close" data-dismiss="alert">
<span>&times;</span>
</button>

</div>

<?php unset($_SESSION['error']); endif; ?>


<div class="card card-primary">

<div class="card-header">

<h3 class="card-title">

Upload PDF

</h3>

</div>

<form
action="../actions/entry.php"
method="POST"
enctype="multipart/form-data">

<input
type="hidden"
name="csrf_token"
value="<?= e(csrf_token()); ?>">

<div class="card-body">

<div class="form-group">

<label>

University

</label>

<select
class="form-control"
name="university_id"
id="university"
required>

<option value="">Select University</option>

<?php foreach($universities as $u): ?>

<option
value="<?= (int)$u['id']; ?>"
data-video="<?= e($u['video_url']); ?>">

<?= e($u['university_name']); ?>

</option>

<?php endforeach; ?>

</select>

</div>


<div class="form-group">

<label>

Upload PDF

</label>

<input
type="file"
name="pdf"
class="form-control"
accept=".pdf"
required>

<small class="text-muted">

Only PDF files allowed (Max 2 MB)

</small>

</div>


<div
id="videoBox"
style="display:none;"
class="alert alert-info">

<strong>University Video:</strong>

<br><br>

<a
id="videoLink"
href="#"
target="_blank"
class="btn btn-success">

Open Video

</a>

</div>

</div>

<div class="card-footer">

<button
class="btn btn-primary">

<i class="fas fa-upload"></i>

Upload PDF

</button>

</div>

</form>

</div>

</div>

</section>

<script>

document
.getElementById("university")
.addEventListener("change",function(){

let video=this.options[this.selectedIndex].dataset.video;

let box=document.getElementById("videoBox");

let link=document.getElementById("videoLink");

if(video){

box.style.display="block";

link.href=video;

}else{

box.style.display="none";

}

});

</script>

<?php require_once '../includes/footer.php'; ?>