<?php
include("db/cn.php");
ob_start();
session_start(); // Make sure the session is started

// Check if session variables are set
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 {
    // Redirect to a login page or show an error
    echo '<script type="text/javascript">window.location.href="logout.php";</script>';
    exit();
}

@$supplier_name = $_GET["emp_name"];

// Create DateTime object for the current date
$currentDate = new DateTime();

// Clone the object and modify it to get the previous month
$previousMonthDate = clone $currentDate;
$previousMonthDate->modify('first day of last month');

// Get the month and year for the previous month
$previous_month = $previousMonthDate->format('m');
$current_year = $currentDate->format('Y');

// Debugging: Echo month and year
echo "Previous Month: $previous_month, Current Year: $current_year<br>";

// Query to get total salary for the previous month in the current year
$salari_total_query = "
    SELECT 
        SUM(emp_salari) AS total_salary
    FROM e_salary
    WHERE emp_name = '$supplier_name'
    AND MONTH(date_time) = $previous_month
    AND YEAR(date_time) = $current_year
";
$salari_total = mysqli_query($connection, $salari_total_query) or die(mysqli_error($connection));

// Debugging: Check the query and result
echo "SQL Query for Total Salary: $salari_total_query<br>";

// Query to get total advances for the previous month in the current year
$salari_advances_query = "
    SELECT 
        SUM(emp_adv) AS total_advances
    FROM adv_salari
    WHERE emp_name = '$supplier_name'
    AND MONTH(date_time) = $previous_month
    AND YEAR(date_time) = $current_year
";
$salari_advances = mysqli_query($connection, $salari_advances_query) or die(mysqli_error($connection));

// Debugging: Check the query and result
echo "SQL Query for Total Advances: $salari_advances_query<br>";

// Query to get total paid for the previous month in the current year
$salari_paid_query = "
    SELECT 
        SUM(emp_paid) AS total_paid
    FROM salari_paid
    WHERE emp_name = '$supplier_name'
    AND MONTH(date_time) = $previous_month
    AND YEAR(date_time) = $current_year
";
$salari_paid = mysqli_query($connection, $salari_paid_query) or die(mysqli_error($connection));

// Debugging: Check the query and result
echo "SQL Query for Total Paid: $salari_paid_query<br>";

// Fetch the results
$total_salary = mysqli_fetch_assoc($salari_total)['total_salary'] ?? 0;
$total_advances = mysqli_fetch_assoc($salari_advances)['total_advances'] ?? 0;
$total_paid = mysqli_fetch_assoc($salari_paid)['total_paid'] ?? 0;
$remaining = $total_salary - $total_paid - $total_advances;

// Debugging: Output fetched amounts
echo "Total Salary: $total_salary, Total Advances: $total_advances, Total Paid: $total_paid, Remaining: $remaining<br>";

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>Salay 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, #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(90deg, #0A5064, #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: #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>

<body>

<?php
ob_start();
 include("common/hd.php");?>
<!-- Sidebar -->
<?php include("common/sd.php");

$salari_history = mysqli_query($connection,"
    SELECT date_time, emp_paid 
    FROM salari_paid 
    WHERE emp_name = '$supplier_name' 
    AND YEAR(date_time) = YEAR(CURRENT_DATE()) 
    ORDER BY MONTH(date_time), DAY(date_time)
");
 ?>
<div class="main-content">
    <div class="container mt-4">
        <h4 class="col-4 text-center" style="background:#0A4657; color:#fff; padding:14px; border-radius:10px;">
            Employee Name : <?php echo htmlspecialchars($supplier_name); ?>
        </h4>
        <div class="row">
            <table class="table table-bordered text-center">
                <thead>
                    <tr>
                        <th style="background:#0A4657;color:#fff;">Month</th>
                        <th style="background:#060606;color:#fff;">Total Salary</th>
                        <th style="background:#060606;color:#fff;">Total Advances</th>
                        <th style="background:#060606;color:#fff;">Total Paid</th>
                        <th style="background:#0A4657;color:#fff;">Remaining</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    // Loop through all months of the current year
                    for ($month = 1; $month <= 12; $month++) {
                        echo "<tr>";
                        echo "<td style='background:#0A4657;color:#fff;'>" . date('F', mktime(0, 0, 0, $month, 10)) . "</td>"; // Month name

                        if ($month == $previous_month) {
                            // Display values only for the previous month
                            echo "<td> Rs " . number_format($total_salary, 2) . "</td>"; // Total salary
                            echo "<td> Rs " . number_format($total_advances, 2) . "</td>"; // Total advances
                            echo "<td> Rs " . number_format($total_paid, 2) . "</td>"; // Total paid
                            echo "<td style='color:#0A4657; font-weight:600;'> Rs " . number_format($remaining, 2) . "</td>"; // Remaining amount
                        } else {
                            // Leave the cells empty for other months
                            echo "<td> - </td>";
                            echo "<td> - </td>";
                            echo "<td> - </td>";
                            echo "<td> - </td>";
                        }

                        echo "</tr>";
                    }
                    ?>
                </tbody>
            </table>
        </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>
