<?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'];
}
//$cr_id = $_GET["cr_id"];
@$customer_name = $_GET["cr_name"];
//echo $cr_nickname;
$rec =  mysqli_query($connection,"select * from credit_note where cr_name = '$customer_name'  ");
$row = mysqli_fetch_array($rec);


if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["edit_credits"])) {
    
    // Capture input data and validate/sanitize them
    $cr_name = mysqli_real_escape_string($connection, $_POST["cr_name1"]);
    $cr_nickname = mysqli_real_escape_string($connection, $_POST["cr_nickname"]);
    $cr_id = mysqli_real_escape_string($connection, $_POST["cr_id"]);
    $phone_number = mysqli_real_escape_string($connection, $_POST["phone_number"]);
    $cr_address = mysqli_real_escape_string($connection, $_POST["cr_address"]);
    
    // Check for $customer_name definition
    if (!isset($customer_name)) {
        die('Customer name is not defined');
    }

    // Prepare the query to prevent SQL injection
    $stmt = $connection->prepare("UPDATE credit_note 
                                  SET cr_name = ?, cr_nickname = ?, cr_address = ?, phone_number = ?, cr_id = ?
                                  WHERE cr_name = ?");
    
    // Bind parameters
    $stmt->bind_param("ssssss", $cr_name, $cr_nickname, $cr_address, $phone_number, $cr_id, $customer_name);
    
    // Execute the query          
    if ($stmt->execute()) {
        echo "<script type='text/javascript'>window.location.href = 'credits.php';</script>";
    } else {
        echo "Error updating record: " . $stmt->error;
    }
    
    // Close the statement
    $stmt->close();
}
?>

<!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>Edit Creditor - Detail</title>
    <!-- Latest compiled and minified CSS -->
<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>
 #table-container-wrapper {
    max-height: 400px; /* 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, #382C21);
        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, #382C21);
        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:#382C21;
			 color:#fff;
			 border: 1px solid #382C21;
                 	}
		.btn-dark:hover{
			background:#fff;
			 color:#382C21;
			 border:1px solid #382C21;
			}	
			.bg-dark2{
			background:#382C21;
			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);
            }
        }
		
		/* Basic styling */
    .tab {
        display: none;
        padding: 20px;
    }
    .active {
        display: block;
    }
    .tab-buttons {
        display: flex;
    }
    .tab-button {
        cursor: pointer;
        padding: 10px 10px;
        background-color: #ffff;
        border: 1px solid #0A5064;
        border-radius: 5px 5px 0 0;
        margin-right: 5px;
		color:#212529;
    }
    .tab-button.active {
        background-color: #212529;
		color:#fff;
    }

    /* Background colors for tab content */
    #tab1 {
        background-color: #fff;
		color:#212529;
		padding:20px;
    }
    #tab2 {
        color:#212529;
		background-color: ;
    }
    #tab3 {
		color:#212529;
        background-color: ;
    }
</style>

<body>

<?php include("common/hd.php");?>
<!-- Sidebar -->
<?php include("common/sd.php"); ?>
<div class="main-content">
<br />
<div class="container">
<div class="row">
<div class="col-sm-9">
<h3 style="color:#212529;">Edit Receivable Detail
<span style="padding:10px; border-radius:7px; text-transform:capitalize; 
color:#fff;font-size:18px; border:1px solid #0A5064;background:#212529;"><?php  echo $row["cr_name"]; ?></span></h3>
</div>
<div class="col-sm-3">
<a href="credits.php"><button type="button" class="btn btn-dark"><i class="fa fa-arrow-left"></i></button></a>

</div>
</div>
<br />
<div class="container">
    <form action="credits_edit.php?cr_name=<?php echo $customer_name; ?>" method="post">
    <div class="row">
        
        <div class="col-sm-1"></div>
        <div class="col-sm-5">
            <div class="form-group">
            <label>Creditor ID:</label>
            <input type="text" name="cr_id" class="form-control" value="<?php echo $row["cr_id"]; ?>">
            </div>
           
            <div class="form-group">
                <label>Creditor Name:</label>
            <input type="text" name="cr_name1" class="form-control" style="background:#382C21;color:#fff;" value="<?php echo $row["cr_name"]; ?>" readonly>
            </div>
            
            <div class="form-group">
                <label>Creditor Nick Name:</label>
            <input type="text" name="cr_nickname" class="form-control" value="<?php echo $row["cr_nickname"]; ?>">
            
            </div>
        </div>
        <div class="col-sm-5">
            <div class="form-group">
                <label>Creditor Address:</label>
            <input type="text" name="cr_address" class="form-control" value="<?php echo $row["cr_address"]; ?>">
            </div>
            
            <div class="form-group">
                <label>Phone Number:</label>
            <input type="text" name="phone_number" class="form-control" value="<?php echo $row["phone_number"]; ?>">
            </div>
            <br>
            <button type="submit" class="btn btn-dark btn-sm form-control" name="edit_credits">Update Receiveable</button>
        </div>
        <div class="col-sm-1"></div>
        
    </div>
    </form>
</div>
</div>    
</body>

<script>
 // Function to switch between tabs
    function openTab(evt, tabName) {
        // Get all elements with class="tab" and hide them
        var tabs = document.getElementsByClassName("tab");
        for (var i = 0; i < tabs.length; i++) {
            tabs[i].style.display = "none";
        }

        // Get all elements with class="tab-button" and remove the class "active"
        var tabButtons = document.getElementsByClassName("tab-button");
        for (var i = 0; i < tabButtons.length; i++) {
            tabButtons[i].classList.remove("active");
        }

        // Show the current tab, and add an "active" class to the button that opened the tab
        document.getElementById(tabName).style.display = "block";
        evt.currentTarget.classList.add("active");
    }
  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);
});
 
	</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>