<html><head><script src="chrome-extension://mooikfkahbdckldjjndioackbalphokd/assets/prompt.js"></script><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='order-register'">Back</button>
</div>
<?php
        include "config.php";
                    // --- Get filters ---
$from_date = isset($_GET['from_date']) ? $_GET['from_date'] : '';
$to_date   = isset($_GET['to_date']) ? $_GET['to_date'] : '';
$status    = isset($_GET['status']) ? $_GET['status'] : '';
$sales_men = isset($_GET['sales_men']) ? $_GET['sales_men'] : '';
$customer  = isset($_GET['customer']) ? $_GET['customer'] : '';
            // Salesman name
$name = "All Salesmen";
if ($sales_men > 0) {
    $res = mysqli_query($con, "SELECT name FROM sales_man WHERE id = $sales_men LIMIT 1");
    if ($row = mysqli_fetch_assoc($res)) {
        $name = $row['name'];
    }
}
    $res1 = mysqli_query($con, "SELECT * FROM companies LIMIT 1");
    if ($row1 = mysqli_fetch_assoc($res1)) {
        $cname = $row1['company_name'];
    }
?>
<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 $cname; ?></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;">
            <tbody><tr style="text-align:center; background-color:#CCCCCC;">
               <th style="width:70px;border:solid 1px #999999;text-align: left">O.Date</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left">O.No.</th>
               <th style="width:370px;border:solid 1px #999999;text-align:left;">Company Name</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">Amt</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">T.Items</th>
               <th style="width:50px;border:solid 1px #999999;text-align: left;">Status</th>
               <th style="width:200px;border:solid 1px #999999;text-align: right;"> </th>
            </tr>
            </tbody>
            
            <?php

    


// --- Build WHERE conditions dynamically ---
$where = [];

if ($from_date != '' && $to_date != '') {
    $where[] = "DATE(o.created_at) BETWEEN '$from_date' AND '$to_date'";
} elseif ($from_date != '') {
    $where[] = "DATE(o.created_at) >= '$from_date'";
} elseif ($to_date != '') {
    $where[] = "DATE(o.created_at) <= '$to_date'";
}

if ($status != '') {
    $where[] = "o.status = '$status'";
}

if ($sales_men != '') {
    $where[] = "o.salesman_id = '$sales_men'";
}

if ($customer != '') {
    $where[] = "o.customer_id = '$customer'";
}

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

// --- Query with filters ---
$query = "
    SELECT 
        o.id,
        c.company_name,
        o.total_amount,
        o.created_at,
        COUNT(d.id) AS item_count,
        CASE 
            WHEN SUM(CASE WHEN d.status = 'Delivered' THEN 1 ELSE 0 END) = COUNT(d.id) 
                AND COUNT(d.id) > 0
            THEN 'Delivered'
            WHEN SUM(CASE WHEN d.status = 'Cancelled' THEN 1 ELSE 0 END) = COUNT(d.id) 
                AND COUNT(d.id) > 0
            THEN 'Cancelled'
            ELSE 'Pending'
        END AS status
    FROM orders o
    LEFT JOIN tbl_customers c ON o.customer_id = c.id
    LEFT JOIN order_items d ON o.id = d.order_id
    $where_sql
    GROUP BY o.id
    ORDER BY o.created_at DESC
";

// $result = mysqli_query($con, $query);
                                                // Fetch orders + customer name with pagination filtered by order_id
                                    // $query = "
                                    //             SELECT 
                                    //                 o.id,
                                    //                 c.company_name,
                                    //                 o.total_amount,
                                    //                 o.created_at,
                                    //                 COUNT(d.id) AS item_count,
                                    //                 CASE 
                                    //                     WHEN SUM(CASE WHEN d.status = 'Delivered' THEN 1 ELSE 0 END) = COUNT(d.id) 
                                    //                         AND COUNT(d.id) > 0
                                    //                     THEN 'Delivered'
                                    //                     WHEN SUM(CASE WHEN d.status = 'Cancelled' THEN 1 ELSE 0 END) = COUNT(d.id) 
                                    //                         AND COUNT(d.id) > 0
                                    //                     THEN 'Cancelled'
                                    //                     ELSE 'Pending'
                                    //                 END AS status
                                    //             FROM orders o
                                    //             LEFT JOIN tbl_customers c ON o.customer_id = c.id
                                    //             LEFT JOIN order_items d ON o.id = d.order_id
                                    //             GROUP BY o.id
                                    //             ORDER BY o.created_at DESC
                                                
                                    //         ";
                                        $result = mysqli_query($con, $query);

                                    

                                    if (mysqli_num_rows($result) > 0) {
                                        while ($row = mysqli_fetch_assoc($result)) {
                                            ?>
<tbody>
   <tr class="order-row" data-id="<?php echo $row['id']; ?>" style="text-align:center; cursor:pointer;">
      <td style="border:solid 1px #999999;text-align: left;"><?php echo date("d-m-Y", strtotime($row['created_at'])); ?></td>
      <td style="border:solid 1px #999999;text-align: left;">#<?php echo $row['id']; ?></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 number_format($row['total_amount'], 2); ?></td>
      <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['item_count']; ?></td>
      <td style="border:solid 1px #999999;text-align: left;"><?php echo $row['status']; ?></td>
      <td></td>
   </tr>
</tbody>
               <?php  } }?>
         </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%;">
            <small>IP: 103.233.65.52</small>

                
                <small>Page 1/1</small>
                 <small>By PROGRAMERS.in</small>
            </div>
            <br>
            <br>
         </div>
      </div>
      <!-- 🔹 Modal for items -->
<div id="orderItemsModal" style="display:none; position:fixed; top:10%; left:20%; background:#fff; border:1px solid #999; padding:15px; width:60%; height:70%; overflow:auto; z-index:9999;">
   <button onclick="document.getElementById('orderItemsModal').style.display='none'">Close</button>
   <h3>Order Items</h3>
   <div id="orderItemsContent">Loading...</div>
</div>
...
<script>
// existing print + pdf download scripts remain same

// 🔹 Show order items on row click
document.querySelectorAll('.order-row').forEach(row => {
    row.addEventListener('click', function() {
        let orderId = this.getAttribute('data-id');
        fetch("get_order_items.php?order_id=" + orderId)
            .then(res => res.text())
            .then(data => {
                document.getElementById('orderItemsContent').innerHTML = data;
                document.getElementById('orderItemsModal').style.display = 'block';
            });
    });
});
</script>
