<?php include_once('include/head.php'); ?>

<body>

    <div class="main-page-wrapper">

        <?php include_once('include/header.php'); ?>


        <!-- page-title-area start -->
        <div class="page-title-area text-center pt-100 pb-100 pt-lg-130 pb-lg-110"
            data-background="assets/img/bg/main-bg.png">

            <div class="container">

                <h2 class="page-title mb-15">Certificate</h2>

            </div>

        </div>
        <!-- page-title-area end -->


        <!-- ht-service-section start -->
        <section class="ht-service-section position-relative servicepics">

            <div class="ht-service-wrapper-bg">

                <div class="container">

                    <!--<h3 class="text-center">Certificate</h3>-->

                    <div class="row text-center">

                        <?php

                        $certificate = mysqli_query(
                            $conn,
                            "SELECT * FROM tbl_certificate ORDER BY id DESC"
                        );

                        if ($certificate && mysqli_num_rows($certificate) > 0) {

                            while ($row = mysqli_fetch_assoc($certificate)) {

                                $photo = $row['photo'];
                                $caption = $row['caption'];

                                $extension = strtolower(
                                    pathinfo($photo, PATHINFO_EXTENSION)
                                );

                                /*
                                |--------------------------------------------------------------------------
                                | Certificate File URL
                                |--------------------------------------------------------------------------
                                */

                                $file_url =
                                    "admin/img/certificate/" .
                                    rawurlencode($photo);

                        ?>

                                <div class="col-md-4 mb-4">

                                    <div class="certificate-card">

                                        <?php if ($extension === 'pdf') { ?>

                                            <!-- PDF THUMBNAIL -->

                                            <a
                                                href="<?php echo htmlspecialchars($file_url); ?>"
                                                data-fancybox="certificate-pdf"
                                                data-type="iframe"
                                                data-caption="<?php echo htmlspecialchars($caption); ?>"
                                                class="certificate-pdf-link">

                                                <div class="certificate-pdf-thumbnail">

                                                    <canvas
                                                        class="pdf-thumbnail"
                                                        data-pdf="<?php echo htmlspecialchars($file_url); ?>">
                                                    </canvas>

                                                    <div class="pdf-loading">
                                                        Loading...
                                                    </div>

                                                </div>

                                            </a>


                                        <?php } else { ?>

                                            <!-- IMAGE THUMBNAIL -->

                                            <a
                                                href="<?php echo htmlspecialchars($file_url); ?>"
                                                data-fancybox="certificate"
                                                data-caption="<?php echo htmlspecialchars($caption); ?>">

                                                <div class="certificate-image-thumbnail">

                                                    <img
                                                        src="<?php echo htmlspecialchars($file_url); ?>"
                                                        alt="<?php echo htmlspecialchars($caption); ?>">

                                                </div>

                                            </a>

                                        <?php } ?>


                                        <!-- Caption -->

                                        <?php if (!empty($caption)) { ?>

                                            <h5 class="certificate-caption">

                                                <?php
                                                echo htmlspecialchars($caption);
                                                ?>

                                            </h5>

                                        <?php } ?>

                                    </div>

                                </div>

                        <?php

                            }

                        } else {

                        ?>

                            <div class="col-md-12">

                                <p class="text-center">
                                    No certificates available.
                                </p>

                            </div>

                        <?php

                        }

                        ?>

                    </div>

                </div>

            </div>

        </section>
        <!-- ht-service-section end -->


        <!--
        |--------------------------------------------------------------------------
        | PDF.js
        |--------------------------------------------------------------------------
        -->

        <script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.min.js"></script>


        <script>

            document.addEventListener("DOMContentLoaded", function() {

                /*
                |--------------------------------------------------------------------------
                | PDF.js Worker
                |--------------------------------------------------------------------------
                */

                pdfjsLib.GlobalWorkerOptions.workerSrc =
                    "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174/pdf.worker.min.js";


                /*
                |--------------------------------------------------------------------------
                | Find PDF Canvases
                |--------------------------------------------------------------------------
                */

                const pdfCanvases =
                    document.querySelectorAll(".pdf-thumbnail");


                pdfCanvases.forEach(function(canvas) {

                    const pdfUrl =
                        canvas.getAttribute("data-pdf");

                    const container =
                        canvas.parentElement;

                    const loading =
                        container.querySelector(".pdf-loading");


                    /*
                    |--------------------------------------------------------------------------
                    | Load PDF
                    |--------------------------------------------------------------------------
                    */

                    pdfjsLib.getDocument(pdfUrl).promise

                        .then(function(pdf) {

                            /*
                            |--------------------------------------------------------------------------
                            | Get First Page
                            |--------------------------------------------------------------------------
                            */

                            return pdf.getPage(1);

                        })

                        .then(function(page) {

                            /*
                            |--------------------------------------------------------------------------
                            | Container Width
                            |--------------------------------------------------------------------------
                            */

                            const containerWidth =
                                container.clientWidth;


                            /*
                            |--------------------------------------------------------------------------
                            | Original PDF Viewport
                            |--------------------------------------------------------------------------
                            */

                            const viewport =
                                page.getViewport({
                                    scale: 1
                                });


                            /*
                            |--------------------------------------------------------------------------
                            | Calculate Scale
                            |--------------------------------------------------------------------------
                            */

                            const scale =
                                containerWidth / viewport.width;


                            /*
                            |--------------------------------------------------------------------------
                            | Scaled Viewport
                            |--------------------------------------------------------------------------
                            */

                            const scaledViewport =
                                page.getViewport({
                                    scale: scale
                                });


                            /*
                            |--------------------------------------------------------------------------
                            | Canvas Size
                            |--------------------------------------------------------------------------
                            */

                            canvas.width =
                                scaledViewport.width;

                            canvas.height =
                                scaledViewport.height;


                            /*
                            |--------------------------------------------------------------------------
                            | Render PDF
                            |--------------------------------------------------------------------------
                            */

                            const context =
                                canvas.getContext("2d");


                            return page.render({

                                canvasContext: context,

                                viewport: scaledViewport

                            }).promise;

                        })

                        .then(function() {

                            /*
                            |--------------------------------------------------------------------------
                            | Hide Loading
                            |--------------------------------------------------------------------------
                            */

                            if (loading) {

                                loading.style.display =
                                    "none";

                            }

                        })

                        .catch(function(error) {

                            console.log(
                                "PDF thumbnail error:",
                                error
                            );


                            if (loading) {

                                loading.innerHTML =
                                    '<i class="fa fa-file-pdf-o" style="font-size:50px;color:#e3342f;"></i>';

                            }

                        });

                });

            });

        </script>


        <!--
        |--------------------------------------------------------------------------
        | Fancybox
        |--------------------------------------------------------------------------
        -->

        <script>

            $(document).ready(function() {


                /*
                |--------------------------------------------------------------------------
                | IMAGE CERTIFICATES
                |--------------------------------------------------------------------------
                */

                $('[data-fancybox="certificate"]').fancybox({

                    buttons: [
                        "slideShow",
                        "thumbs",
                        "zoom",
                        "fullScreen",
                        "share",
                        "close"
                    ],

                    loop: false,

                    protect: true

                });


                /*
                |--------------------------------------------------------------------------
                | PDF CERTIFICATES
                |--------------------------------------------------------------------------
                */

                $('[data-fancybox="certificate-pdf"]').fancybox({

                    type: 'iframe',

                    smallBtn: true,

                    toolbar: true,

                    iframe: {
                        preload: false
                    },

                    buttons: [
                        "fullScreen",
                        "close"
                    ],

                    loop: false,

                    protect: true

                });

            });

        </script>


        <!--
        |--------------------------------------------------------------------------
        | Certificate CSS
        |--------------------------------------------------------------------------
        -->

        <style>

            /* =========================================
               CERTIFICATE CARD
            ========================================= */

            .certificate-card {
                width: 100%;
            }


            /* =========================================
               PDF THUMBNAIL
            ========================================= */

            .certificate-pdf-thumbnail {

                position: relative;

                width: 100%;

                height: 300px;

                overflow: hidden;

                background: #f5f5f5;

                border: 1px solid #ddd;

                transition: all 0.3s ease;

            }


            .certificate-pdf-thumbnail canvas {

                display: block;

                padding: 10px;

                width: 100% !important;

                height: 100% !important;

                object-fit: contain;

                background: #fff;

            }


            /* =========================================
               PDF LOADING
            ========================================= */

            .pdf-loading {

                position: absolute;

                inset: 0;

                display: flex;

                align-items: center;

                justify-content: center;

                background: #f5f5f5;

                font-size: 14px;

                color: #777;

            }


            /* =========================================
               PDF HOVER
            ========================================= */

            .certificate-pdf-link:hover
            .certificate-pdf-thumbnail {

                transform: translateY(-4px);

                box-shadow:
                    0 10px 25px rgba(0, 0, 0, 0.12);

            }


            /* =========================================
               IMAGE THUMBNAIL
            ========================================= */

            .certificate-image-thumbnail {

                width: 100%;

                height: 300px;

                overflow: hidden;

                border: 1px solid #ddd;

                background: #fff;

            }


            .certificate-image-thumbnail img {

                width: 100%;

                height: 100%;

                object-fit: contain;

                transition: transform 0.3s ease;

            }


            .certificate-image-thumbnail:hover img {

                transform: scale(1.03);

            }


            /* =========================================
               CAPTION
            ========================================= */

            .certificate-caption {

                margin-top: 15px;

                font-size: 17px;

                font-weight: 600;

                color: #222;

            }


            /* =========================================
               MOBILE
            ========================================= */

            @media (max-width: 767px) {

                .certificate-pdf-thumbnail,
                .certificate-image-thumbnail {

                    height: 250px;

                }

            }

        </style>


        <?php include_once('include/footer.php'); ?>

    </div>

</body>

</html>