<?php
include("db/cn.php");
if(isset($_SESSION['user_name']) && isset($_SESSION['user_role'])&& isset($_SESSION['outlet_name'])
&& isset($_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
	$outlet_address = $_SESSION['outlet_address'];
	$outlet_name = $_SESSION['outlet_name'];
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit4"])) {
    $prname = $_POST["prname"];
    $stock_qty1 = $_POST["prqty"];

    // Update stock quantity by product name
    $updateSql2 = "UPDATE products SET total_quantity = total_quantity + ? WHERE name = ?";
    $stmt = $connection->prepare($updateSql2);
    $stmt->bind_param("is", $stock_qty1, $prname);

    if ($stmt->execute()) {
        // Stock quantity updated successfully
        header("location:pos.php");
    } else {
        echo "Error updating Stock Quantity: " . $stmt->error;
    }
}

// Fetch products
$products_query = "SELECT * FROM products";
$products_result = mysqli_query($connection, $products_query);

// Store fetched products in an array
$products = array();
while ($row135 = mysqli_fetch_array($products_result)) {
    $products[] = $row135["name"];
}

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Update Stock Qty</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel="stylesheet" href="css/custom_css.css" />
    <!-- QRCode.js -->
    <script src="https://cdn.rawgit.com/davidshimjs/qrcodejs/gh-pages/qrcode.min.js"></script>
</head>

<style>
 #product-container-wrapper {
    max-height: 700px; /* Adjust the maximum height as needed */
    overflow-y: auto; /* Enable vertical scroll when content overflows */
}

@media (max-width: 880px) {
    .col-4 {
        width: 33.33% !important;
    }
    .fa-wifi {
        display: none !important;
    }
    .col-6 {
        width: 50% !important;
    }
}

/* Custom styles for sidebar */    /* Custom styles for sidebar */
    .sidebar {
        width: 100px;
        background: linear-gradient(360deg, #060606, #0A5064);
        position: fixed;
        bottom: 0;
        top: 60px;
        transition: transform 0.3s ease-in-out;
        transform: translateX(0);
        z-index: 1;
        height: 100%;
        border-radius: 0px;
		   transition: transform 0.3s ease-in-out, width 0.3s ease-in-out;
    }.sidebar.hide {
      transform: translateX(-100px); /* Move sidebar out of the viewport when it is hidden */
    }
      .sidebar .nav-item {
    padding: 10px 0 ;
    text-align: center;
    position: relative;
	font-size:12px;
}
    .sidebar .nav-link {
      color: #fff;
      position: relative; /* Ensure icon and text are positioned relative to the nav link */
      z-index: 2; /* Ensure the text and icon are above the hover background */
    }
    .sidebar .nav-link i {
      margin-bottom: 5px; /* Adjust the margin of the icon */
    }
    /* Custom styles for hover effect */
    .sidebar .nav-item:hover:after {
      background-color: #fff;
      content: '';
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      z-index: 1; /* Ensure the hover background is below the text and icon */
    }
    .sidebar .nav-item:hover .nav-link {
      color: #0A5064;
    }
    /* Custom styles for divider */
    .sidebar .nav-item:after {
      content: "";
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      height: 1px;
      background-color: #fff;
      transition: height 0.3s ease-in-out; /* Transition effect for smooth animation */
    }
    /* Custom styles for header */    .header {
        background: linear-gradient(270deg, #060606, #0A5064);
        color: #fff;
        padding: 10px;
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 2;
        height: 69px;
        box-shadow: #FFF 2px 0px 0px 0px;
        /* Ensure header appears above sidebar */
    }
    .header .navbar-toggler {
      color: #fff;
    }
    .header .navbar-toggler-icon {
      color: #fff; /* Set the color of the toggle button to white */
    }
    .header .navbar-toggler.white {
      color: #fff !important; /* Ensure the toggle button is white */
    }
    .header .navbar-brand {
      color: #fff; /* Set navbar brand text color to white */
    }
    .header .nav-item .nav-link {
      color: #fff; /* Set nav link text color to white */
    }
    .main-content {
      margin-top: 50px; /* Height of fixed header + padding */
      margin-left: 100px; /* Width of sidebar */
      padding-top: 30px; /* Height of fixed header */
    }
	/* Custom styles for search bar */
    .search-container {
      position: relative;
      display: inline-block;
      width: 300px;
      margin: 2px auto;
    }
    .search-input {
      width: 100%;
      padding: 10px 30px 10px 10px; /* Adjust padding to accommodate the icon */
      border: 1px solid #ccc;
      
      font-size: 16px;
    }
    .search-icon {
      position: absolute;
      top: 50%;
      right: 10px;
      transform: translateY(-50%);
      color: #aaa;
    }
    .search-icon i {
      position: relative;
    }
    /* Your CSS styles here */
    .dropdown-options {
        display: none;
        position: absolute;
        z-index: 1000;
        width: calc(94% - 2px);
        max-height: 200px; /* Set a fixed max-height for the dropdown */
        overflow-y: auto; /* Add scroll if content exceeds max-height */
        border: 1px solid #ccc;
        background-color: #fff;
    }
    .option {
        padding: 5px 10px;
        cursor: pointer;
        transition: background-color 0.3s;
    }
    .option:hover {
        background-color: #353535;
        color:#fff;
    }
    .highlight {
        background-color: #353535;
		color:#fff;
    }
.btn-dark {
    background: #0A5064;
    color: #fff;
    border: 1px solid #0A5064;
}

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

.bg-dark2 {
    background: #8A1218;
    color: #fff;
}

.modal-content {
    animation: modal-animation 0.5s;
}

@keyframes modal-animation {
    from {
        opacity: 0;
        transform: scale(0.8);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}
label{font-size:12px; font-weight:600;}
</style>

</style>
<body>
<body>
<body>
<body>
<?php include("common/hd.php"); ?>
<!-- Sidebar -->
<?php include("common/sd.php"); ?>
<div class="main-content">

    <div class="container">
        <div class="row">
            <div class="col-sm-9">
                <h3 style="color:#212529;"> Update Product Quantity</h3>
            </div>
            <div class="col-sm-3">
                <a href="pos.php"><button type="button" class="btn btn-dark" style="float:left;"><i class="fa fa-arrow-left"></i></button></a>
            </div>
        </div>
    </div><br />
    <div class="container">
        <div class="row">
            <div class="col-sm-3"></div>
            <div class="col-sm-6">
                <form id="updateForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Product Name</label>
                        <input type="text" id="searchInput" class="search-input form-control" />
                        <div id="filteredOptions" class="dropdown-options"></div>
                    </div>
                    <input type="hidden" name="prname" id="selectedProduct">
                    <div class="form-group">
                        <label>Product Quantity</label>
                        <input type="text" name="prqty" class="form-control" />
                    </div>
                    <button type="submit" name="submit4" class="btn btn-dark form-control">Enter</button>
                </form>
            </div>
            <div class="col-sm-3"></div>
        </div>
    </div>

</div>
</body>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
    var sidebar = document.getElementById("sidebar");
    var overlay = document.createElement("div");
    overlay.classList.add("overlay");
    document.body.appendChild(overlay);

    function toggleSidebar() {
        sidebar.classList.toggle("hide");
        overlay.classList.toggle("hide");

        var mainContent = document.querySelector(".main-content");
        if (sidebar.classList.contains("hide")) {
            mainContent.style.marginLeft = "0";
        } else {
            mainContent.style.marginLeft = "100px"; // Width of the sidebar
        }
    }

    overlay.addEventListener("click", function () {
        toggleSidebar();
    });

    // Add toggle functionality to navbar toggler
    var navbarToggler = document.querySelector(".navbar-toggler");
    navbarToggler.addEventListener("click", function () {
        toggleSidebar();
    });

    // Automatically hide sidebar on all screen sizes
    // sidebar.classList.add("hide");
    // overlay.classList.add("hide");
    
    // Add class to body to prevent scrolling when sidebar is open
    // document.body.classList.add("noscroll");

    // Check if screen size is larger than a certain threshold (e.g., 768px for desktop screens)
    function checkScreenSize() {
        if (window.innerWidth > 768) {
            sidebar.classList.remove("hide");
            overlay.classList.remove("hide");
        } else {
            sidebar.classList.add("hide");
            overlay.classList.add("hide");
        }
    }

    // Check screen size on initial load and whenever the window is resized
    checkScreenSize();
    window.addEventListener("resize", checkScreenSize);
});
 

    $(document).ready(function(){
        var products = <?php echo json_encode($products); ?>;
        var $searchInput = $('#searchInput');
        var $filteredOptions = $('#filteredOptions');
        var optionSelected = false;

        $searchInput.on('input', function(){
            var value = $(this).val().toLowerCase();
            var html = '';
            products.forEach(function(product) {
                if (product.toLowerCase().includes(value)) {
                    html += '<div class="option" data-value="' + product + '">' + product + '</div>';
                }
            });
            $filteredOptions.html(html);
            if (value.length > 0) {
                $filteredOptions.show();
                $filteredOptions.find('.option').first().addClass('highlight');
            } else {
                $filteredOptions.hide();
            }
        });

        // Handle keyboard events
        $searchInput.on('keydown', function(e) {
            var $options = $('.option');
            var currentIndex = $options.index($('.option.highlight'));
            switch (e.which) {
                case 40: // Down Arrow
                    e.preventDefault();
                    if (currentIndex === -1 || currentIndex === $options.length - 1) {
                        $options.first().addClass('highlight').siblings().removeClass('highlight');
                    } else {
                        $options.eq(currentIndex + 1).addClass('highlight').siblings().removeClass('highlight');
                    }
                    break;
                case 38: // Up Arrow
                    e.preventDefault();
                    if (currentIndex === -1 || currentIndex === 0) {
                        $options.last().addClass('highlight').siblings().removeClass('highlight');
                    } else {
                        $options.eq(currentIndex - 1).addClass('highlight').siblings().removeClass('highlight');
                    }
                    break;
                case 13: // Enter
                    if (currentIndex !== -1) {
                        optionSelected = true;
                        var selectedProduct = $options.eq(currentIndex).data('value');
                        $('#selectedProduct').val(selectedProduct);
                        $searchInput.val(selectedProduct);
                        $filteredOptions.hide();
                        return false; // Prevent form submission
                    }
                    break;
            }
            // Adjust scroll position
            var $highlightedOption = $('.option.highlight');
            if ($highlightedOption.length) {
                var optionTop = $highlightedOption.position().top;
                var optionHeight = $highlightedOption.outerHeight();
                var visibleAreaHeight = $filteredOptions.innerHeight();
                var scrollTop = $filteredOptions.scrollTop();
                if (optionTop < 0) {
                    $filteredOptions.scrollTop(scrollTop + optionTop); // Scroll up
                } else if (optionTop + optionHeight > visibleAreaHeight) {
                    $filteredOptions.scrollTop(scrollTop + optionTop + optionHeight - visibleAreaHeight); // Scroll down
                }
            }
        });

        $(document).on('click', '.option', function(){
            optionSelected = true;
            var selectedProduct = $(this).data('value');
            $('#selectedProduct').val(selectedProduct);
            $searchInput.val(selectedProduct);
            $filteredOptions.hide();
        });

        $('#updateForm').submit(function(){
            return optionSelected;
        });
    });
</script>
</html>