<?php
include("db/cn.php");
ob_start();
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'];
} else {
    // Handle the case where session variables are not set
    // Redirect to a login page or show an error
    echo '<script type="text/javascript">window.location.href="login.php";</script>';
   // header("Location: login.php");
    exit();
}

if ($_GET) {
    if (isset($_GET["id"])) {
        $supplier_id = $_GET["id"];  // Use supplier_id instead of supplier_name
        
        // Use prepared statements to prevent SQL injection
        $stmt = $connection->prepare("DELETE FROM supplier WHERE supplier_id = ? AND user_name = ? AND user_role = ? AND outlet_name = ? AND outlet_address = ?");
        $stmt->bind_param("sssss", $supplier_id, $userName, $userRole, $outlet_name, $outlet_address);  // Adjusted to bind supplier_id as integer
        
        if ($stmt->execute()) {
            echo '<script type="text/javascript">window.location.href="supplier.php";</script>';
            exit();
        } else {
            echo "Error: " . $stmt->error;
        }
        
        $stmt->close();
    }
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["submit2"])) {
    $supplier_name = $_POST["supplier_name"];
    $supplier_id = $_POST["supplier_id"];
    $supplier_company = $_POST["supplier_company"];
    $supplier_category = $_POST["supplier_category"];
    $supplier_product = $_POST["supplier_product"];
    $date_time = date("Y-m-d");
    $supplier_phone = $_POST["supplier_phone"];
    
    // Use prepared statements to prevent SQL injection
    $stmt = $connection->prepare("INSERT INTO supplier (supplier_name, supplier_company, supplier_id, supplier_product, supplier_phone, supplier_category, date_time, user_name, user_role, outlet_name, outlet_address) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("sssssssssss", $supplier_name, $supplier_company, $supplier_id, $supplier_product, $supplier_phone, $supplier_category, $date_time, $userName, $userRole, $outlet_name, $outlet_address);
    
    if ($stmt->execute()) {
        // Success, redirect or inform the user
         echo '<script type="text/javascript">window.location.href="supplier.php";</script>';
       // header("Location: supplier.php");
        exit();
    } else {
        echo "Error: " . $stmt->error;
    }

    $stmt->close();
}
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>Supplier Management</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" />
</head>

<style>
 #supplier-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;
    }
}

@media (min-width: 880px) {
    .sidebar {
        width: 100%;
        /* Change sidebar width to 100% on mobile */
        max-width: 120px; /* Set maximum width for the sidebar */
    }
}

@media (min-width: 880px) {
    .main-content {
        margin-left: ; /* Width of the sidebar */
    }
}

/* Added new class to control overlay */
.overlay {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 999;
    display: none; /* Initially hidden */
}
    /* 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: #503F2E;
    }

    /* 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(90deg, #3C2F23, #060606);
        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 */
    margin-left: 100px; /* Initially, set margin-left to 0 */
    padding-top: 30px; /* Height of fixed header */
    transition: margin-left 0.3s ease-in-out; /* Add transition for smoother animation */
}


    /* Custom styles for search bar */
    .search-container {
        display: flex;
        align-items: center;
       width:100%;
        margin: 2px auto;
        float:left;
	}

/* Updated hide class to apply to the overlay */
.sidebar.hide, .overlay.hide {
    transform: translateX(-100%);
}

    .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
ob_start();
 include("common/hd.php");?>
<!-- Sidebar -->
<?php include("common/sd.php"); ?>
<div class="main-content">
<br />
<div class="container">
<div class="row text-left">
<div class="col-sm-8" id="table-container-wrapper">
<h3 style="color:#212529;">Supplier Management </h3>

 <input type="text" id="searchInput" class="form-control" placeholder="Search...">
           
</div>

<?php
    $remain = mysqli_query($connection,"select SUM(closing_balance) AS total_remain FROM supplier_closing ");
	$fetch_remain = mysqli_fetch_array($remain);
	$remain_sum = $fetch_remain["total_remain"];
	
	?>
	<div class="col-sm-1"></div>
<div class="col-sm-3" style="background:#503F2E;color:#fff; padding:08px; border-radius:20px;"><br>
             <center><p style="font-weight:600px; font-size:18px;">Total Payable Rs <?php  echo number_format(round($remain_sum, 2), 2);  ?></p></center>

</div>
</div>

</div>

<div class="container" style="margin-top:">
      <div class="row">
        <div class="col-sm-9" style="" >
            <div class="row text-center">
                 <!-- Search container -->
                  <table class="table text-center table-hover table-sm" style="margin-top:25px;">
    <thead style="background:#212529; color: white; font-size:14px;">
        <tr>
        <th>Sr</th>
            <th>ID</th>
            <th> Name</th>
            <th>Cell</th>
            <th>Company</th>
            <th>Closing Balance</th>
            <th>Detail</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody id="supplierTableBody">
        <?php   
        // Join the supplier table with the supplier_closing table and retrieve necessary fields
        $rec = mysqli_query($connection, "
            SELECT supplier.supplier_id, supplier.supplier_name, supplier.supplier_phone, supplier.supplier_company, supplier_closing.closing_balance 
            FROM supplier 
            LEFT JOIN supplier_closing ON supplier.supplier_name = supplier_closing.supplier_name 
            
            
        ");
        $sr_no = 1;
        while($row = mysqli_fetch_array($rec)) {		   
        ?>
        <tr>
        <td><?php echo $sr_no++; ?></td>
            <td><?php echo $row["supplier_id"]; ?></td>
            <td><?php echo $row["supplier_name"]; ?></td>
            <td><?php echo $row["supplier_phone"]; ?></td>
            <td class="text-uppercase"><?php echo $row["supplier_company"]; ?></td>
            <td><?php echo  number_format(round($row["closing_balance"], 2), 2); ?></td> <!-- Display the closing_balance -->
            <td><a href="supplier_detail.php?supplier_name=<?php echo $row["supplier_name"]; ?>">
            <button class="btn btn-dark btn-sm"><i class="fas fa-clipboard-check fa-1x"></i></button></a></td>
            <td><a href="supplier_edit.php?supplier_name=<?php echo $row["supplier_name"]; ?>">
            <button class="btn btn-dark btn-sm">Edit</button></a></td>
            <td><a href="supplier.php?id=<?php echo $row["supplier_id"]; ?>"><button class="btn btn-danger btn-sm">x</button></a></td>
        </tr>
        <?php  } 
        ob_end_flush();
        ?>
    </tbody>
</table>
         

                </div>
        </div>
        <div class="col-sm-1"></div>
        <div class="col-sm-2" >      
<br />
<button type="button" class="btn btn-dark form-control" data-toggle="modal" data-target="#addPurchaseModal" style="margin-top:14px;">
              Add Supplier
            </button>
        </div>
        
        <div class="modal fade" id="addPurchaseModal" tabindex="-1" role="dialog" 
            aria-labelledby="addPurchaseModalLabel" aria-hidden="true" style="margin-top:80px;">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title text-capitalize" id="addPurchaseModalLabel">Add Supplier</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            <!-- Form for adding purchase -->
                            <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
                            <div class="form-group">
                                <label>Supplier ID</label>
                                <input type="text" name="supplier_id"  class="form-control" />
                                </div>
                           <div class="form-group">
                             <label>Supplier Name</label>
                                <input type="text" name="supplier_name"  class="form-control" />
                                </div>
                                
                                <div class="form-group">
                                <label>Supplier Company</label>
                                <input type="text" name="supplier_company"  class="form-control" />
                                </div>
                                <div class="form-group">
                                <label>Phone Number</label>
                                <input type="text" name="supplier_phone"  class="form-control" />
                                </div>
                                <div class="form-group">
                                
                                <input type="hidden" class="form-control" name="supplier_category" />
                                </div>
                               <div class="form-group">
                               
                     <input type="hidden" name="supplier_product"  class="form-control" style="height:180px;" required="required" >
                                </div>
                              
                                <button type="submit" name="submit2" class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>    
       </div>
    </div>
 
 

</div>    
</body>


<script>
document.addEventListener("DOMContentLoaded", function() {
    var searchInput = document.getElementById('searchInput');
    var rows = document.querySelectorAll("#supplierTableBody tr");

    searchInput.addEventListener("keyup", function(event) {
        var term = event.target.value.toLowerCase();
        rows.forEach(function(row) {
            var cells = row.getElementsByTagName("td");
            var found = false;
            Array.from(cells).forEach(function(cell) {
                if (cell.textContent.toLowerCase().indexOf(term) > -1) {
                    found = true;
                }
            });
            if (found) {
                row.style.display = "";
            } else {
                row.style.display = "none";
            }
        });
    });
});

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>
