<?php

include "config.php";
// Pagination variables
$results_per_page = 10; // Number of results to display per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1; // Current page number
$start_from = ($page - 1) * $results_per_page; // Calculate the starting row for the SQL query
$search = isset($_GET['search']) ? mysqli_real_escape_string($con, $_GET['search']) : "";
$where = "";
if (!empty($search)) {
    $where = "WHERE company_name LIKE '%$search%' 
              OR contact_person LIKE '%$search%' 
              OR contact_number LIKE '%$search%' 
              OR email LIKE '%$search%'";
}
// Get total number of results
$total_results_query = mysqli_query($con, "SELECT COUNT(*) AS total FROM tbl_customers $where");
$total_row = mysqli_fetch_assoc($total_results_query);
$total_results = $total_row['total'];
$total_pages = ceil($total_results / $results_per_page); // Calculate total pages
// Fetch results for the current page
$result = mysqli_query($con, "SELECT * FROM tbl_customers $where ORDER BY id DESC LIMIT $start_from, $results_per_page");
$compresult = mysqli_query($con, "SELECT * FROM companies where id =1");


$compresult = mysqli_query($con, "SELECT * FROM companies where id =1");
$comprow = mysqli_fetch_array($compresult);

?>

<html><head><script src="chrome-extension://mooikfkahbdckldjjndioackbalphokd/assets/prompt.js"></script></head>
   <body style="margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:12px;" cz-shortcut-listen="true">
      <br>
      <style>
         @media print {
         thead {display: table-header-group;}
         .b { display:none;}
         }
      </style>
      <style>
       table {
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
        }

        th, td {
            border: 1px dashed #999999!important;
            padding: 5px;
            text-align: left;
        }
   .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
/*            border: 1px solid #ccc;*/
            background-color: #f9f9f9;
        }
        th {
            background-color: #CCCCCC;
        }
         .button-row {
         display: flex;              /* Use flexbox for alignment */
         justify-content: center;    /* Center the buttons horizontally */
         align-items: center;        /* Center the buttons vertically (if needed) */
         margin-top: 20px;           /* Add some spacing from other content */
         }
         button {
         padding: 10px 20px;         /* Add padding to the buttons */
         margin: 10 10px;             /* Add spacing between the buttons */
         font-size: 16px;            /* Adjust font size */
         cursor: pointer;            /* Change cursor on hover */
         background-color: #407cbb;  /* Button background color */
         color: white;               /* Button text color */
         border: none;               /* Remove border */
         border-radius: 5px;         /* Rounded corners */
         transition: background-color 0.3s ease; /* Add hover transition */
         }
         button:hover {
         background-color: #0056b3; /* Darker background on hover */
         }
          @media print {
            .container {
                width: 95%;
                margin: 0;
                padding-left: 50px;
                border: none;
                background-color: transparent;
            }
        }
      </style>
      <div class="button-row">
         <button class="b" onclick="printContent()">Print</button>
         <button class="b" id="downloadPdfBtn">Download</button>
         <button class="b" onclick="window.location.href='add-customer'">Back</button>
      </div>
      <div id="invoice" class="container" style="height:1010px; margin:0px auto; background-color:#FFFFFF; padding:0px 0px 25px 0;">
         <div style="  height:1100px; margin:0px auto;">
            <div style="text-align:left; margin-top:5px;font-weight:bold; font-size:20px;display:flex;justify-content:space-between;align-items:center;">
               <!--   Task Register -->
               <strong style="float: left; color:red;"><u><?php echo $comprow['company_name'];?></u></strong>
              <small style="font-size:13px;font-weight:100;">Date: <?php echo date("d/m/Y"); ?></small>
                </div>
            <div style="width:100%; ">
              <p style="margin-top:5px;margin-bottom:5px;display:flex;justify-content:space-between;">Report: Customer Register Report

</p>               <!-- <p>Report of: All Branches of All Staffs  from 10.01.2025 to 11.01.2025</p> -->
                               <table style="width:100%; font-size:11px; border:none;">
                   <thead>
                      <tr style="text-align:center; background-color:#CCCCCC;">
                         <!-- <th style="width:30px;border:solid 1px #999999;text-align: left">#</th> -->
                         <th style="width:75px;border:solid 1px #999999;text-align: left">C.Name</th>
                         <th style="width:130px;border:solid 1px #999999;text-align:left;">C.Person</th>
                         <th style="width:80px;border:solid 1px #999999;text-align: left;">C.No</th>
                         <th style="width:150px;border:solid 1px #999999;text-align: left;">Email</th>
                         <th style="width:80px;border:solid 1px #999999;text-align: left;">GST</th>
                         <th style="width:200px;border:solid 1px #999999;text-align: left;">Address</th>
                      </tr>
                   </thead>
                   <tbody>
                    <?php while($row = mysqli_fetch_assoc($result)) { ?>

                      <tr style="text-align:center;">
                         <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['company_name'];?></td>
                         <td style="border:solid 1px #999999;text-align:left;"><?php echo $row['contact_person'];?></td>
                         <td style="border:solid 1px #999999;text-align:left;"><?php echo $row['contact_number'];?></td>
                         <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['email'];?></td>
                         <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['gst_details'];?></td>
                         <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['address'];?></td>
                         <!-- <td style="border:solid 1px #999999;text-align: right;"><?php echo $row['company_name'];?></td> -->
                      </tr>
            <?php } ?>
                   </tbody>
                </table>
<div class="pagination">
                    <?php
                    // Display pagination controls
                    // for ($i = 1; $i <= $total_pages; $i++) {
                    //     echo "<a href='?page=$i&search=$search' style='margin: 0 5px;'>" . $i . "</a>";
                    // }
                    ?>
                </div>
               <style>
    table {
    width: 100%;
    font-size: 11px;
    border-collapse: collapse;
    table-layout: fixed;
}
/*th, td {*/
/*    border: solid 1px #999999;*/
/*    padding: 3px;*/
/*    word-wrap: break-word;*/
/*    white-space: normal;*/
/*}*/
th {
    background-color: #CCCCCC;
    text-align: left;
}

</style>
                           </div>
              <div class="container" style="
            position: fixed;
    bottom: 10px;
    left: 0;
    display: flex
;
    justify-content: space-around;
    align-items: center;
    font-size: 15px;
    padding: 5px 15px;
    background: white;
    z-index: 9999;
    width: 100%;">
            <?php
                        function getClientIP() {
                            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                                return $_SERVER['HTTP_CLIENT_IP'];
                            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                                return $_SERVER['HTTP_X_FORWARDED_FOR'];
                            } else {
                                return $_SERVER['REMOTE_ADDR'];
                            }
                        }
                        $clientIP = getClientIP();
                    ?>
        
            <small>IP: <?php echo $clientIP; ?></small>
                
                <small>Page <?php echo $page; ?>/<?php echo $total_pages; ?></small>
                 <small>By PROGRAMERS.in</small>
            </div>
            <br>
            <br>
         </div>
      </div>
      <script>
         // Print the content
         function printContent() {
             window.print();
         }
      </script>
      <!--- To download report -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.4.0/jspdf.umd.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
      <script>
         document.getElementById('downloadPdfBtn').addEventListener('click', function () {
             const element = document.getElementById('invoice'); // Get the table element
         
             // Store original styles
             const originalBackgroundColor = element.style.backgroundColor;
             const originalTextColor = element.style.color;
         
             // Apply temporary styles
             element.style.backgroundColor = 'white';
             element.style.color = 'black';
         
             html2canvas(element, {
                 scale: 2, // Improve quality of the output
             }).then((canvas) => {
                 const imgData = canvas.toDataURL('image/png'); // Convert to image
                 const pdf = new jspdf.jsPDF('p', 'mm', 'a4'); // Create a PDF object
         
                 // Calculate dimensions for the image to fit A4
                 const imgWidth = 190; // A4 width minus margins
                 const imgHeight = (canvas.height * imgWidth) / canvas.width;
         
                 pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight); // Add image to PDF
                 pdf.save('Expense_report.pdf'); // Save as PDF
         
                 // Restore original styles
                 element.style.backgroundColor = originalBackgroundColor;
                 element.style.color = originalTextColor;
             });
         });
      </script>
   
</body></html>