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

ob_start();
//session_start(); // Ensure session is started
if (isset($_SESSION['user_name'], $_SESSION['user_role'], $_SESSION['outlet_name'], $_SESSION['outlet_address'])) {
    $userName = $_SESSION['user_name'];
    $userRole = $_SESSION['user_role'];
    $outlet_address = $_SESSION['outlet_address'];
    $outlet_name = $_SESSION['outlet_name'];
}

$id = $_GET["id"];

$rec = mysqli_query($connection, "SELECT * FROM products WHERE id = '$id'");
$row = mysqli_fetch_array($rec);

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit2"])) {
    $id = $_POST["id"];
    $product_id = $_POST["product_id"];
    $product_name = $_POST["product_name"];
    $company = $_POST["company"];
    @$length = $_POST["length"];
    @$width = $_POST["width"];
    @$length_feet = $_POST["lengthfeet"];
    @$width_feet = $_POST["widthfeet"];
    @$square_feet = $_POST["sqarea"];
    @$quantity = $_POST["quantity"];
    @$total_quantity = $_POST["total_quantity"];
    @$retail_price = $_POST["retail_price"];
    @$percentage = $_POST["percentage"];
    $trade_price = $_POST["trade_price"];
    $total_amount = $_POST["total_amount"];
    $supplier = $_POST["supplier"];
    @$rack_no = $_POST["rack_no"];
    @$batch_no = $_POST["batch_no"];
    $date_time = date("Y-m-d");
    $login_user = $userName; // Use session value
    $user_role = $userRole; // Use session value
    $outlet_name = $outlet_name; // Use session value
    $outlet_address = $outlet_address; // Use session value
    $product_unit = $_POST["product_unit"];

    // Update query
    $query = "UPDATE products SET 
        product_id = '$product_id', 
        product_name = '$product_name', 
        company = '$company',  
        quantity = '$quantity', 
        retail_price = '$retail_price', 
        percentage = '$percentage', 
        trade_price = '$trade_price', 
        product_unit = '$product_unit',
        supplier = '$supplier', 
        date_time = '$date_time', 
        login_user = '$login_user', 
        user_role = '$user_role', 
        outlet_name = '$outlet_name', 
        outlet_address = '$outlet_address' 
    WHERE id = '$id'";

    if (mysqli_query($connection, $query)) {
        // Purchase updated successfully
        echo '<script type="text/javascript">window.location.href="addproduct.php";</script>';
       // header("location:addproduct.php");
        exit();
    } else {
        echo "Error updating product: " . mysqli_error($connection);
    }
}

ob_end_flush();
?>

<!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>New Purchase</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 */
}
/* Custom styles for sidebar */   /* Custom styles for sidebar */
    .sidebar {
        width: 100px;
        background: linear-gradient(360deg, #060606, #3C2F23);
        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: #1A5319;
    }
    /* 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, #3C2F23);
        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: 20px;
      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:#503F2E;
			 color:#fff;
			 border: 1px solid #503F2E;
                 	}
		.btn-dark:hover{
			background:#fff;
			 color:#503F2E;
			 border:1px solid #503F2E;
			}	
			.bg-dark2{
			background:#503F2E;
			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>
<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</h3>
            </div>
            <div class="col-sm-3">
                <a href="addproduct.php"><button type="button" class="btn btn-dark" style="float:left;"><i class="fa fa-arrow-left"></i></button></a>
            </div>
        </div>
        <div class="row">
         <div class="col-sm-2"></div>
         <div class="col-sm-8">
         
         </div>
         <div class="col-sm-2"></div>
         </div>
        
    </div><br /><br /><br />
    <div class="container">
        <div class="row">
            <div class="col-sm-1"></div>
            <div class="col-sm-5">
                <form method="post" action="product_update.php?id=<?php echo $id; ?>" enctype="multipart/form-data">
                    <div class="form-group">
                        <label>Product ID</label>
                        <input type="text" name="product_id" class="form-control" value="<?php echo $row["product_id"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Product Name</label>
                        <input type="text" name="product_name" class="form-control" value="<?php echo $row["product_name"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Company</label>
                        <input type="text" name="company" class="form-control" value="<?php echo $row["company"]; ?>" />
                    </div>
                 <!--   
                    <div class="row">
                        <div class="col-sm-6 form-group">
                            <label>Length in Inch</label>
                            <input type="text" name="length" class="form-control" placeholder="Length" value="<?php echo $row["length"]; ?>" />
                        </div>
                        <div class="form-group col-sm-6">
                            <label>Width in Inch</label>
                            <input type="text" name="width" class="form-control" placeholder="Width"  value="<?php echo $row["width"]; ?>"/>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-6 form-group">
                            <label>Length in Feets</label>
                            <input type="text" name="lengthfeet" class="form-control" placeholder="Length" value="<?php echo $row["length_feet"]; ?>" />
                        </div>
                        <div class="form-group col-sm-6">
                            <label>Width in Feets </label>
                            <input type="text" name="widthfeet" class="form-control" placeholder="Width" value="<?php echo $row["width_feet"]; ?>" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label>Square / Feet Area</label>
                        <input type="text" name="sqarea" class="form-control" readonly  value="<?php echo $row["square_feet"]; ?>"/>
                    </div>-->
                    <div class="form-group">
                        <label>Quantity</label>
                        <input type="text" name="quantity" class="form-control" value="<?php echo $row["quantity"]; ?>" />
                    </div>
                    <!--<div class="form-group">
                        <label>Total Quantity in Square Feet</label>
                        <input type="text" name="total_quantity" id="total_quantity" class="form-control" readonly value="<?php echo $row["total_quantity"]; ?>" />
                    </div>-->
                        <div class="form-group">
                        <label>Retail Price Per Litre</label>
                        <input type="text" name="retail_price" id="retail_price" class="form-control" value="<?php echo $row["retail_price"]; ?>" />
                    </div>
                   
                </div>
                <div class="col-sm-5">
                 <div class="form-group">
                        <label>% Apply</label>
                        <input type="text" name="percentage" id="percentage" class="form-control" readonly value="<?php echo $row["percentage"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Trade Price Per Litre</label>
                        <input type="text" name="trade_price" id="trade_price" class="form-control" value="<?php echo $row["trade_price"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Total Amount</label>
                        <input type="text" name="total_amount" id="total_amount" class="form-control" readonly value="<?php echo $row["total_amount"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Supplier</label>
                        <input type="text" name="supplier" class="form-control" value="<?php echo $row["supplier"]; ?>" />
                   <!-- </div>  
                    <div class="form-group">
                        <label>Rack No</label>
                        <input type="text" name="rack_no" class="form-control" value="<?php echo $row["rack_no"]; ?>" />
                    </div>
                    <div class="form-group">
                        <label>Batch No</label>
                        <input type="text" name="batch_no" class="form-control" value="<?php echo $row["batch_no"]; ?>" />
                    </div>-->
                    <div class="form-group">
                        <label>Product Unit</label>
                        <select name="product_unit" class="form-control" value="<?php echo $row["product_unit"]; ?>">
                            <option value="">Select</option>
                            <option value="pcs">Pcs</option>
                            
                        </select>
                        
                    </div>
                    <hr />
                    <input type="hidden" name="id" value="<?php echo $id; ?>" />
                    <input type="hidden" name="login_user" value="<?php echo $userName; ?>" />
                    <input type="hidden" name="user_role" value="<?php echo $userRole; ?>" />
                    <input type="hidden" name="outlet_name" value="<?php echo $outlet_name; ?>" />
                    <input type="hidden" name="outlet_address" value="<?php echo $outlet_address; ?>" />
                    <button class="btn btn-dark form-control" name="submit2" type="submit">Update Product</button>
                </form>
            </div>
            <div class="col-sm-1"></div>
        </div>
    </div>
</div>               
</body>
<script>
       function calculateTotalAmount() {
        let quantity = parseFloat(document.querySelector('input[name="quantity"]').value) || 0;
        let trade_price = parseFloat(document.getElementById("trade_price").value) || 0;

        if (quantity > 0 && trade_price > 0) {
            let total_amount = (quantity * trade_price).toFixed(2);
            document.getElementById("total_amount").value = total_amount;
        } else {
            document.getElementById("total_amount").value = '';
        }
    }

    function calculateTradePercentage() {
        let retail_price = parseFloat(document.getElementById("retail_price").value) || 0;
        let trade_price = parseFloat(document.getElementById("trade_price").value) || 0;

        if (retail_price > 0 && trade_price > 0) {
            let percentage = ((retail_price - trade_price) * 100 / retail_price).toFixed(2);
            document.getElementById("percentage").value = percentage;
        } else {
            document.getElementById("percentage").value = '';
        }
    }

    document.addEventListener('DOMContentLoaded', (event) => {
        document.querySelector('input[name="quantity"]').addEventListener('input', calculateTotalAmount);
        document.getElementById("retail_price").addEventListener('input', calculateTradePercentage);
        document.getElementById("trade_price").addEventListener('input', () => {
            calculateTradePercentage();
            calculateTotalAmount();
        });
    });

    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();
        });

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

        function checkScreenSize() {
            if (window.innerWidth > 768) {
                sidebar.classList.remove("hide");
                overlay.classList.remove("hide");
            } else {
                sidebar.classList.add("hide");
                overlay.classList.add("hide");
            }
        }

        checkScreenSize();
        window.addEventListener("resize", checkScreenSize);
    });

    function toggleSubMenu(element) {
        $(element).next('.sub-menu').slideToggle();
    }
</script>
<!-- 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>
</html>
