<?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_image'])) {

    $imageTitle = $_POST['title'];
    $status = $_POST['status'];

    $postPhoto1 = $_FILES['post_photo_1']['name'];
    move_uploaded_file($_FILES['post_photo_1']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_1']['name']);

    $postPhoto2 = $_FILES['post_photo_2']['name'];
    move_uploaded_file($_FILES['post_photo_2']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_2']['name']);

    $postPhoto3 = $_FILES['post_photo_3']['name'];
    move_uploaded_file($_FILES['post_photo_3']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_3']['name']);

    $postPhoto4 = $_FILES['post_photo_4']['name'];
    move_uploaded_file($_FILES['post_photo_4']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_4']['name']);

    $postPhoto5 = $_FILES['post_photo_5']['name'];
    move_uploaded_file($_FILES['post_photo_5']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_5']['name']);

    $postPhoto6 = $_FILES['post_photo_6']['name'];
    move_uploaded_file($_FILES['post_photo_6']['tmp_name'], 'front_assets/img/gallery/' . $_FILES['post_photo_6']['name']);


    if ($imageTitle != '') {

        $response = $db->addGallery($imageTitle, $postPhoto1,  $postPhoto2, $postPhoto3, $postPhoto4, $postPhoto5, $postPhoto6, $status);

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

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

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

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

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

    $galleryID = $_POST['galleryID'];
    $status = 'published';

    if (!empty($galleryID)) {
        $response = $db->updateGalleryStatus($galleryID, $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="unpublished_gallery.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Images Deactivated
                                
                  </div>';

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

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

    $galleryID = $_POST['galleryID'];

    if (!empty($galleryID)) {
        $response = $db->deleteGallery($galleryID);
    } 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="unpublished_gallery.php" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                    <strong>Success!</strong> Images 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="#addImagesModal">Add Images
                        </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 Images</h2>
                                        <?php if (isset($response['message'])) echo $response["message"];
                                        $query = "SELECT * FROM `tbl_images` WHERE status = 'inactive' ORDER By id DESC";
                                        $new_query = $paginate->getQuery($query, 10);
                                        $response = $db->getAllGalleries($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 $gallery) : ?>
                                                <tr>
                                                    <td><?php echo $gallery['title']; ?></td>
                                                    <td>
                                                        <div id="demo" class="carousel slide" data-ride="carousel">

                                                            <style>
                                                                #demo{
                                                                    width: 120px!important;
                                                                    height: 80px !important;
                                                                }
                                                                .carousel-item img{
                                                                    width: 120px!important;
                                                                    height: 80px !important;
                                                                }
                                                            </style>
                                                            <!-- Indicators -->
                                                            <ul class="carousel-indicators">
                                                                <li data-target="#demo" data-slide-to="0" class="active"></li>
                                                                <li data-target="#demo" data-slide-to="1"></li>
                                                                <li data-target="#demo" data-slide-to="2"></li>
                                                                <li data-target="#demo" data-slide-to="3"></li>
                                                                <li data-target="#demo" data-slide-to="4"></li>
                                                                <li data-target="#demo" data-slide-to="5"></li>
                                                                <li data-target="#demo" data-slide-to="6"></li>
                                                            </ul>

                                                            <!-- The slideshow -->
                                                            <div class="carousel-inner">
                                                                <div class="carousel-item active">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image1']; ?>" alt="Los Angeles">
                                                                </div>
                                                                <div class="carousel-item">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image2']; ?>" alt="Chicago">
                                                                </div>
                                                                <div class="carousel-item">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image3']; ?>" alt="Chicago">
                                                                </div>
                                                                <div class="carousel-item">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image4']; ?>" alt="Chicago">
                                                                </div>
                                                                <div class="carousel-item">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image5']; ?>" alt="Chicago">
                                                                </div>
                                                                <div class="carousel-item">
                                                                    <img src="assets/img/gallery/<?php echo $gallery['image6']; ?>" alt="Chicago">
                                                                </div>
                                                            </div>


                                                        </div>
                                                    </td>
                                                    <td><?php echo $gallery['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">

                                                                    <div class="row ml-2 mr-3">
                                                                        <form action="unpublished_gallery.php"
                                                                              method="post">
                                                                            <input type="hidden" name="galleryID"
                                                                                   value="<?php echo $gallery['id']; ?>"/>
                                                                            <input type="submit" class="btn btn-info"
                                                                                   name="btn_activate"
                                                                                   value="Activate">
                                                                        </form>

                                                                    </div>

                                                                    <form action="unpublished_gallery.php" method="post">
                                                                        <input type="hidden" name="galleryID"
                                                                               value="<?php echo $gallery['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_gallery.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";
?>
