<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";

$start_date = isset($_GET['from_date']) ? $_GET['from_date'] : date('Y-m-d');
$end_date   = isset($_GET['to_date']) ? $_GET['to_date'] : date('Y-m-d');
$status     = isset($_GET['status']) ? $_GET['status'] : '';
$salesman   = 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;

// Base query
$sql = "SELECT 
        COALESCE(o.id, '') AS order_id,
        COALESCE(c.company_name, '') AS company_name,
        p.item_name AS product_name,
        COALESCE(d.total, p.price) AS rate,
        COALESCE(d.quantity, 0) AS quantity,
        COALESCE(d.total * d.quantity, 0) AS total_amt,
        COALESCE(o.status, 'N/A') AS status,
        cat.category_name,
        p.stock,
        p.reorder_level
    FROM tbl_stock_items p
    LEFT JOIN order_items d ON p.id = d.product_id
    LEFT JOIN orders o ON d.order_id = o.id
    LEFT JOIN tbl_customers c ON o.customer_id = c.id
    JOIN tbl_categories cat ON p.category = cat.id";

// Prepare dynamic WHERE conditions
$conditions = [];
$conditions[] = "p.stock <= p.reorder_level"; // always filter low stock

if ($salesman > 0) {
    $conditions[] = "o.salesman_id = $salesman";
}

if (!empty($status)) {
    $status_safe = mysqli_real_escape_string($con, $status);
    $conditions[] = "o.status = '$status_safe'";
}

if ($customer > 0) {
    $conditions[] = "o.customer_id = $customer";
}

if ($category > 0) {
    $conditions[] = "p.category = $category";
}

// Combine conditions
if (count($conditions) > 0) {
    $sql .= " WHERE " . implode(" AND ", $conditions);
}

// Order by
$sql .= " ORDER BY o.id DESC";

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

// Fetch first row
$firstRow = mysqli_fetch_assoc($result);
$company_name = $firstRow ? $firstRow['company_name'] : 'No Company';

// Reset pointer
mysqli_data_seek($result, 0);

// Fetch salesman name if selected
$name = "All Salesmen";
if ($salesman > 0) {
    $res = mysqli_query($con, "SELECT name FROM sales_man WHERE id = $salesman 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='create-order'">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 $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:75px;border:solid 1px #999999;text-align: left">C.Name</th> -->
      <th style="width:130px;border:solid 1px #999999;text-align:left;">P.Name</th>
      <th style="width:30px;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['rate']; ?></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>