<?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'];
}

// Check if order_id is provided via GET request
if (!isset($_GET["order_id"])) {
    // Redirect or handle error
    exit("Order ID is missing.");
}

$order_id = mysqli_real_escape_string($connection, $_GET["order_id"]);

// Fetch data from sales table
$rec = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$order_id'");
$row = mysqli_fetch_array($rec);

$sumQuery = "SELECT SUM(product_total) AS total_cash_amount FROM log_user_sales WHERE order_id = '$order_id'";

// Execute the query
$result = $connection->query($sumQuery);

// Check if the query was successful
if ($result) {
    // Fetch the result as an associative array
    $row4 = $result->fetch_assoc();

    // Extract the sum of cash_amount and change_amount
    $totalCashAmount = $row4['total_cash_amount'];
}

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Detail</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <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">

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

 #table-container-refund {
    max-height: 580px; /* Adjust the maximum height as needed */
    overflow-y: auto; /* Enable vertical scroll when content overflows */
}
/* 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 */
    }
	    .dropdown-options {
        display: none;
        position: absolute;
        z-index: 1000;
        width:620px;
        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;
    }
    /* 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 {
      display: flex;
      align-items: center;
      width: 300px;
      margin: 2px auto;
    }
    .search-input {
      width: 100%;
      padding: 10px 30px 10px 10px; /* Adjust padding to accommodate the icon */
      border: 1px solid #ccc;
      border-radius: 7px;
      font-size: 16px;
    }
    .search-icon {
      position: relative;
      left: -25px; /* Adjust the position of the icon */
      color: #aaa;
    }
    .search-icon i {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
.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);
            }
        }
		
</style>
<body>

<?php include("common/hd.php");?>

<?php include("common/sd.php"); ?>

<div class="main-content">
    <form id="refund-form" method="post" action="">
        <br />
        <div class="container">
            <div class="row">
                <br>
                <div class="col-sm-7" >
                <div class="form-group">
                        
                        <input type="text" id="searchInput" class="search-input form-control" style="width:400px;" placeholder="Product Name" />
                        <div id="filteredOptions" class="dropdown-options"></div>
                    </div>
                    <br>
              <table id="cart-table" class="table table-striped table-sm text-center">
                    <thead>
                        <tr class="bg-dark text-white" style="border-radius:10px; font-size:14px;">
                        
                        <th>Item Code</th>
                            <th>Description</th>
                            <th>Qty</th>
                            <th>Unit Price</th>
                            <th>Amount</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <!-- Cart items will be dynamically added here -->
                    </tbody>
                </table>
                </div>
                    
                
                <div class="col-sm-1"></div>
                <div class="col-sm-4 text-center" >
                    <span style="font-size:22px;">Order ID#</span>
                    <span class="text-dark" style="font-size:22px; text-align:center;border:1px solid #353535;padding:10px; border-radius:8px;">
                        <?php echo isset($order_id) ? $order_id : ""; ?>
                    </span> <?php 
                                $rec4 = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$order_id'");
                                $row4 = mysqli_fetch_array($rec4); ?>
                    <br><br>
                    <i class=" fa fa-calendar-check " style="color:#8A1218;font-size:20px;margin-top:10px;"></i>
                    &nbsp; <span class="" style="text-align:center; margin-top:10px;"><?php echo $row["date_time"]; ?></span>
                    <div>
                        <div style="padding:20px; margin-top:10px;">
                            <h4>Customer Detail</h4>
                            <table class="table table-striped text-center" >
                                <tr>
                                    <td style="text-align:left;">Name:</td>
                                    <td><?php echo isset($row4["customer_name"]) ? $row4["customer_name"] : ""; ?></td>
                                </tr>
                                <tr>
                                    <td style="text-align:left;" >Cell#:</td>
                                    <td><?php echo isset($row4["customer_phone"]) ? $row4["customer_phone"] : ""; ?></td>
                                </tr>
                                <tr>
                                    <td style="text-align:left;" >Cashier:</td>
                                    <td>Hassan</td>
                                </tr>
                                <tr>
                                    <td  style="text-align:left;" >Payment Method:</td>
                                    <td class="text-capitalize"><?php echo isset($row4["paid_by"]) ? $row4["paid_by"] : ""; ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>
                    <center>
                        <div class="" style="width:350px;border-radius:10px;border:1px solid #ccc;box-shadow:0px 2px 0px 1px #F6EEEE; padding:10px;margin-top:20px; ">
                            <table class="table text-dark text-center" style="width:300px; padding:30px; font-weight:700; ">
                                <tr>
                                    <td>Sub - </td>
                                    <td><?php echo "Rs ".$totalCashAmount.".00"; ?></td>
                                </tr>
                                <tr>
                                    <td>Tax  - </td>
                                    <td> <?php echo  "Rs ".$row["tax_amount"].".00"; ?></td>
                                </tr>
                                <tr>
                                    <td>Total  - </td>
                                    <td><?php echo  "Rs ".$row["total_amount"].".00"; ?></td>
                                </tr>
                            </table>
                        </div>
                    </center>
                    <a href="orders_detail_v.php">
                        <button type="button" class="btn btn-dark form-control" style="border-radius:20px;margin-top:20px;">Submit</button>
                    </a>
                </div>
            </div>
        </div>
    </form>
</div>

<!-- 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>
</body>
</html>
<?php  ?>