<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>
      <?php
include "config.php"; // db connection

// Get filters
$start_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$end_date   = isset($_GET['to_date']) ? $_GET['to_date'] : '';
$status     = isset($_GET['status']) ? $_GET['status'] : '';
$id         = isset($_GET['sales_men']) ? (int) $_GET['sales_men'] : 0;
$customer   = isset($_GET['customer']) ? (int) $_GET['customer'] : 0;
$category   = isset($_GET['category']) ? (int) $_GET['category'] : 0;
$item_code = isset($_GET['item_code']) ? (int) $_GET['item_code'] : 0;

// Base query
$sql = "SELECT 
            o.id, 
            c.company_name, 
            d.product_name,
            d.price,
            d.total AS rate, 
            d.quantity, 
            (d.total) AS total_amt, 
            o.status,
            cat.category_name
        FROM orders o
        JOIN tbl_customers c ON o.customer_id = c.id
        JOIN order_items d ON o.id = d.order_id
        JOIN tbl_stock_items p ON d.product_id = p.id
        JOIN tbl_categories cat ON p.category = cat.id
        WHERE 1=1"; // always true, so filters can be appended

// Apply filters only if provided
if (!empty($start_date) && !empty($end_date)) {
    $sql .= " AND DATE(o.created_at) BETWEEN '$start_date' AND '$end_date'";
}

if ($id > 0) {
    $sql .= " AND o.salesman_id = $id";
}

if (!empty($status)) {
    $sql .= " AND o.status = '" . mysqli_real_escape_string($con, $status) . "'";
}

if ($customer > 0) {
    $sql .= " AND o.customer_id = $customer";
}

if ($category > 0) {
    $sql .= " AND p.category = $category";
}
if ($item_code > 0) {
    $sql .= " AND p.id = $item_code";
}

$sql .= " ORDER BY o.id DESC";

$result = mysqli_query($con, $sql);
if (!$result) {
    die("SQL Error: " . mysqli_error($con));
}

// First row for company name
$firstRow = mysqli_fetch_assoc($result);
$company_name = $firstRow ? $firstRow['company_name'] : 'No Company';
mysqli_data_seek($result, 0);

// Salesman name
$name = "All Salesmen";
if ($id > 0) {
    $res = mysqli_query($con, "SELECT name FROM sales_man WHERE id = $id LIMIT 1");
    if ($row = mysqli_fetch_assoc($res)) {
        $name = $row['name'];
    }
}
?>

<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='order-register'">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;">
         <strong style="float: left; color:red;"><u><?php echo $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: Sales Man Register Report By- <strong><?php echo $name; ?></strong>
         </p>

         <table style="width:100%; font-size:11px; border:none;">
            <tr style="text-align:center; background-color:#CCCCCC;">
               <th style="width:30px;border:solid 1px #999999;text-align: left">#</th>
               <th style="width:150px;border:solid 1px #999999;text-align: left">C.Name</th>
               <th style="width:150px;border:solid 1px #999999;text-align:left;">P.Name</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">Rate</th>
               <th style="width:60px;border:solid 1px #999999;text-align: left;">Qty</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">T.Amt</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">Status</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">Category</th>
               <th style="width:200px;border:solid 1px #999999;text-align: right;"> </th>
            </tr>
            <tbody>
               <?php
               $slno = 1;
               while ($row = mysqli_fetch_assoc($result)) {
               ?>
                  <tr style="text-align:center;">
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $slno++; ?></td>
                     <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['product_name']; ?></td>
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['price']; ?></td>
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['quantity']; ?></td>
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['total_amt']; ?></td>
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['status']; ?></td>
                     <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['category_name']; ?></td>
                     <td style="border:solid 1px #999999;text-align: right;"></td>
                  </tr>
               <?php
               }
               ?>
            </tbody>
         </table>


               <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 getUserIP() {
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                return $_SERVER['HTTP_CLIENT_IP'];
            } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                return explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
            } else {
                return $_SERVER['REMOTE_ADDR'];
            }
        }
        ?>
<small>IP: <?php echo getUserIP(); ?></small>

                
                <small>Page 1/1</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('Salesman_Report_<?php echo date("Ymd_His"); ?>.pdf');//for print

         
                 // Restore original styles
                 element.style.backgroundColor = originalBackgroundColor;
                 element.style.color = originalTextColor;
             });
         });
      </script>
   
</body></html>