<?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';

$pageTitle = 'Universities';

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

$universities = getUniversities();
?>

<section class="content-header">
    <div class="container-fluid">
        <div class="d-flex justify-content-between align-items-center">
            <h1>Universities</h1>
        </div>
    </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 type="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 type="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">

            Add University

        </h3>

    </div>

    <form action="../actions/university.php" method="POST">

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

        <input
            type="hidden"
            name="action"
            value="add">

        <div class="card-body">

            <div class="row">

                <div class="col-lg-5 col-md-6 col-12">

                    <div class="form-group">

                        <label>University Name</label>

                        <input
                            type="text"
                            name="university_name"
                            class="form-control"
                            placeholder="University Name"
                            required>

                    </div>

                </div>

                <div class="col-md-5">

                    <div class="form-group">

                        <label>Video URL</label>

                        <input
                            type="url"
                            name="video_url"
                            class="form-control"
                            placeholder="https://youtube.com/...">

                    </div>

                </div>

                <div class="col-lg-2 col-md-12 col-12">

                    <div class="form-group">

                        <label>&nbsp;</label>

                        <button
                            type="submit"
                            class="btn btn-primary btn-block">

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

                            Save

                        </button>

                    </div>

                </div>

            </div>

        </div>

    </form>

</div>



<div class="card">

    <div class="card-header">

        <h3 class="card-title">

            University List

        </h3>

    </div>

    <div class="card-body table-responsive">

        <table class="table table-bordered table-hover">

            <thead>

            <tr>

                <th width="60">ID</th>

                <th>University</th>

                <th width="150">Video</th>

                <th width="220">Action</th>

            </tr>

            </thead>

            <tbody>

            <?php if(empty($universities)): ?>

            <tr>

                <td colspan="4" class="text-center">

                    No University Found

                </td>

            </tr>

            <?php endif; ?>

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

            <tr>

                <td><?= (int)$row['id']; ?></td>

                <td><?= e($row['university_name']); ?></td>

                <td>

                    <?php if(!empty($row['video_url'])): ?>

                        <a
                            href="<?= e($row['video_url']); ?>"
                            target="_blank"
                            class="btn btn-success btn-sm">

                            Video

                        </a>

                    <?php else: ?>

                        <span class="text-muted">

                            No Video

                        </span>

                    <?php endif; ?>

                </td>

                <td>

                    <button
type="button"
class="btn btn-warning btn-sm editUniversity"

data-id="<?= (int)$row['id']; ?>"

data-name="<?= e($row['university_name']); ?>"

data-video="<?= e($row['video_url']); ?>">

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

Edit

</button>

                    <form
                        action="../actions/university.php"
                        method="POST"
                        style="display:inline;">

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

                        <input
                            type="hidden"
                            name="action"
                            value="delete">

                        <input
                            type="hidden"
                            name="id"
                            value="<?= (int)$row['id']; ?>">

                        <button
                            type="submit"
                            class="btn btn-danger btn-sm"
                            onclick="return confirm('Delete this university?');">

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

                            Delete

                        </button>

                    </form>

                </td>

            </tr>

            <?php endforeach; ?>

            </tbody>

        </table>

    </div>

</div>

</div>

</section>
<div class="modal fade" id="editUniversityModal">

<div class="modal-dialog">

<div class="modal-content">

<form
method="POST"
action="../actions/university.php">

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

<input
type="hidden"
name="action"
value="edit">

<input
type="hidden"
name="id"
id="edit_id">

<div class="modal-header">

<h5>

Edit University

</h5>

<button
type="button"
class="close"
data-dismiss="modal">

×

</button>

</div>

<div class="modal-body">

<div class="form-group">

<label>

University Name

</label>

<input
type="text"
class="form-control"
name="university_name"
id="edit_name"
required>

</div>

<div class="form-group">

<label>

Video URL

</label>

<input
type="url"
class="form-control"
name="video_url"
id="edit_video">

</div>

</div>

<div class="modal-footer">

<button
class="btn btn-success">

Update

</button>

</div>

</form>

</div>

</div>

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

$(function(){

    $('.editUniversity').click(function(){

        <script>

$(function(){

    $('.editUniversity').click(function(){

        $('#edit_id').val($(this).data('id'));
        $('#edit_name').val($(this).data('name'));
        $('#edit_video').val($(this).data('video'));

        $('#editUniversityModal').modal('show');

    });

});

</script>
<script>
$(document).ready(function () {

    $('.editUniversity').on('click', function () {

        $('#edit_id').val($(this).data('id'));
        $('#edit_name').val($(this).data('name'));
        $('#edit_video').val($(this).data('video'));

        $('#editUniversityModal').modal('show');

    });

});
</script>
