<?php
include("../db/cn.php");
include("common/tx.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="logout.php";</script>';
   // header("Location: login.php");
    exit();
}


if(isset($_GET['delete_id'])){
    
    $id = $_GET['delete_id'];
    mysqli_query($connection,"DELETE FROM e_salary where emp_id = '$id'");
    echo '<script type="text/javascript">window.location.href="view_salari.php";</script>';
}

ob_end_flush();
?>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title><?php echo $tx; ?></title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
   <link href="https://fonts.googleapis.com/css2?family=Quicksand:wght@200;300;400;500;600;700&display=swap" rel="stylesheet">
    <link href="../css/sb-admin-2.css" rel="stylesheet">
    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">
    
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<style>
body {
    font-family: 'Quicksand', sans-serif;
    
}
		    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
.bg-gradient-primary{
    
	background:#0A4657;
	color:#fff;	
	
	}

.btn-primary{
	background:#0A4657;
	color:#fff;
	 border:1px #0A4657 solid;
	
	}  
.btn-primary:hover{
  
  background:#fff;
  border:1px #0A4657 solid;
	color:#0A4657;
	
	}	 
    .chart-container {
      position: relative;
      background: rgba(255, 255, 255, 0.9);
      border-radius: 10px;
      padding: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }

    canvas {
      max-width: 100%;
      height: auto;
    }
	
	.fa-download:hover{
		color:#953E39;
		}
		

  </style>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

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

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <!-- Topbar -->
                <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">

                    <!-- Sidebar Toggle (Topbar) -->
                    <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
                        <i class="fa fa-bars"></i>
                    </button>

                    <!-- Topbar Search -->
                    
  <?php include('common/log.php'); 
  
    $remain = mysqli_query($connection,"select SUM(closing) AS total_remain FROM salari_close ");
	$fetch_remain = mysqli_fetch_array($remain);
	$remain_sum = $fetch_remain["total_remain"];
  
  ?>

                    <!-- Topbar Navbar -->
                    
                </nav>
                <!-- End of Topbar -->

                <!-- Begin Page Content -->
                
                <div class="container">
<div class="row">
<div class="col-sm-8">
<h3 style="color:#212529;font-weight:700;">View Employee Ledger</h3></div>
<div class="col-sm-4" style="background:#0A4657;color:#fff; padding:7px; border-radius:7px;">

    <center><p style="font-weight:700; font-size:18px;">
     Total Salary Payable <br> Rs <?php  echo number_format(round(@$remain_sum, 2), 2);  ?></p></center>
    
</div>
 
</div>

<div class="row">
    <div class="col-sm-12">
        <input type="text" id="searchInput" class="form-control" placeholder="Search..." style="width:305px; font-weight:700;">
        <table class="table text-center table-hover text-center bg-white" style="margin-top:7px;">
    <thead style="background:#5A5C69; color: white; font-size:14px;">
        <tr>
            <th>Sr#</th>
            <th>ID</th>
            <th>Name</th>
            <th>Cell#</th>
            <th>Joining Date</th>
            <th style="background:#0A4657; color:#fff;">Closing</th> <!-- New column for closing -->
            <th>Detail</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
    </thead>
    <tbody id="supplierTableBody">
        <?php   
        $sr_no = 1;
        // Modify the query with collation to ensure case-insensitive comparison for emp_name
         $rec = mysqli_query($connection, "SELECT e_salary.id, e_salary.emp_name, e_salary.emp_phone, e_salary.j_date, e_salary.emp_id, 
                                          salari_close.closing 
                                          FROM e_salary 
                                          LEFT JOIN salari_close 
                                          ON e_salary.emp_name COLLATE utf8mb4_general_ci = salari_close.emp_name COLLATE utf8mb4_general_ci");

        // Check if the query returned rows
        if (!$rec) {
            echo "Query error: " . mysqli_error($connection); // Debugging query errors
        } else {
            // Fetch and display the data
            while($row = mysqli_fetch_array($rec)){		   
        ?>
        <tr style="font-weight:700; color:#000;">
            <td><?php echo $sr_no++; ?></td>
            <td><?php echo $row["emp_id"]; ?></td>
            <td style="font-weight:700;"><?php echo $row["emp_name"]; ?></td>
            <td class="text-uppercase"><?php echo $row["emp_phone"]; ?></td>
            <td class="text-uppercase"><?php echo $row["j_date"]; ?></td>
            <td class="text-uppercase">
                <?php echo ($row["closing"] !== null) ? $row["closing"] : 'N/A'; ?> <!-- Handle NULL closing -->
            </td>
            <td>
                <a href="salari_detail.php?emp_name=<?php echo $row['emp_name']; ?>">
                    <button class="btn btn-dark btn">
                        <i class="fas fa-clipboard-check fa-1x"></i>
                    </button>
                </a>
            </td>
            <td class="font-weight:700;">
                <a href="edit_salary.php?id=<?php echo $row["emp_id"]; ?>" class="btn btn-sm font-weight-bold btn-success">Edit</a>
            </td>
            <td class="font-weight:700;">
                <a href="view_salari.php?delete_id=<?php echo $row["emp_id"]; ?>" class="btn btn-sm font-weight-bold btn-danger">X</a>
            </td>
        </tr>
        <?php  
            } 
        }
        ?>
    </tbody>
</table>
    </div>
    
</div>

</div>
</div>
            <br><br>
            <!-- End of Main Content -->

            <!-- Footer -->
            <?php include("common/main_ft.php");  ?>
            <!-- End of Footer -->

        </div>
   </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <!-- Logout Modal-->
    <div class="modal fade" id="logoutModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
        aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Ready to Leave?</h5>
                    <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">Select "Logout" below if you are ready to end your current session.</div>
                <div class="modal-footer">
                    <button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
                    <a class="btn btn-primary" href="">Logout</a>
                </div>
            </div>
        </div>
    </div>

     <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";
                    }
                });
            });
        });
     </script> 
   <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.10.2/dist/umd/popper.min.js"></script>
    <!-- Bootstrap core JavaScript-->
    <script src="../vendor/jquery/jquery.min.js"></script>
    <script src="../vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="../vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="../js/sb-admin-2.min.js"></script>

</body>

</html>