<!DOCTYPE html>
<html lang="en">
   <head>
      <!-- Required meta tags -->
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>Pipscart</title>
      <!-- plugins:css -->
      <link rel="stylesheet" href="assets/vendors/mdi/css/materialdesignicons.min.css">
      <link rel="stylesheet" href="assets/vendors/ti-icons/css/themify-icons.css">
      <link rel="stylesheet" href="assets/vendors/css/vendor.bundle.base.css">
      <link rel="stylesheet" href="assets/vendors/font-awesome/css/font-awesome.min.css">
      <link rel="stylesheet" href="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.css">
      <link rel="stylesheet" href="assets/css/style.css">
      <style>
         .table thead th {
            background: #212529;
            color: #fff;
            text-align: center;
            vertical-align: middle;
         }
         .table td {
            vertical-align: middle;
         }
         .pagination .page-link {
            color: #212529;
         }
         .pagination .page-item.active .page-link {
            background-color: #212529;
            border-color: #212529;
            color: #fff;
         }
      </style>
   </head>
   <body>
      <div class="container-scroller">
         <?php
            session_start();
            $userid   = $_SESSION['user_id'] ?? 0;
            $username = $_SESSION['username'] ?? 'Guest';
            include "header.php"; 
         ?>
         <div class="container-fluid page-body-wrapper">
            <?php include "sidebar.php"; ?>
            <div class="main-panel">
               <div class="content-wrapper">
                  <div class="row g-1">
                     <div class="col-md-12 grid-margin stretch-card">
                        <div class="card">
                           <div class="card-body" style="padding: 15px;">
                              <h4 class="card-title mb-3">Orders</h4>
                              <div class="table-responsive">
                                 <table class="table table-striped table-hover table-bordered text-center">
                                    <thead>
                                       <tr>
                                          <th>Order Date</th>
                                          <th>Order No</th>
                                          <th>Customer</th>
                                          <th>Amount</th>
                                          <th>Qty</th>
                                          <th>Status</th>
                                          <!--<th>Action</th>-->
                                       </tr>
                                    </thead>
                                    <tbody>
                                       <?php
                                          include "config.php"; // DB connection
                                          
                                          // Pagination setup
                                          $limit = 10;
                                          $page  = isset($_GET['page']) ? (int)$_GET['page'] : 1;
                                          if($page < 1) $page = 1;
                                          $start = ($page - 1) * $limit;
                                          
                                          // Fetch orders with customer name
                                          $sql = "SELECT o.*, c.company_name 
                                                  FROM orders o
                                                  LEFT JOIN tbl_customers c ON o.customer_id = c.id where salesman_id='$userid'
                                                  ORDER BY o.created_at DESC 
                                                  LIMIT $start, $limit";
                                          $result = mysqli_query($con, $sql);
                                          
                                          // Count total rows
                                          $countRes = mysqli_query($con, "SELECT COUNT(*) AS total FROM orders");
                                          $totalRow = mysqli_fetch_assoc($countRes)['total'];
                                          $totalPages = ceil($totalRow / $limit);
                                          
                                          if (mysqli_num_rows($result) > 0) {
                                              while ($row = mysqli_fetch_assoc($result)) {
                                                  ?>
                                       <tr>
                                          <td><?php echo date("Y-m-d H:i:s", strtotime($row['created_at'])); ?></td>
                                          <td>#<?php echo $row['id']; ?></td>
                                          <td><?php echo !empty($row['company_name']) ? $row['company_name'] : 'N/A'; ?></td>
                                          <td><?php echo number_format($row['total_amount'], 2); ?></td>
                                          <td>1</td> <!-- Replace with actual qty if you have order_items table -->
                                          <td>
                                             <?php if($row['status']=="Processing"){ ?>
                                             <span class="badge bg-warning text-dark"><?php echo $row['status']; ?></span>
                                             <?php } elseif($row['status']=="Completed"){ ?>
                                             <span class="badge bg-success"><?php echo $row['status']; ?></span>
                                             <?php } elseif($row['status']=="Cancelled"){ ?>
                                             <span class="badge bg-danger"><?php echo $row['status']; ?></span>
                                             <?php } else { ?>
                                             <span class="badge bg-secondary"><?php echo $row['status']; ?></span>
                                             <?php } ?>
                                          </td>
                                         
                                       </tr>
                                       <?php
                                              }
                                          } else {
                                              echo "<tr><td colspan='7' class='text-center text-muted'>No orders found</td></tr>";
                                          }
                                       ?>
                                    </tbody>
                                 </table>
                              </div>
                              <!-- Pagination -->
                              <nav aria-label="Page navigation example">
                                 <ul class="pagination mt-3 d-flex justify-content-center">
                                    <!-- Previous -->
                                    <li class="page-item <?php if($page <= 1){ echo 'disabled'; } ?>">
                                       <a class="page-link" href="?page=<?php echo $page-1; ?>">
                                       <i class="fa fa-angle-left"></i>
                                       </a>
                                    </li>
                                    <?php for($i = 1; $i <= $totalPages; $i++): ?>
                                    <li class="page-item <?php if($page == $i){ echo 'active'; } ?>">
                                       <a class="page-link" href="?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                                    </li>
                                    <?php endfor; ?>
                                    <!-- Next -->
                                    <li class="page-item <?php if($page >= $totalPages){ echo 'disabled'; } ?>">
                                       <a class="page-link" href="?page=<?php echo $page+1; ?>">
                                       <i class="fa fa-angle-right"></i>
                                       </a>
                                    </li>
                                 </ul>
                              </nav>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>
               <!-- footer -->
               <footer class="footer">
                  <div class="d-sm-flex justify-content-center justify-content-sm-between">
                     <span class="text-muted text-center text-sm-left d-block d-sm-inline-block">
                     Copyright © <span id="year"></span>.
                     All rights reserved.
                     </span>
                     <span class="float-none float-sm-right d-block mt-1 mt-sm-0 text-center">
                     Developed by <a href="www.programers.in" target="_blank">Programers.in</a>
                     </span>
                  </div>
               </footer>
               <script>
                  document.getElementById("year").textContent = new Date().getFullYear();
               </script>
            </div>
         </div>
      </div>
      <!-- plugins:js -->
      <script src="assets/vendors/js/vendor.bundle.base.js"></script>
      <script src="assets/vendors/chart.js/chart.umd.js"></script>
      <script src="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
      <script src="assets/js/off-canvas.js"></script>
      <script src="assets/js/misc.js"></script>
      <script src="assets/js/settings.js"></script>
      <script src="assets/js/todolist.js"></script>
      <script src="assets/js/jquery.cookie.js"></script>
      <script src="assets/js/dashboard.js"></script>
   </body>
</html>
