<?php
ob_start();
include("db/cn.php");
include("common/tx.php");
$product_name = $_GET['product_name'];
if(isset($_GET['id'])){
    $id = $_GET["id"];
    $del_query = mysqli_query($connection,"delete FROM products where id = '$id'
    ");
        echo '<script type="text/javascript">
window.location.href="product_detail.php?product_name=' . urlencode($product_name) . '";
</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; ?> - Product Batch Detail</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>
/* Preserved original color scheme with subtle improvements */
html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Quicksand', sans-serif;
    background: #f8f9fc;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

#wrapper {
    display: flex;
    min-height: 100vh;
}

#content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
}

#content {
    flex: 1 0 auto;
}

/* Footer styling - properly aligned */
footer.sticky-footer {
    flex-shrink: 0;
    margin-top: auto;
    background: #fff;
    border-top: 1px solid #e3e6f0;
    padding: 1rem 0;
}

/* Product Name Header Card - Enhanced Design */
.product-header-card {
    background: linear-gradient(135deg, #0A4657 0%, #0d5c72 100%);
    border-radius: 20px;
    padding: 20px 25px;
    margin-bottom: 25px;
    box-shadow: 0 10px 25px -5px rgba(10,70,87,0.2);
    position: relative;
    overflow: hidden;
}

.product-header-card::before {
    content: '';
    position: absolute;
    top: -50%;
    right: -20%;
    width: 200px;
    height: 200px;
    background: rgba(255,255,255,0.08);
    border-radius: 50%;
}

.product-header-card::after {
    content: '';
    position: absolute;
    bottom: -30%;
    left: -10%;
    width: 150px;
    height: 150px;
    background: rgba(255,255,255,0.05);
    border-radius: 50%;
}

.product-title {
    font-size: 1.8rem;
    font-weight: 800;
    color: #fff;
    margin: 0;
    letter-spacing: -0.3px;
    position: relative;
    z-index: 1;
}

.product-subtitle {
    color: rgba(255,255,255,0.85);
    font-size: 0.85rem;
    margin-top: 8px;
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
    position: relative;
    z-index: 1;
}

.product-badge {
    background: rgba(255,255,255,0.2);
    padding: 4px 12px;
    border-radius: 30px;
    font-size: 0.75rem;
    font-weight: 600;
}

.product-badge i {
    margin-right: 5px;
}

/* Table container styling */
#table-container {
    overflow-y: auto;
    max-height: 550px;
    border-radius: 16px;
    background: transparent;
}

/* Table styling */
#productTable {
    width: 100%;
    border-collapse: collapse;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 2px 8px 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: 14px 10px;
    border-bottom: none;
    color: #fff;
}

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

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

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

.btn-dark:hover {
    background: #0A4657;
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(10,70,87,0.3);
}

.btn-danger {
    background: #dc3545;
    border: none;
    border-radius: 8px;
    padding: 6px 12px;
    transition: all 0.2s;
}

.btn-danger:hover {
    background: #c82333;
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(220,53,69,0.3);
}

/* 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: 12px 24px;
    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;
}

/* Stats card */
.stats-card {
    background: white;
    border-radius: 16px;
    padding: 15px 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    margin-bottom: 20px;
    border: 1px solid #e9ecef;
}

.stat-item {
    text-align: center;
    border-right: 1px solid #e9ecef;
}

.stat-item:last-child {
    border-right: none;
}

.stat-number {
    font-size: 1.6rem;
    font-weight: 800;
    color: #0A4657;
    line-height: 1.2;
}

.stat-label {
    font-size: 0.7rem;
    color: #6c757d;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

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

.no-data-card i {
    color: #0A4657;
    opacity: 0.5;
}

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

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

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

/* 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;
}

/* Responsive */
@media (max-width: 768px) {
    .product-title {
        font-size: 1.2rem;
    }
    .stat-number {
        font-size: 1.2rem;
    }
    #productTable td, #productTable th {
        font-size: 0.7rem;
        padding: 8px 4px;
    }
    .btn-sm {
        padding: 4px 8px;
        font-size: 0.65rem;
    }
    .product-header-card {
        padding: 15px;
    }
    .stats-card {
        padding: 10px;
    }
    .stat-label {
        font-size: 0.6rem;
    }
}

.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;
}

.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;
}

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

/* Search row spacing */
.search-row {
    margin-bottom: 20px;
}
</style>
</head>

<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">
                    
                    <!-- Enhanced Product Name Header Card -->
                    <div class="product-header-card">
                        <div class="d-flex justify-content-between align-items-center flex-wrap">
                            <div>
                                <h1 class="product-title">
                                    <i class="fas fa-cubes mr-2"></i>
                                    <?php echo htmlspecialchars($product_name); ?>
                                </h1>
                                <div class="product-subtitle">
                                    <span class="product-badge">
                                        <i class="fas fa-tag"></i> Product Batch Details
                                    </span>
                                    <span class="product-badge">
                                        <i class="fas fa-calendar-alt"></i> <?php echo date('F d, Y'); ?>
                                    </span>
                                </div>
                            </div>
                            <div class="mt-2 mt-sm-0">
                                <a href="addproduct.php" class="btn btn-light btn-sm rounded-pill px-3" style="background: rgba(255,255,255,0.2); color: white; border: none;">
                                    <i class="fas fa-arrow-left"></i> Back to Products
                                </a>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Search Row - Restored with original border style -->
                  

                </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
$sql = "SELECT * FROM products where product_name = '$product_name'";
$result = $connection->query($sql);

// Calculate statistics
$totalProducts = 0;
$totalQuantity = 0;
$totalValue = 0;
$lowStockCount = 0;

if ($result && $result->num_rows > 0) {
    // Store results to display and calculate stats
    $productsData = [];
    while ($row = $result->fetch_assoc()) {
        $productsData[] = $row;
        $totalProducts++;
        $totalQuantity += $row["quantity"];
        $totalValue += $row["total_amount"];
        if ($row["quantity"] < 5) {
            $lowStockCount++;
        }
    }
    
    // Display Statistics Row
    echo '<div class="stats-card mb-4 w-100">
            <div class="row">
                <div class="col-4 col-sm-3 stat-item">
                    <div class="stat-number">' . $totalProducts . '</div>
                    <div class="stat-label">Total Batches</div>
                </div>
                <div class="col-4 col-sm-3 stat-item">
                    <div class="stat-number">' . $totalQuantity . '</div>
                    <div class="stat-label">Total Quantity</div>
                </div>
                <div class="col-4 col-sm-3 stat-item">
                    <div class="stat-number">₨ ' . number_format($totalValue, 0) . '</div>
                    <div class="stat-label">Total Value</div>
                </div>
                <div class="col-4 col-sm-3 stat-item">
                    <div class="stat-number" style="color: ' . ($lowStockCount > 0 ? '#dc3545' : '#28a745') . '">' . $lowStockCount . '</div>
                    <div class="stat-label">Low Stock Items</div>
                </div>
            </div>
          </div>';
    
    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>Batch No</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>Update</th>';
    echo '<th>Delete</th>';
    echo '</tr>';
    echo '</thead><tbody>';

    $sr_no = 1;
    foreach ($productsData as $row) {
        $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:#0a4657; color:#fff; padding:4px 12px; border-radius:20px; display:inline-block; font-size:0.75rem;">' . htmlspecialchars($row["batch_no"]) . '</span></td>';
        echo '<td style="font-weight:600; text-transform:capitalize;"><span style="background:#e6f7f0; color:#0A4657; padding:4px 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:4px 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:4px 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_update.php?id=' . $row["id"] . '">
                    <button type="button" class="btn btn-dark btn-sm"><i class="fas fa-edit"></i></button>
                </a>
              </td>';
        echo '<td>
                <button type="button" class="btn btn-danger btn-sm" onclick="confirmDelete(' . $row["id"] . ')"><i class="fas fa-trash-alt"></i></button>
              </td>';
        echo '</tr>';
    }
    echo '</tbody>';
} else {
    echo '<div class="no-data-card w-100 mt-4">
            <i class="fas fa-box-open fa-4x mb-3" style="color:#0A4657; opacity:0.4;"></i>
            <h4 style="color:#212529;">No Products Found</h4>
            <p class="text-muted mb-3">No products available for <strong>' . htmlspecialchars($product_name) . '</strong></p>
            <a href="addproduct.php" class="btn btn-primary rounded-pill px-4"><i class="fas fa-plus"></i> Add New Product</a>
          </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) {
            Swal.fire({
                title: "Deleted!",
                text: "Product has been removed.",
                icon: "success",
                timer: 1500,
                showConfirmButton: false
            });
            setTimeout(() => {
                window.location.href = "addproduct.php?id=" + productId;
            }, 500);
        }
    });
}
</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++;
                            if(searchText !== "" && productName.includes(searchText)) {
                                row.style.backgroundColor = "#fffbe6";
                                setTimeout(() => {
                                    if(row.style.backgroundColor === "rgb(255, 251, 230)") 
                                        row.style.backgroundColor = "";
                                }, 800);
                            } else {
                                row.style.backgroundColor = "";
                            }
                        } 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="12" 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>