<?php
ob_start();
include("db/cn.php");
include("common/tx.php");

if($_GET){
    $id = $_GET["id"];
    $del_query = mysqli_query($connection,"delete FROM products where id = '$id'
    ");
        echo "<script type='text/javascript'>window.location.href = 'addproduct.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title><?php echo $tx; ?></title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    <link href="../css/sb-admin-2.css" rel="stylesheet">
    
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

<style>
body {
    font-family: 'Quicksand', sans-serif;
    background: #f8f9fc;
}

/* Chart container styles */
.chart-container {
    position: relative;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

canvas {
    max-width: 100%;
    height: auto;
}

/* Button styles - original color scheme */
.bg-gradient-primary {
    background: #0A4657;
    color: #fff;
}

.btn-primary {
    background: #0A4657;
    color: #fff;
    border: 1px #0A4657 solid;
}

.btn-primary:hover {
    background: #fff;
    border: 1px #0A4657 solid;
    color: #0A4657;
}

/* Button dark style */
.btn-dark {
    background: #212529;
    border: none;
    border-radius: 8px;
    padding: 5px 12px;
    transition: all 0.2s ease;
}

.btn-dark:hover {
    background: #0A4657;
    transform: translateY(-1px);
}

.btn-dark i {
    margin-right: 5px;
}

/* Table container scroll */
#table-container {
    overflow-y: auto;
    max-height: 750px;
    border-radius: 12px;
    background: transparent;
}

/* Table styling */
#productTable {
    width: 100%;
    border-collapse: collapse;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

#productTable thead tr {
    background: #212529;
}

#productTable th {
    font-weight: 700;
    font-size: 0.8rem;
    letter-spacing: 0.3px;
    padding: 12px 10px;
    color: #fff;
    border-bottom: none;
}

#productTable td {
    padding: 10px 8px;
    vertical-align: middle;
    border-bottom: 1px solid #e9ecef;
    transition: background 0.2s ease;
}

#productTable tr:hover td {
    background-color: #f1f5f9;
}

/* Search container - ORIGINAL BORDER STYLE RESTORED */
.search-container {
    position: relative;
    display: flex;
    align-items: center;
    background: white;
    border-radius: 77px;
    border: 1px solid #0A4657;
    padding: 0;
    transition: all 0.2s ease;
}

.search-container:focus-within {
    box-shadow: 0 0 0 3px rgba(10,70,87,0.2);
    border-color: #0A4657;
}

#search-input {
    background: white;
    border: none;
    padding: 10px 20px;
    font-size: 0.9rem;
    border-radius: 77px;
    font-weight: 600;
    color: #2c3e50;
    width: 100%;
}

#search-input::placeholder {
    color: #6c757d;
    font-weight: 500;
}

#search-input:focus {
    outline: none;
    box-shadow: none;
}

/* Header styling */
h3 {
    color: #212529;
    font-weight: 700;
}

/* Empty state styling */
.no-data-card {
    background: #fff;
    padding: 40px;
    border-radius: 16px;
    text-align: center;
    box-shadow: 0 2px 10px rgba(0,0,0,0.05);
    border: 1px solid #e0e4e8;
}

.no-data-card p {
    background: transparent !important;
    color: #6c757d !important;
}

/* Low stock row styling */
.product-low-stock {
    background-color: #fff5f5 !important;
    border-left: 3px solid #dc3545;
}

.product-low-stock td {
    background-color: #fff5f5 !important;
}

/* Category and company chips */
td[style*="text-transform:capitalize"] {
    display: inline-block;
    background: #eef2ff;
    padding: 4px 12px;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    color: #1e3a8a;
    white-space: nowrap;
}

/* Responsive */
@media (max-width: 768px) {
    #productTable td, #productTable th {
        font-size: 0.7rem;
        padding: 6px 4px;
    }
    .btn-sm {
        padding: 3px 8px;
        font-size: 0.7rem;
    }
    .btn-dark i {
        margin-right: 2px;
    }
}

/* Scrollbar styling */
#table-container::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

#table-container::-webkit-scrollbar-track {
    background: #e9ecef;
    border-radius: 10px;
}

#table-container::-webkit-scrollbar-thumb {
    background: #adb5bd;
    border-radius: 10px;
}

/* Sticky header */
#table-container table thead tr th {
    position: sticky;
    top: 0;
    background: #212529;
    z-index: 10;
}

.fa-download:hover {
    color: #953E39;
}

/* View button icon spacing */
.btn-dark .fas.fa-eye {
    margin-right: 5px;
}
</style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <!-- Sidebar -->
        <?php
        include("common/sd.php");
        ?>
        <!-- End of Sidebar -->

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php');  ?>

                    <!-- Topbar Navbar -->
                    
                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                
                <div class="container-fluid">
<div class="row align-items-center">
<div class="col-sm-7">
<h3 style="color:#212529;font-weight:700;"> Product Management</h3>
<p class="text-muted mb-0" style="font-size:0.85rem;">Manage and view all product batches</p>
</div>
<div class="col-sm-5" style="border:1px solid #0A4657; padding:0px; border-radius:77px; background:#fff;">
<div class="search-container">
    <input type="text" id="search-input" class="form-control font-weight-bold" placeholder="Search Product by Name:-" style="border-radius:77px;border:none;" />
    <div class="search-icon">
      
    </div>
  </div>

</div>
</div>

</div>


<br />
    <div class="container-fluid text-center">
      <div class="row">
        <div class="col-sm-12" id="product-container-wrapper">
            <div class="row text-center" id="table-container" >
<?php
// Fetch products from database - grouped by product_name
$sql = "SELECT * FROM products GROUP BY product_name";
$result = $connection->query($sql);

if ($result->num_rows > 0) {
    echo '<table id="productTable" class="table table-striped table-hover table-sm bg-white">';
    echo '<thead>';
    echo '<tr style="color:#fff; background:#212529; font-size:14px;">';
    echo '<th>Sr#</th>';
    echo '<th>Item Code</th>';
    echo '<th>R.~ Qty ~</th>';
    echo '<th>Supplier</th>';
    echo '<th>Category</th>';
    echo '<th>Company</th>';
    echo '<th>Product</th>';
    // echo '<th>Sale Price(Rs)</th>';
    echo '<th>Inches</th>';
    // echo '<th>Total Amount(Rs)</th>';
    echo '<th>Detail</th>';
    echo '</tr>';
    echo '</thead><tbody>';

    $sr_no = 1;
    while ($row = $result->fetch_assoc()) {
        // Check for low stock (quantity < 5)
        $isLowStock = ($row["quantity"] < 5);
        $rowClass = $isLowStock ? 'product-low-stock' : '';

        echo '<tr style="font-size:14px;" class="product-row ' . $rowClass . '" data-category="'.$row["company"].'">';
        echo '<td style="font-weight:600;">' . $sr_no++ . '</td>';
        echo '<td style="font-weight:600;">' . htmlspecialchars($row["product_id"]) . '</td>';
        echo '<td style="font-weight:600; ' . ($isLowStock ? 'color:#dc3545;' : 'color:#0A4657;') . '">' . htmlspecialchars($row["quantity"]) . 
             ($isLowStock ? '<span class="low-stock-badge" style="background:#dc3545; color:white; font-size:9px; padding:2px 6px; border-radius:20px; margin-left:8px; display:inline-block;">⚠ Low</span>' : '') . 
             '</td>';
        echo '<td style="font-weight:600; text-transform:capitalize;"><span style="background:#e6f7f0; color:#0A4657; padding:3px 12px; border-radius:20px; display:inline-block; font-size:0.75rem;">' . htmlspecialchars($row["supplier"]) . '</span></td>';
        echo '<td class="text-capitalize" style="font-weight:600;"><span style="background:#f1f5f9; padding:3px 12px; border-radius:20px; display:inline-block; font-size:0.75rem;">' . htmlspecialchars($row["category"]) . '</span></td>';
        echo '<td class="text-capitalize" style="font-weight:600;"><span style="background:#eef2ff; padding:3px 12px; border-radius:20px; display:inline-block; font-size:0.75rem;">' . htmlspecialchars($row["company"]) . '</span></td>';
        echo '<td style="font-weight:700; color:#0A4657;">' . htmlspecialchars($row["product_name"]) . '</td>';
        // echo '<td class="text-capitalize" style="font-weight:600;">₨ ' . number_format($row["retail_price"], 0) . '</td>';
        echo '<td><span style="font-weight:600;">' . htmlspecialchars($row["inches"]) . '"</span></td>'; 
        // echo '<td><span style="font-weight:700; color:#1f5e3a;">₨ ' . number_format($row["total_amount"], 0) . '</span></td>';
        echo '<td>
                <a href="product_detail.php?product_name=' . urlencode($row["product_name"]) . '">
                    <button type="button" class="btn btn-dark btn-sm"><i class="fas fa-eye"></i> View Batch</button>
                </a>
              </td>';
        echo '</tr>';
    }
    echo '</tbody></table>';
} else {
    echo '<div class="no-data-card w-100 mt-4">
            <i class="fas fa-box-open fa-3x" style="color:#adb5bd;"></i>
            <h5 class="mt-3" style="color:#212529;">No Products Found</h5>
            <p style="background:transparent; color:#6c757d; padding:0;">No product data available</p>
          </div>';
}
?>

                </div>
        </div>
        
        <div class="" >
		
       

        </div>    
       </div>
    </div>
    
            
                <!-- /.container-fluid -->

            </div>
            <br><br>
            <!-- End of Main Content -->

            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>
    <!-- SweetAlert2 -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
function confirmDelete(productId) {
    Swal.fire({
        title: "Are you sure?",
        text: "Do you really want to delete this product?",
        icon: "warning",
        showCancelButton: true,
        confirmButtonColor: "#d33",
        cancelButtonColor: "#3085d6",
        confirmButtonText: "Yes, delete it!",
        cancelButtonText: "Cancel"
    }).then((result) => {
        if (result.isConfirmed) {
            window.location.href = "addproduct.php?id=" + productId;
        }
    });
}
</script>
<script>
    
    document.addEventListener("DOMContentLoaded", function() {
        // Search functionality - filter by product name (7th column)
        const searchInput = document.getElementById("search-input");
        if(searchInput) {
            searchInput.addEventListener("input", function() {
                var searchText = this.value.toLowerCase();
                var rows = document.querySelectorAll(".product-row");
                var visibleCount = 0;

                rows.forEach(function(row) {
                    var productNameCell = row.querySelector("td:nth-child(7)");
                    if (productNameCell) {
                        var productName = productNameCell.textContent.toLowerCase();
                        if (searchText === "" || productName.includes(searchText)) {
                            row.style.display = "";
                            visibleCount++;
                        } else {
                            row.style.display = "none";
                        }
                    }
                });
                
                // Show/hide no results message
                var tableBody = document.querySelector("#productTable tbody");
                var existingMsg = document.getElementById("noSearchResultMsg");
                
                if(visibleCount === 0 && rows.length > 0) {
                    if(!existingMsg && tableBody) {
                        var msgRow = document.createElement("tr");
                        msgRow.id = "noSearchResultMsg";
                        msgRow.innerHTML = '<td colspan="11" class="text-center py-4" style="background:#f8f9fc;"><i class="fas fa-search fa-2x text-muted mb-2 d-block"></i><span style="font-weight:600;">No matching products found</span><br><small class="text-muted">Try a different product name</small></td>';
                        tableBody.appendChild(msgRow);
                    }
                } else {
                    if(existingMsg) existingMsg.remove();
                }
            });
        }
    });
</script>
      
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

</body>

</html>