<?php
require_once "common/header.php";
require_once "include/db_handler.php";
require_once "include/paging.php";
$paginate = new Paging();
$db = new DbHandler();

if (isset($_POST['submit_video'])) {

    $videoTitle = $_POST['title'];
    $vUrl = $_POST['video_url'];
    $status = $_POST['status'];

    $videoUrl = ltrim($vUrl, 'https://www.youtube.com/watch?v=');

    if ($videoTitle != '' && $videoUrl != '') {

        $response = $db->addVideo($videoTitle, $videoUrl, $status);

        if ($response["error"] == false) {

            $response["message"] = '<div class="alert alert-success" style="text-align:center;">
                    <a href="published_video.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Video added successfully
                  </div>';
        }

    } else {
        $response["message"] = '<div class="alert alert-danger" style="text-align:center;">
                    <a href="published_video.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Sorry!</strong> All fields are required 
                                
                  </div>';
    }
} else if (isset($_POST['btn_deactivate'])) {

    $db = new DbHandler();
    $response = array();

    $videoID = $_POST['videoID'];
    $status = 'inactive';

    if (!empty($videoID)) {
        $response = $db->updateVideoStatus($videoID, $status);
    } else {
        $response['error'] = true;
        $response['message'] = "An error occurred while deleting";
    }

    $response["message"] = '<div class="alert alert-danger" id="danger-alert" style="text-align:center;">
                    <a href="category.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Video Deactivated
                                
                  </div>';

} else if (isset($_POST['btn_delete'])) {

    $db = new DbHandler();
    $response = array();

    $videoID = $_POST['videoID'];

    if (!empty($videoID)) {
        $response = $db->deleteVideo($videoID);
    } else {
        $response['error'] = true;
        $response['message'] = "An error occurred while deleting";
    }

    $response["message"] = '<div class="alert alert-danger" id="danger-alert" style="text-align:center;">
                    <a href="published_video.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Video deleted
                                
                  </div>';
}
?>

<body>
<div class="container-fluid">
    <div class="row">
        <?php
        require_once "common/sidebar.php";
        ?>
        <div class="right-column">
            <?php
            require_once "common/navbar.php";
            ?>
            <main class="main-content p-5" role="main">
                <div class="row">
                    <div class="col-md-12">
                        <button type="button" class="btn btn-success pull-right mb-3" data-toggle="modal"
                                data-target="#addVideoModal">Add Video
                        </button>
                    </div>
                </div>
                <div class="row mb-5">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-lg-12 pb-5">
                                        <h2>Published Videos</h2>
                                        <?php if (isset($response['message'])) echo $response["message"];
                                        $query = "SELECT * FROM `tbl_videos` WHERE STATUS = 'published' ORDER By id DESC";
                                        $new_query = $paginate->getQuery($query, 10);
                                        $response = $db->getAllVideos($new_query);
                                        ?>
                                        <table class="table">
                                            <thead class="thead-light">
                                            <tr>
                                                <th>Title</th>
                                                <th>Image</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                            </thead>
                                            <tbody>

                                            <?php foreach ($response as $video) : ?>
                                                <tr>
                                                    <td><?php echo $video['title']; ?></td>
                                                    <td>
                                                        <a href="https://www.youtube.com/watch?v=<?php echo $video['video_url']; ?>"
                                                           target="_blank">
                                                            <img src="https://img.youtube.com/vi/<?php echo $video['video_url']; ?>/0.jpg"
                                                                 style="width: 60px!important; height: 60px!important;">
                                                        </a>
                                                    </td>
                                                    <td><?php echo $video['status']; ?></td>
                                                    <td>
                                                        <div class="email-pager-top">
                                                            <div class="email-pager-container">
                                                                <div class="btn-group" role="group"
                                                                     aria-label="Previous or Next Page">

                                                                    <a href="edit_video.php?id=<?php echo $video['id']; ?>">
                                                                        <button type="button"
                                                                                class="btn btn-primary email-list-previous waves-effect waves-light mr-3"
                                                                                data-toggle="tooltip" title=""
                                                                                data-original-title="Edit">
                                                                            <i class="fa fa-pencil"> Edit</i>
                                                                        </button>
                                                                    </a>

                                                                    <div class="row ml-2 mr-3">
                                                                        <form action="published_video.php"
                                                                              method="post">
                                                                            <input type="hidden" name="videoID"
                                                                                   value="<?php echo $video['id']; ?>"/>
                                                                            <input type="submit" class="btn btn-info"
                                                                                   name="btn_deactivate"
                                                                                   value="Deactivate">
                                                                        </form>

                                                                    </div>

                                                                    <form action="published_video.php" method="post">
                                                                        <input type="hidden" name="videoID"
                                                                               value="<?php echo $video['id']; ?>"/>

                                                                        <button type="submit" name="btn_delete"
                                                                                class="btn btn-danger email-list-next waves-effect waves-light"
                                                                                data-toggle="tooltip" title=""
                                                                                data-original-title="Delete">
                                                                            <i class="fa fa-trash"> Delete</i>
                                                                        </button>
                                                                    </form>


                                                                </div>
                                                            </div>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>

                                    <style>
                                        .pagination a {
                                            color: black;
                                            float: left;
                                            padding: 8px 16px;
                                            text-decoration: none;
                                            transition: background-color .3s;
                                            border: 1px solid #ddd;
                                        }
                                    </style>
                                    <div class="row">
                                        <div class="col-md-8"></div>
                                        <div class="col-md-4">
                                            <ul class="pagination">
                                                <li class="button"><?php $records_per_page = 10;
                                                    $paginate->paginglink($query, $records_per_page); ?></li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </main>
        </div>
    </div>
</div>
<?php
require_once "modals/add_article.php";
require_once "modals/add_video.php";
require_once "modals/logout.php";
require_once "modals/add_category.php";
require_once "modals/add_user.php";
require_once "common/footer.php";
?>
