<?php
session_start();
include './drawers/php/config.php';
if (isset($_GET['token'])) {
    $token = $_GET['token'];
    $_SESSION['token'] = $token;
    ?>
    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="./drawers/css/fa.css">
        <link rel="stylesheet" href="./drawers/css/style.css">
    </head>

    <body class="pachment">
        <?php
        $query_select_submissions = mysqli_query($conn, "SELECT * FROM `submissions` WHERE `token` = '$token'");
        if (mysqli_num_rows($query_select_submissions) > 0) {
            $query_row_sub = mysqli_fetch_array($query_select_submissions);
            ?>
            <section class="pachment_sec">
                <div class="pachment_cont">
                    <div class="pachment_flex">
                        <div class="pachment_fir_flex">
                            <div class="patc_fir_fle_head">
                                <div class="patc_fir_fle_head_flex">
                                    <div class="patc_fir_fle_head_flex_one">
                                        <div class="butt" style="margin-right: 20px;"><i class="fa-brands fa-chromecast"></i>
                                        </div>
                                        <div class="butt"><i class="fa-solid fa-magnifying-glass-plus"></i></div>
                                        <div class="butt"><i class="fa-solid fa-magnifying-glass-minus"></i></div>
                                    </div>
                                    <div class="patc_fir_fle_head_flex_two">
                                        <div class="ttxt">
                                            Transcript
                                        </div>
                                    </div>
                                    <div class="patc_fir_fle_head_flex_three">
                                        <div class="butt_dld" onclick="downloadPDFf()">
                                            <i class="fa-solid fa-download" style="margin-right: 10px;"></i>
                                            <input style=" position: relative; display: none;" type="text"
                                                value="./generate/pdf/<?php echo $query_row_sub['pdf_upload_path'] ?>"
                                                readonly>Download
                                        </div>
                                        <script>
                                            async function downloadPDFf() {
                                                const input = document.querySelector('.butt_dld input[type="text"]');
                                                const pdfUrl = input.value;

                                                try {
                                                    const response = await fetch(pdfUrl);
                                                    const blob = await response.blob();
                                                    const url = window.URL.createObjectURL(blob);

                                                    const a = document.createElement('a');
                                                    a.href = url;
                                                    a.download = pdfUrl.split('/').pop() || 'document.pdf';
                                                    document.body.appendChild(a);
                                                    a.click();

                                                    // Cleanup
                                                    window.URL.revokeObjectURL(url);
                                                    document.body.removeChild(a);
                                                } catch (error) {
                                                    console.error('Download failed:', error);
                                                    // Fallback to normal link opening
                                                    window.open(pdfUrl, '_blank');
                                                }
                                            }
                                        </script>

                                    </div>
                                </div>
                            </div>
                            <div class="patc_fir_fle_body">
                                <?php
                                if (!empty($query_row_sub['background_image_path_one'])) {
                                    ?>
                                    <div class="img">
                                        <img src="./generate/attachment/<?php echo $query_row_sub['background_image_path_one']; ?>"
                                            alt="">
                                    </div>
                                    <?php

                                }
                                if (!empty($query_row_sub['background_image_path_two'])) {
                                    ?>
                                    <div class="img">
                                        <img src="./generate/attachment/<?php echo $query_row_sub['background_image_path_two']; ?>"
                                            alt="">
                                    </div>
                                    <?php

                                }
                                if (!empty($query_row_sub['background_image_path_three'])) {
                                    ?>
                                    <div class="img">
                                        <img src="./generate/attachment/<?php echo $query_row_sub['background_image_path_three']; ?>"
                                            alt="">
                                    </div>
                                    <?php

                                }
                                if (!empty($query_row_sub['background_image_path_four'])) {
                                    ?>
                                    <div class="img">
                                        <img src="./generate/attachment/<?php echo $query_row_sub['background_image_path_four']; ?>"
                                            alt="">
                                    </div>
                                    <?php

                                }
                                if (!empty($query_row_sub['background_image_path_five'])) {
                                    ?>
                                    <div class="img">
                                        <img src="./generate/attachment/<?php echo $query_row_sub['background_image_path_five']; ?>"
                                            alt="">
                                    </div>
                                    <?php

                                }
                                ?>
                            </div>
                            <div class="patc_fir_fle_footer">
                                <div class="ttftr">
                                    Page 1 of <?php echo $query_row_sub['delivery_id_repeat']; ?>
                                </div>
                            </div>
                        </div>
                        <div class="index_second_flex">
                            <div class="sec_flex_end">
                                <i class="fa-solid fa-greater-than"></i>
                            </div>
                            <div class="flex_time">
                                <div class="watch">
                                    <i class="fa-regular fa-clock"></i>
                                </div>
                                <div class="text_flex"><strong style="font-size: 17px;">TIME SENSITIVE: </strong>This download
                                    is
                                    only available for
                                    <?php
                                    $expiryDate = new DateTime($query_row_sub['expiry_date']); // Database date format: 2025-05-29 15:29:26
                                    $currentDate = new DateTime(); // Current date and time
                                    $interval = $currentDate->diff($expiryDate);
                                    $daysRemaining = $interval->format('%r%a'); // %r shows negative sign if expired, %a is absolute days
                            
                                    if ($daysRemaining > 0) {
                                        ?>
                                        <strong style="font-size: 17px;"><?php echo $daysRemaining  ?></strong> more

                                        <?php

                                    }// elseif ($daysRemaining == 0) {
                                       // echo '<strong style="font-size: 17px; color: orange;">Expires today!</strong>';
                                   // } else {
                                     //   echo '<strong style="font-size: 17px; color: red;">Expired ' . abs($daysRemaining) . ' days ago</strong>';
                                   // }
                                    ?>
                                    days
                                </div>
                            </div>
                            <div class="head_flex">
                                <div class="first_head_flex">
                                    <div class="img">
                                        <img src="./drawers/images/logo.jpeg" alt="">
                                    </div>
                                    <h4>parchment</h4>
                                </div>
                                <div class="last_head_flex">
                                    <i class="fa-solid fa-caret-up"></i>
                                </div>
                            </div>
                            <div class="sec_flex_bdy_one">
                                <div class="order_det">
                                    <h3>Order Details</h3>
                                    <p><span>Order Placed By:</span> <?php echo $query_row_sub['ordered_by'] ?>
                                    </p>
                                    <p>
                                        <span>Date Delivered:</span><?php echo $query_row_sub['delivery_date'] ?> PDT
                                    </p>
                                </div>
                                <div class="from_det">
                                    <h3>FROM</h3>
                                    <p>
                                        <?php echo $query_row_sub['school_from'] ?><br />
                                        <?php echo $query_row_sub['school_from_post_office'] ?><br />
                                        <?php echo $query_row_sub['school_from_street'] ?><br />
                                    </p>
                                </div>
                                <div class="to_det">
                                    <h3>TO</h3>
                                    <p>
                                        <?php echo $query_row_sub['school_to'] ?><br />
                                        <?php echo $query_row_sub['school_to_email'] ?>
                                    </p>
                                </div>
                            </div>
                            <div class="sec_flex_bdy_two">
                                <h3>Documents in this delivery package</h3>
                                <div class="flex_sec_flex_bdy_two">
                                    <div class="cir_prog">
                                        <div class="e0e0">

                                        </div>
                                    </div>
                                    <div class="border">

                                    </div>
                                    <div class="doc_desc">
                                        <i class="fa-regular fa-file-pdf"></i>PDF<br />
                                        <?php echo $query_row_sub['delivery_id'] ?>.pdf<br />
                                        Delivery ID:
                                        <strong><?php echo $query_row_sub['delivery_id'] ?></strong><br />
                                        <div class="button_dold">
                                            <button onclick="downloadPdf(this)">
                                                <input style=" position: relative; display: none;" type="text"
                                                    value="./generate/pdf/<?php echo $query_row_sub['pdf_upload_path'] ?>"
                                                    readonly>
                                                <i class="fa-solid fa-download" style="margin-right: 10px;"></i>Download
                                            </button>

                                            <script>
                                                function downloadPdf(buttonElement) {
                                                    // Get the input element inside the button
                                                    const input = buttonElement.querySelector('input[type="text"]');

                                                    // Get the PDF path from the input value
                                                    const pdfPath = input.value;

                                                    if (!pdfPath) {
                                                        alert('No PDF path specified');
                                                        return;
                                                    }

                                                    // Extract the filename from the path
                                                    const filename = pdfPath.split('/').pop();

                                                    // Create a temporary anchor element to trigger the download
                                                    const a = document.createElement('a');
                                                    a.href = pdfPath;
                                                    a.download = filename || 'document.pdf';
                                                    document.body.appendChild(a);
                                                    a.click();
                                                    document.body.removeChild(a);
                                                }
                                            </script>
                                        </div>



                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
            <script src="./drawers/js/all.js"></script>
            <script src="./drawers/js/jquery.js"></script>
            <?php
        }
        ?>
    </body>

    </html>
    <?php
}

?>