<style type="text/css">
                body {
                    font-size: 16px;
                }

                #PdfPrintSection {
                    font-family: "Poppins", sans-serif;
                }

                html {
                    margin: 0px;
                    padding: 0px;
                }

                th {

                    padding: 7px !important;
                    border: 0px !important;
                    vertical-align: middle;
                }

                td {

                    padding: 15px !important;
                    border: 0px !important;
                    vertical-align: middle;
                }

                .temporaryHide {
                    display: none !important;
                }

                @media (max-width: 768px) {
        .table-responsive-wrapper {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch; /* Smooth scrolling for iOS */
        }

        .table-responsive {
            width: 100%;
            min-width: 600px; /* Ensure table doesn't shrink too much */
        }
    }

    @media (max-width: 768px) {
    .table-responsive-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch; /* Smooth scrolling for iOS */
    }

    .table-responsive {
        width: 100%;
        min-width: 600px; /* Ensure table doesn't shrink too much */
    }

    /* Center logo on mobile */
    .logo-container {
        text-align: center !important;
    }

    .logo-container img {
        display: block;
        margin: 0 auto;
        max-width: 80px; /* Adjust size for mobile */
        height: auto;
    }

    /* Center Download Receipt button */
    .download-receipt {
        text-align: center !important;
        margin-top: 10px;
    }

    .download-receipt a {
        display: inline-block;
    }
}

            </style>
            <script src="https://cdn.bootcss.com/html2pdf.js/0.9.1/html2pdf.bundle.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.9.1/html2pdf.bundle.min.js"></script>
            <script>
                function createPDF() {
                    // Add the class to hide elements in PDF
                    document.querySelectorAll('.hideInPDF').forEach(element => {
                        element.classList.add('temporaryHide');
                    });

                    var element = document.getElementById('PdfPrintSection');
                    html2pdf(element, {
                        margin: 0.5,
                        padding: 0,
                        filename: '<?= date('Ymd') . '-' . $envoice_data[0]['memberName'] . '-PaymentsReceipt' ?>.pdf',
                        image: {
                            type: 'jpeg',
                            quality: 1
                        },
                        html2canvas: {
                            scale: 2,
                            logging: true
                        },
                        jsPDF: {
                            unit: 'in',
                            format: 'A4',
                            orientation: 'P'
                        }
                    }).then(() => {
                        // Remove the class after PDF generation
                        document.querySelectorAll('.hideInPDF').forEach(element => {
                            element.classList.remove('temporaryHide');
                        });
                    });
                }
            </script>
            <?php
            $header_data = getWebSyncsData('Header Settings', 'headerSettings', '');
            $GeneralSettings = GeneralSettings();
            $currency = isset($GeneralSettings['currencySymbol']) ? ($GeneralSettings['currencySymbol'] != '' || $GeneralSettings['currencySymbol'] != 'null' ?  $GeneralSettings['currencySymbol'] : '$') : '$';


            $tax = isset($GeneralSettings['TaxPercent']) ? ($GeneralSettings['TaxPercent'] != '' || $GeneralSettings['TaxPercent'] != 0 ?  $GeneralSettings['TaxPercent'] : 0) : 0;

            // print_r($envoice_data);
            ?>


            <?php $this->load->view('admin/includes/head'); ?>


            <?php $this->load->view('admin/includes/sidebar'); ?>

            <!-- Top navigation-->
            <?php $this->load->view('admin/includes/topbar'); ?>

            <div class="container p-5">

                <div class="row">

                    <div class="col-md-10 col-sm-12 shadow bg-white mx-auto d-block">
                        <div class="p-5">

                            <div class="bordered" id="PdfPrintSection">
                                <div style="background-color:#fff">
                                    <div class="row" style="vertical-align: middle!important;">
                                        <div class="col-md-3 col-sm-2 col-lg-2 col-xl-2 mx-auto d-block logo-container">
                                            <?php
                                            // Check if the left image is available
                                            $imagePath = $header_data[0]['leftImage'] ? ApiBaseUrl()['url'] . $header_data[0]['leftImage'] : base_url('assets/img/logo.png');


                                            $options = [
                                                "ssl" => [
                                                    "verify_peer" => false,
                                                    "verify_peer_name" => false,
                                                ],
                                            ];

                                            // Fetch the image contents
                                            $imageData = file_get_contents($imagePath, false, stream_context_create($options));

                                            // Encode the image data in Base64
                                            $base64Image = base64_encode($imageData);

                                            // Create the img tag with the Base64 string

                                            ?>
                                            <!-- <img src="<?= $header_data[0]['leftImage'] ? ApiBaseUrl()['url'] . $header_data[0]['leftImage'] : base_url('assets/img/logo.png') ?>" class="img-fluid" height="100px" width="100px"> -->
                                            <img src="data:image/png;base64,<?= $base64Image ?>" class="img-fluid" height="100px" width="100px">


                                        </div>
                                        <div class="col-md-7 col-sm-10 col-lg-10 col-xl-10 mx-auto d-block text-center">

                                            <h1 style="font-size:25px; font-weight: bold;color: black;"><?= PROJECT_NAME ?></h1>
                                            <p style="margin-left: -45px; margin-top: -5px !important;font-size: 16px;"><?= $header_data[0]['address']; ?></p>
                                            <h2 class="text-center fw-bold mt-4" style="font-size: 19px; margin-top: -5px !important; margin-bottom: 0px !important;">Payment Receipt</h2>
                                        </div>
                                        <div class="col-md-2"></div>
                                    </div>
                                </div>
                                <div>

                                    
                                    <hr style="margin: 0.4rem 0;opacity: 1;">



                                    <div class="row mt-4">
                                        <div class="col-md-6 fw-bold mb-3" style="font-size:16px;"><span style="border-bottom:1px solid black;">Devotee Details</span></div>
                                        <div class="col-md-6 fw-bold mb-3" style="font-size:16px"><span style="border-bottom:1px solid black;">Transaction Details</span></div>
                                        <div class="col-md-6">
                                            <p class="mb-0" style="font-size: 14px;"><span style="width: 100px;clear: left;float: left;text-align: left;padding-right: 2px;">Name</span><strong>: <?= $envoice_data[0]['memberName'] ?></strong></p>


                                            <!-- <p class="mb-0"><span style="width: 150px;clear: left;float: left;text-align: left;padding-right: 2px;">Address</span><strong>: <?php

                                                                                                                                                                //  $this->session->userdata('address');
                                                                                                                                                                $userDetails = getUserDetails()->data[0];
                                                                                                                                                                //   var_dump($userDetails);
                                                                                                                                                                echo isset($userDetails->addressLine1) ? $userDetails->addressLine1 . ', ' : '';
                                                                                                                                                                echo isset($userDetails->addressLine2) ? $userDetails->addressLine2 . ', ' : '';
                                                                                                                                                                echo isset($userDetails->stateTypes) ? $userDetails->stateTypes . ', ' : '';
                                                                                                                                                                echo isset($userDetails->cityTypes) ? $userDetails->cityTypes . ', ' : '';
                                                                                                                                                                echo isset($userDetails->zip) ? $userDetails->zip : '';
                                                                                                                                                                ?></strong></p> -->

                                            <p class="mb-0" style="font-size: 14px;"><span style="width: 100px;clear: left;float: left;text-align: left;padding-right: 2px;">Phone</span><strong>: <?= formatPhoneNumber(base64_decode($envoice_data[0]['prsnPhone'])); ?></strong></p>
                                        </div>
                                        <?php
                                        // var_dump($envoice_data[0]);
                                        $serviceDate = $envoice_data[0]['recCreDate'];

                                        $formatted_date = date('M d, Y', strtotime($serviceDate));

                                        ?>
                                        <div class="col-md-6">
                                            <p class="mb-0" style="font-size: 14px;"><span style="width: 111px;clear: left;float: left;text-align: left;padding-right: 2px; margin-left: 2px;">Receipt #</span><strong>: <?= $envoice_data[0]['tokenNumber'] ?></strong></p>
                                            <p class="mb-0" style="font-size: 14px;"><span style="width: 111px;clear: left;float: left;text-align: left;padding-right: 2px; margin-left: 2px;">Date &amp; Time</span><strong>: <?= $formatted_date . ' ' . $envoice_data[0]['cartCreTime']; ?></strong></p>
                                            <p class="mb-0" style="font-size: 14px;"><span style="width: 111px;clear: left;float: left;text-align: left;padding-right: 2px; margin-left: 2px;">Payment Mode</span><strong>: <?= $envoice_data[0]['paymentType'] ?></strong></p>
                                        </div>

                                    </div>
                                    <div class="table-responsive-wrapper">
                                    <table class="table table-responsive table-hover" style="border: 1px solid black; border-collapse: collapse; width: 100%; margin-top: 42px !important;">
                                        <thead>
                                            <tr style="background-color: rgb(245 247 228)!important;color:black!important;">
                                                <th style="border: 1px solid black;background-color: rgb(245 247 228)!important;color:black!important;border: 1px solid black!important;width: 20px !important;">#</th>
                                                <th style="border: 1px solid black;background-color: rgb(245 247 228)!important;color:black!important;border: 1px solid black!important;width: 85%!important;">Service Name</th>
                                                <!-- <th style="border: 1px solid black;background-color: rgb(245 247 228)!important;color:black!important;border: 1px solid black!important;">Qty</th> -->
                                                <th class="text-end" style="border: 1px solid black;background-color: rgb(245 247 228)!important;color:black!important;border: 1px solid black!important;">Amount</th>
                                            </tr>

                                        </thead>
                                        <tbody>

                                            <?php
                                            // var_dump($envoice_data[0]['paymentsData']);


                                            $sub_total = 0;
                                            $ids = [];
                                            $discountedAmt=0;

                                            foreach ($envoice_data[0]['paymentsData'] as $key => $val) {
                                                if(isset($val['discountedAmount']) && !empty($val['discountedAmount'])){
                                                    $discountedAmt+=$val['discountedAmount'];
                                                }
                                                // Check if fullPackage is true and handle accordingly
                                                if (isset($val['fullPackage']) && $val['fullPackage'] == "true") {
                                                    if (!in_array($val['ServiceId'], $ids)) {
                                                        array_push($ids, $val['ServiceId']);

                                                        // Collect all dates for the same ServiceId
                                                        $dateList = [];
                                                        foreach ($envoice_data[0]['paymentsData'] as $item) {
                                                            if ($item['ServiceId'] === $val['ServiceId'] && $item['fullPackage'] === "true") {
                                                                if (isset($item['serviceDate']) && !empty($item['serviceDate'])) {
                                                                    $dateList[] = $item['serviceDate'];
                                                                }
                                                            }
                                                        }
                                                        // $concatenatedDates = implode(", ", $dateList); // Concatenate dates
                                                        $concatenatedDates ='From: <b>'.$dateList[0].'</b> - To: <b>'. end($dateList).'</b>';

                                            ?>
                                                        <tr style="border-bottom:1px solid black;">
                                                            <td style="border: 1px solid black!important;width: 20px !important;"><?= $key+1?></td>
                                                            <td style="border: 1px solid black!important;width: 84%!important;">
                                                                <?= $val['ServiceSetup'] ?>
                                                                <?= !empty($concatenatedDates) ? '<br>' . $concatenatedDates : '' ?>
                                                                <?= isset($val['time']) && !empty($val['time']) ? '<br>Service Time: <b>' . $val['time'] . '</b>' : '' ?>
                                                            </td>
                                                            <!-- <td style="border: 1px solid black!important;">
                                                                <?= $val['qty'] ?>
                                                            </td> -->
                                                            <td class="text-end" style="border: 1px solid black!important;">
                                                                <?= $currency . ' ' . price_format($val['serviceAmount'] * $val['serviceOccurrencesNo'], 2); ?>
                                                            </td>
                                                        </tr>
                                                    <?php
                                                    }
                                                } else {
                                                    ?>
                                                    <tr style="border-bottom:1px solid black;">
                                                    <td style="border: 1px solid black!important;"><?= $key+1?></td>
                                                        <td style="border: 1px solid black!important;">
                                                            <?= $val['ServiceSetup'] ?>
                                                            <?= isset($val['serviceDate']) && !empty($val['serviceDate']) ? '<br>Service Date: <b>' . $val['serviceDate'] . '</b>' : '' ?>
                                                            <?= isset($val['time']) && !empty($val['time']) ? '<br>Service Time: <b>' . $val['time'] . '</b>' : '' ?>
                                                        </td>
                                                        <!-- <td style="border: 1px solid black!important;">
                                                            <?= $val['qty'] ?>
                                                        </td> -->
                                                        <td class="text-end" style="border: 1px solid black!important;">
                                                            <?= $currency . ' ' . price_format($val['serviceAmount'], 2); ?>
                                                        </td>
                                                    </tr>
                                            <?php
                                                }

                                                $sub_total += $val['serviceAmount'];
                                            }
                                            ?>


                                            <?php

                                            $tax_total_in_price = ($sub_total * $tax) / 100;

                                            ?>


                                            <!-- <tr style="background-color: white; border-bottom:1px solid black;" class="fw-bold">
                                                <td class="text-start" style="border: 1px solid black;">Tax</td>
                                                <td style="border: 1px solid black;"><?= $tax; ?>%</td>
                                                <td style="border: 1px solid black;" class="text-end"><?= $currency . ' ' . price_format($tax_total_in_price, 2); ?></td>
                                            </tr> -->

<?php if($discountedAmt>0){
    ?>
                                            <tr style="background-color: white;" class="fw-bold">
                                                <td colspan="2" style="border: 1px solid black!important;" class="text-end">Total Discounts:</td>
                                              
                                                <td style="border: 1px solid black!important;" class="text-end"><?= $currency . ' ' . price_format(($discountedAmt), 2); ?></td>
                                            </tr>
<?php
}
?>
                                            <tr style="background-color: white;" class="fw-bold">
                                                <td colspan="2" style="border: 1px solid black!important;" class="text-end">Total Amount:</td>
                                              
                                                <td style="border: 1px solid black!important;" class="text-end"><?= $currency . ' ' . price_format(($sub_total + $tax_total_in_price), 2); ?></td>
                                            </tr>


                                        </tbody>
                                    </table>
                                    </div>

                                </div>

                                <div class="row pt-3">
                                    <div class="col-md-7">
                                        <!-- <p>Thank you for using our services.</p> -->
                                        <p class="mb-0 pb-0"><b>Recieved with Thanks.</b></p>
                                        <p><b>Temple Management</b></p>
                                    </div>

                                    <div class="col-md-5 text-end">
                                        <div class="hideInPDF download-receipt">
                                            <a class="btn" style="background-color: #B02135;color: #fff;" class="html2PdfConverter" onclick="createPDF()">Download Receipt <i class="fa fa-download"></i></a>
                                        </div>
                                    </div>

                                </div>
                            </div>


                            <!-- <div class="row pt-3">
                                <div class="col-md-7">
                                    <p><?= PROJECT_NAME ?> Management.</p>
                                </div>

                                <div class="col-md-5 text-end">
                                    <a class="btn" style="background-color: #B02135;color: #fff;" class="html2PdfConverter" onclick="createPDF()">Download Receipt <i class="fa fa-download"></i></a>
                                </div>

                            </div> -->
                        </div>
                    </div>
                </div>

            </div>
            </div>
            <!-- Bootstrap core JS-->
            <script src="<?= base_url('admin_assets/js/bootstrap.bundle.min.js') ?>"></script>
            <!-- Core theme JS-->
            <script src="<?= base_url('admin_assets/js/theme.js') ?>"></script>

            <script type="text/javascript">
                $("#heading").text('Payments Receipt');
            </script>
            </body>

            </html>