<!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">
      <!-- endinject -->
      <!-- Plugin css for this page -->
      <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">
      <!-- End plugin css for this page -->
      <!-- inject:css -->
      <!-- endinject -->
      <!-- Layout styles -->
      <link rel="stylesheet" href="assets/css/style.css">
      <!-- End layout styles -->
      <!-- <link rel="shortcut icon" href="assets/images/favicon.png" /> -->
   </head>
   <body>
      <div class="container-scroller">
         <!-- partial:partials/_navbar.html -->
         <!-- partial:partials/_navbar.html -->
    
            <?php
         session_start();
               //  $userid = $_SESSION['user_id'] ;
               //  $username = $_SESSION['username'] ;
                 $userid   = $_SESSION['user_id'] ?? 0;
               $username = $_SESSION['username'] ?? 'Guest';
           
            include "header.php"; 
            
            ?>
         <!-- partial -->
         <div class="container-fluid page-body-wrapper">
            <!-- partial:partials/_sidebar.html -->
            <?php
               include "sidebar.php";
               ?>
            <!-- partial -->
            <!-- partial -->
            <style>
               .table .thead-dark th {
               color: #fff;
               background-color: #212529;
               border-color: #32383e;
               }
            </style>
            <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.css" rel="stylesheet">

            <div class="main-panel">
               <div class="content-wrapper">
                  <div class="page-header">
                     <h3 class="page-title">
                        <span class="page-title-icon bg-gradient-primary text-white me-2">
                        <i class="mdi mdi-home"></i>
                        </span> Dashboard
                     </h3>
                     <nav aria-label="breadcrumb">
                        <ul class="breadcrumb">
                           <li class="breadcrumb-item active" aria-current="page">
                              <!--<span></span>Overview <i class="mdi mdi-alert-circle-outline icon-sm text-primary align-middle"></i>-->
                           </li>
                        </ul>
                     </nav>
                  </div>
                  <div class="row">
                     <div class="col-md-8 grid-margin stretch-card">
                        <div class="card">
                           <div class="card-body" style="padding: 15px;">
                              <h4 class="card-title">Orders</h4>
                              <div class="table-responsive">
                                 <!-- ✅ Success / Error message section -->
                                 <?php if (isset($_GET['msg'])): ?>
                                    <div class="alert alert-<?php echo htmlspecialchars($_GET['type'] ?? 'success'); ?>">
                                          <?php echo htmlspecialchars($_GET['msg']); ?>
                                    </div>
                                 <?php endif; ?>

                                 <table class="table table-striped table-bordered  ">
                                    <thead class="thead-dark">
                                       <tr>
                                          <th style="width: 10px;">Order date</th>
                                          <th style="width: 10px;">Order no</th>
                                          <th style="width: 200px;">Company name</th>
                                          <th style="width: 10px;">Amount</th>
                                          <th style="width: 10px;">T.Items</th>
                                          <th style="width: 10px;">Status</th>
                                          <!--<th style="width: 10px;">Action</th>-->
                                          <th>View</th>
                                       </tr>
                                    </thead>
                                    <?php
include "config.php"; // DB connection

// Pagination settings
$limit = 20; // rows per page
$orders_page = isset($_GET['orders_page']) ? (int)$_GET['orders_page'] : 1;
if ($orders_page < 1) $orders_page = 1;
$offset = ($orders_page - 1) * $limit;


// Count total records (orders)
$total_query = "SELECT COUNT(*) AS total FROM orders";
$total_result = mysqli_query($con, $total_query);
$total_row = mysqli_fetch_assoc($total_result);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $limit);

// Fetch paginated data with item count
// $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
//                                                 LIMIT $offset, $limit
//                                             ";
$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
    HAVING status = 'Pending'
    ORDER BY o.created_at DESC
    LIMIT $offset, $limit
";

$result = mysqli_query($con, $query);
?>

<tbody>
<?php
if ($result && mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        // Add "table-secondary" class if cancelled
        $row_class = ($row['status'] === "Cancelled") ? "table-secondary text-muted" : "";

        echo '<tr class="' . $row_class . '">
        <td>' . htmlspecialchars($row['created_at']) . '</td>
        <td>#' . htmlspecialchars($row['id']) . '</td>
        <td>' . htmlspecialchars($row['company_name']) . '</td>
        <td>' . htmlspecialchars($row['total_amount']) . '</td>
        <td>' . htmlspecialchars($row['item_count']) . '</td>
        <td>' . htmlspecialchars($row['status']) . '</td>
        <td>
            <a href="view_items.php?order_id=' . urlencode($row['id']) . '">
                <i class="bi bi-eye"></i>
            </a>';
            
// Add edit option only if condition is true
if ($row['status'] != 'Delivered' && $row['status'] != 'Cancelled' && $row['issued_qty'] == 0) {
    echo ' <a href="order-form1.php?order_id=' . urlencode($row['id']) . '" class="ms-2">
                <i class="fa fa-pencil text-warning"></i>
            </a>';
}

echo '   </td>
      </tr>';

    }
} else {
    echo '<tr><td colspan="7">No orders found</td></tr>';
}
?>

</tbody>

</table>
</div>

<!-- Orders Pagination -->
<nav aria-label="Page navigation example">
    <ul class="pagination mt-3 d-flex justify-content-center">
        <li class="page-item <?php if ($orders_page <= 1) echo 'disabled'; ?>">
            <a class="page-link" href="?orders_page=<?php echo ($orders_page-1); ?>">
                <i class="fa fa-angle-left"></i>
            </a>
        </li>

        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
            <li class="page-item <?php if ($orders_page == $i) echo 'active'; ?>">
                <a class="page-link" href="?orders_page=<?php echo $i; ?>"><?php echo $i; ?></a>
            </li>
        <?php endfor; ?>

        <li class="page-item <?php if ($orders_page >= $total_pages) echo 'disabled'; ?>">
            <a class="page-link" href="?orders_page=<?php echo ($orders_page+1); ?>">
                <i class="fa fa-angle-right"></i>
            </a>
        </li>
    </ul>
</nav>


                              <div id="traffic-chart-legend" class="rounded-legend legend-vertical legend-bottom-left pt-4"></div>
                           </div>
                        </div>
                     </div>
                     <div class="col-md-4">
                        <div class="card">
                           <div class="card-body" style="padding: 15px;">
                              <h4 class="card-title">Sales Managers</h4>
                              <div class="table-responsive">
                                 <table class="table">
                                    <!-- <thead>
                                       <tr>
                                         <th> Assignee </th>
                                         <th> Subject </th>
                                         <th> Status </th>
                                         <th> Last Update </th>
                                         <th> Tracking ID </th>
                                       </tr>
                                       </thead> -->
                                    <tbody>
                                     <?php
                                    include "config.php"; // DB connection

                                    // Pagination settings
                                    $limit = 20; // number of records per page
                                    $sales_page = isset($_GET['sales_page']) ? (int)$_GET['sales_page'] : 1;
                                    if ($sales_page < 1) $sales_page = 1;
                                    $offset = ($sales_page - 1) * $limit;

                                    // Count total Sales Managers
                                    $total_query = "SELECT COUNT(*) AS total FROM sales_man";
                                    $total_result = mysqli_query($con, $total_query);
                                    $total_row = mysqli_fetch_assoc($total_result);
                                    $total_records = $total_row['total'];
                                    $total_pages = ceil($total_records / $limit);

                                    // Fetch only employees whose position is Sales Manager
                                    $query = "SELECT name FROM sales_man ORDER BY name ASC LIMIT $offset, $limit";
                                    $result = mysqli_query($con, $query);
                                    ?>

                                    <tbody>
                                    <?php
                                    if (mysqli_num_rows($result) > 0) {
                                       while ($row = mysqli_fetch_assoc($result)) {
                                          echo "<tr><td>" . htmlspecialchars($row['name']) . "</td></tr>";
                                       }
                                    } else {
                                       echo "<tr><td>No Sales Managers found</td></tr>";
                                    }
                                    ?>
                                    </tbody>
                                    </table>
                                    </div>

                                    <!-- Sales Managers Pagination -->
                                 <nav aria-label="Page navigation example">
                                    <ul class="pagination mt-3 d-flex justify-content-center">
                                       <li class="page-item <?php if ($sales_page <= 1) echo 'disabled'; ?>">
                                             <a class="page-link" href="?sales_page=<?php echo ($sales_page-1); ?>">
                                                <i class="fa fa-angle-left"></i>
                                             </a>
                                       </li>

                                       <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                             <li class="page-item <?php if ($sales_page == $i) echo 'active'; ?>">
                                                <a class="page-link" href="?sales_page=<?php echo $i; ?>"><?php echo $i; ?></a>
                                             </li>
                                       <?php endfor; ?>

                                       <li class="page-item <?php if ($sales_page >= $total_pages) echo 'disabled'; ?>">
                                             <a class="page-link" href="?sales_page=<?php echo ($sales_page+1); ?>">
                                                <i class="fa fa-angle-right"></i>
                                             </a>
                                       </li>
                                    </ul>
                                 </nav>

                           </div>
                        </div>
                        <div class="card">
                           <div class="card-body" style="padding: 15px;">
                              <h4 class="card-title">Categories</h4>
                              <div class="table-responsive">
                                 <table class="table">
                                    <!-- <thead>
                                       <tr>
                                         <th> Assignee </th>
                                         <th> Subject </th>
                                         <th> Status </th>
                                         <th> Last Update </th>
                                         <th> Tracking ID </th>
                                       </tr>
                                       </thead> -->
                                  <?php
                                    include "config.php"; // DB connection

                                    // Pagination settings
                                    $limit = 10; // number of categories per page
                                    $cat_page = isset($_GET['cat_page']) ? (int)$_GET['cat_page'] : 1;
                                    if ($cat_page < 1) $cat_page = 1;
                                    $offset = ($cat_page - 1) * $limit;


                                    // Get total number of categories
                                    $total_query = "SELECT COUNT(*) AS total FROM tbl_categories";
                                    $total_result = mysqli_query($con, $total_query);

                                    if (!$total_result) {
                                       die("Count query failed: " . mysqli_error($con));
                                    }

                                    $total_row = mysqli_fetch_assoc($total_result);
                                    $total_records = $total_row['total'];
                                    $total_pages = ceil($total_records / $limit);

                                    // Fetch categories
                                    $query = "SELECT id, category_name FROM tbl_categories ORDER BY category_name ASC LIMIT $offset, $limit";
                                    $result = mysqli_query($con, $query);

                                    if (!$result) {
                                       die("Category fetch failed: " . mysqli_error($con));
                                    }
                                    ?>

                                    <div class="table-responsive">
                                       <table class="table">
                                          <tbody>
                                          <?php
                                          if (mysqli_num_rows($result) > 0) {
                                             while ($row = mysqli_fetch_assoc($result)) {
                                                echo '<tr>
                                                         <td class="d-flex justify-content-between">
                                                            <span>' . htmlspecialchars($row['category_name']) . '</span>
                                                            <span>' . $row['id'] . '</span>
                                                         </td>
                                                      </tr>';
                                             }
                                          } else {
                                             echo '<tr><td>No Categories Found</td></tr>';
                                          }
                                          ?>
                                          </tbody>
                                       </table>
                                    </div>

                                   <!-- Categories Pagination -->
                                 <nav aria-label="Page navigation example">
                                    <ul class="pagination mt-3 d-flex justify-content-center">
                                       <li class="page-item <?php if ($cat_page <= 1) echo 'disabled'; ?>">
                                             <a class="page-link" href="?cat_page=<?php echo ($cat_page-1); ?>">
                                                <i class="fa fa-angle-left"></i>
                                             </a>
                                       </li>

                                       <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                                             <li class="page-item <?php if ($cat_page == $i) echo 'active'; ?>">
                                                <a class="page-link" href="?cat_page=<?php echo $i; ?>"><?php echo $i; ?></a>
                                             </li>
                                       <?php endfor; ?>

                                       <li class="page-item <?php if ($cat_page >= $total_pages) echo 'disabled'; ?>">
                                             <a class="page-link" href="?cat_page=<?php echo ($cat_page+1); ?>">
                                                <i class="fa fa-angle-right"></i>
                                             </a>
                                       </li>
                                    </ul>
                                 </nav>

                           </div>
                        </div>
                        <div class="card">
                           <div class="card-body" style="padding: 15px;">
                              <h4 class="card-title">Customers</h4>
                              <div class="table-responsive">
                                 <table class="table">
                                    <!-- <thead>
                                       <tr>sales_man
                                         <th> Assignee </th>
                                         <th> Subject </th>
                                         <th> Status </th>
                                         <th> Last Update </th>
                                         <th> Tracking ID </th>
                                       </tr>
                                       </thead> -->
                                   <?php
include "config.php"; // DB connection

// Pagination settings
$limit = 5; // number of rows per page
$cust_page = isset($_GET['cust_page']) ? (int)$_GET['cust_page'] : 1;
if ($cust_page < 1) $cust_page = 1;
$offset = ($cust_page - 1) * $limit;


// Count total records
$total_query = "SELECT COUNT(*) AS total FROM tbl_customers";
$total_result = mysqli_query($con, $total_query);
$total_row = mysqli_fetch_assoc($total_result);
$total_records = $total_row['total'];
$total_pages = ceil($total_records / $limit);

// Fetch paginated records
$query = "SELECT company_name FROM tbl_customers ORDER BY company_name ASC LIMIT $offset, $limit";
$result = mysqli_query($con, $query);
?>

<tbody>
<?php
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        echo '<tr>
                <td class="d-flex justify-content-between">
                    <span>' . htmlspecialchars($row['company_name']) . '</span>
                </td>
              </tr>';
    }
} else {
    echo '<tr><td>No customers found</td></tr>';
}
?>
</tbody>
</table>
</div>

<!-- Customers Pagination -->
<nav aria-label="Page navigation example">
    <ul class="pagination mt-3 d-flex justify-content-center">
        <li class="page-item <?php if ($cust_page <= 1) echo 'disabled'; ?>">
            <a class="page-link" href="?cust_page=<?php echo ($cust_page-1); ?>">
                <i class="fa fa-angle-left"></i>
            </a>
        </li>

        <?php for ($i = 1; $i <= $total_pages; $i++): ?>
            <li class="page-item <?php if ($cust_page == $i) echo 'active'; ?>">
                <a class="page-link" href="?cust_page=<?php echo $i; ?>"><?php echo $i; ?></a>
            </li>
        <?php endfor; ?>

        <li class="page-item <?php if ($cust_page >= $total_pages) echo 'disabled'; ?>">
            <a class="page-link" href="?cust_page=<?php echo ($cust_page+1); ?>">
                <i class="fa fa-angle-right"></i>
            </a>
        </li>
    </ul>
</nav>

                           </div>
                        </div>
                     </div>
                  </div>
               </div>
               <!-- content-wrapper ends -->
               <!-- partial:partials/_footer.html -->
               <?php
                  $title = "Php Header Footer";
                  include "footer.php";
                  ?>
               <!-- partial -->
            </div>
            <!-- main-panel ends -->
         </div>
         <!-- page-body-wrapper ends -->
      </div>
      <!-- container-scroller -->
      <!-- plugins:js -->
      <script src="assets/vendors/js/vendor.bundle.base.js"></script>
      <!-- endinject -->
      <!-- Plugin js for this page -->
      <script src="assets/vendors/chart.js/chart.umd.js"></script>
      <script src="assets/vendors/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
      <!-- End plugin js for this page -->
      <!-- inject:js -->
      <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>
      <!-- endinject -->
      <!-- Custom js for this page -->
      <script src="assets/js/dashboard.js"></script>
      <!-- End custom js for this page -->
   </body>
</html>