<?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'];
}
@$supplier_name = $_GET["emp_name"];



//-------------------------------------
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["emp_adv"])){
$date_time32 = date("Y-m-d");
$empName = $_POST["empName"];
$emp_adv = $_POST["emp_adv1"];	
$paid_by = $_POST["paid_by"];	
$emp_desc = $_POST["emp_desc"];
$date = date("Y-m-d");
 mysqli_query($connection,"insert into adv_salari(emp_name,emp_adv,emp_desc,paid_by,date_time)
 values('".$empName."','".$emp_adv."','".$emp_desc."','".$paid_by."','".$date_time32."')");
 
  mysqli_query($connection,"insert into incash(name_detail,amount,cr_id,paid_by,login_user,user_role,outlet_name,outlet_address,date_time)
 values('".$empName."','".$emp_adv."','','$paid_by','".$userName."','".$userRole."','".$outlet_name."','".$outlet_address."','".$date."')");
}
//--------------------------------------

//-------------------------------------
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["paid_emp"])){
$dateTime= date("Y-m-d");
$emp_name = $_POST["emp_name"];
$emp_paid = $_POST["emp_paid"];
$emp_des = $_POST["emp_des"];

	

 mysqli_query($connection,"insert into salari_paid(emp_name,emp_paid,emp_des,date_time)
 values('".$emp_name."','".$emp_paid."','".$emp_des."','".$dateTime."')");
 

}
//--------------------------------------

//-------------------------------------
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["open_emp"])){
$dateTime= date("Y-m-d");
$emp_name1 = $_POST["emp_name1"];
$emp_amount = $_POST["emp_amount"];	
$amount_type = $_POST["amount_type"];

 mysqli_query($connection,"insert into opening_salari(emp_name1,emp_amount,amount_type,date_time)
 values('".$emp_name1."','".$emp_amount."','".$amount_type."','".$dateTime."')");
}
//--------------------------------------

//-------------------------------------
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["close_emp"])) {
    $dateTime = date("Y-m-d");
    $emp_name2 = $_POST["emp_name2"];
    $paid = $_POST["paid"];    
    $payable = $_POST["payable"];
    $closing = $_POST["closing"];
    $date = date("Y-m-d");

    // Check if the row exists
    $checkQuery = "SELECT * FROM salari_close WHERE emp_name = '$emp_name2'";
    $result = mysqli_query($connection, $checkQuery);

    if (mysqli_num_rows($result) > 0) {
        // Row exists, update the record
        $updateQuery = "UPDATE salari_close 
                        SET paid = '$paid', payable = '$payable', closing = '$closing', date_time = '$date' 
                        WHERE emp_name = '$emp_name2'";
        mysqli_query($connection, $updateQuery);
    } else {
        // Row doesn't exist, insert a new record
        $insertQuery = "INSERT INTO salari_close (emp_name, paid, payable, closing, date_time) 
                        VALUES ('$emp_name2', '$paid', '$payable', '$closing', '$date')";
        mysqli_query($connection, $insertQuery);
    }

    // Redirect to salari_detail.php
    echo "<script type='text/javascript'>window.location.href = 'salari_detail.php?emp_name=$emp_name2';</script>";
}
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>Employee Salary - 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: 460px; /* 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: #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, #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: linear-gradient(90deg, #503F2E, #060606);
        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"); ?>
<?php
// Fetch the opening salary details based on the employee name.
$sal = mysqli_query($connection, "SELECT * FROM opening_salari WHERE emp_name1 = '$supplier_name'");
$sal_g = mysqli_fetch_array($sal);

// Adjust emp_amount based on the amount_type.
$emp_amount = (int)$sal_g["emp_amount"];
if ($sal_g["amount_type"] === 'dr') {
    $emp_amount = -$emp_amount; // Make the amount negative for 'dr'
}

// Get the sum of employee advances (debits).
$dr = mysqli_query($connection, "SELECT SUM(emp_adv) AS emp_dr FROM adv_salari WHERE emp_name = '$supplier_name'");
$dr_get = mysqli_fetch_array($dr);

// Get the sum of employee payments (credits).
$cr = mysqli_query($connection, "SELECT SUM(emp_paid) AS emp_cr FROM salari_paid WHERE emp_name = '$supplier_name'");
$cr_get = mysqli_fetch_array($cr);
?>
<div class="main-content">

<br />
<div class="container">
<div class="row">
<div class="col-sm-9">
<h3 style="color:#212529;">Salary Detail</h3>
</div>
<div class="col-sm-3" style="padding:10px; border-radius:7px; font-size:18px; border:1px solid #503F2E; background:#503F2E; color:#fff; 
">
<center>
<h5>Employee Detail</h5>

<?php echo 'Name : '.$supplier_name; ?>
<br />

</center>
</div>

<div class="col-sm-8"></div>
<div class="col-sm-4">
<br>
<a href="e_salari.php"><button type="button" class="btn btn-dark "><i class="fa fa-arrow-left"></i></button></a>
 <button type="button" class="btn btn-dark btn-sm " data-toggle="modal" data-target="#addopenModal">
                Open
            </button>
<button type="button" class="btn btn-dark btn-sm" data-toggle="modal" data-target="#addPurchaseModal">
                 Paid
            </button>
            <button type="button" class="btn btn-dark btn-sm" data-toggle="modal" data-target="#addpaidModal">
                Payable
            </button>
           
</div>
</div>
</div>


<div class="container">
 <div class="row">
 <div class="col-sm-1"></div>
 <div class="col-sm-10">
 <!-- Bootstrap Tab Navigation -->
<div class="tab-buttons">
    <div class="tab-button active" onclick="openTab(event, 'tab1')">Overview</div>
    <div class="tab-button" onclick="openTab(event, 'tab2')">Paid</div>
    <div class="tab-button" onclick="openTab(event, 'tab3')">Payable</div>

</div>
<!-- Tab Content -->
<div class="tab-container">

    <div id="tab1" class="tab active"  >
  
 
  <h4>Overview</h4>
     
     
     <div class="container" style="padding:20px; border-radius:14px; box-shadow: 0px 0px 5px 2px #503F2E;">
     <br />
    <form method="post" action="salari_detail.php?emp_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
    <table class="table table-stripped table-hover">
        <tr>
            <td style="font-size:18px;font-weight:600;color:#A31218;">Opening</td>
            <td class="text-right" style="font-weight:600; font-size:18px; color:#A31218;">
                <?php echo ' Rs ' . $emp_amount; ?>
            </td>
        </tr>
        <tr>
            <td style="font-size:18px;font-weight:600;">Payable</td>
            <td class="text-right" style="font-weight:600;font-size:18px;">
                Rs <?php echo $cr_get["emp_cr"]; ?>
            </td>
        </tr>
        <tr>
            <td style="font-size:18px;font-weight:600;">Paid</td>
            <td class="text-right" style="font-weight:600; font-size:18px;">
                Rs <?php echo $dr_get["emp_dr"]; ?>
            </td>
        </tr>
        <tr>
            <td style="font-size:18px;font-weight:600;">Closing</td>
            <td class="text-right" style="font-weight:600;font-size:18px;">
                Rs <?php
                    // Calculate the closing balance
                    $close = $emp_amount - (int)$dr_get["emp_dr"] + (int)$cr_get["emp_cr"];
                    echo $close;
                ?>
            </td>
        </tr>
    </table>

    <input type="hidden" name="emp_name2" value="<?php echo $supplier_name; ?>">
    <input type="hidden" name="paid" value="<?php echo $dr_get["emp_dr"]; ?>">
    <input type="hidden" name="payable" value="<?php echo $cr_get["emp_cr"]; ?>">
    <input type="hidden" name="closing" value="<?php echo $close; ?>">
    <button type="submit" class="btn btn-dark" name="close_emp" style="margin-bottom:20px;">Save</button>
</form>
    </div>
</div>
    <!-- Closing Info Tab -->
    <div <div id="tab2" class="tab">
         <table class=" table table-stripped text-center ">
  <tr class="bg-dark2">
    <td>Date</td>
    <td>Description</td>
    <td>Amount</td>
    <td>Method</td>
    
  </tr>
  <?php
  // Using a UNION to simulate FULL OUTER JOIN
  $query1 = "
    select * from adv_salari where emp_name = '$supplier_name'
  ";

  $result1 = mysqli_query($connection, $query1);

  while ($row1 = mysqli_fetch_array($result1)) {
    $date_time1 = $row1["date_time"];
    $emp_desc = $row1["emp_desc"] ? $row1["emp_desc"] : '-';
    $emp_adv = $row1["emp_adv"] ? 'Rs ' . $row1["emp_adv"] : '-';
	$paid_by = $row1["paid_by"] ? '' . $row1["paid_by"] : '-';
	
  
  ?>
  <tr>
    <td><?php echo $date_time1; ?></td>
    <td><?php echo $emp_desc; ?></td>
    <td><?php echo $emp_adv; ?></td>
    <td class="text-capitalize"><?php echo $paid_by; ?></td>
   
   
      </tr>
  <?php
  }
  ?>
</table>
    </div>
    
    <div <div id="tab3" class="tab">
    <table class=" table table-stripped text-center ">
  <tr class="bg-dark2">
    <td>Date</td>
    <td>Description</td>
    <td>Amount</td>
    <td>By.</td>
    
  </tr>
  <?php
  // Using a UNION to simulate FULL OUTER JOIN
  $query = "
    select * from salari_paid where emp_name = '$supplier_name'
  ";

  $result = mysqli_query($connection, $query);

  while ($row = mysqli_fetch_array($result)) {
    $date_time = $row["date_time"];
    $emp_desc = $row["emp_des"] ? $row["emp_des"] : '-';
    $emp_adp = $row["emp_paid"] ? 'Rs ' . $row["emp_paid"] : '-';
	 $user = $userName;
  
  ?>
  <tr>
    <td><?php echo $date_time; ?></td>
    <td><?php echo $emp_desc; ?></td>
    <td><?php echo $emp_adp; ?></td>
     <td class="text-capitalize"><?php echo $user; ?></td>
   
      </tr>
  <?php
  }
  ?>
</table>
    
    
       
    </div>
</div>
</div>
<div class="col-sm-1"></div>
 </div>
</div>

<div class="modal fade" id="addopenModal" tabindex="-1" role="dialog" 
            aria-labelledby="addopenModalLabel" 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="addopenModalLabel">Add Opening Amount</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="salari_detail.php?emp_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="emp_name1" value="<?php echo $supplier_name; ?>" 
                                style="background:#503F2E; color:#fff;" class="form-control" readonly />
                                <br />
                                <input type="text" name="emp_amount" placeholder="Amount" class="form-control" />
                               
                               <br />
                                <select name="amount_type" class="form-control">
                                <option value="">Select type</option>                                
                                    <option value="dr">Recieable</option>
                                    <option value="cr">Payable</option>
                                   
                                </select>
                                <br />
                               
                                <button type="submit" name="open_emp"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </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 Salary</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="salari_detail.php?emp_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="empName" value="<?php echo $supplier_name; ?>" 
                                style="background:#503F2E; color:#fff;" class="form-control" readonly />
                                <br />
                                <input type="text" name="emp_desc" placeholder="Description" class="form-control" />
                                <br />
                                <input type="text" name="emp_adv1" placeholder="Amount" class="form-control" />
                               <br />
                                <select name="paid_by" class="form-control">
                                <option value="">Select Method</option>                                
                                    <option value="cash">Cash</option>
                                    <option value="bank1">Bank 1</option>
                                    <option value="bank2">Bank 2 </option>
                                    <option value="bank3">Bank 3</option>
                                    <option value="other">Other</option>
                                </select>
                                <br />
                               
                                <button type="submit" name="emp_adv"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade" id="addpaidModal" tabindex="-1" role="dialog" 
            aria-labelledby="addpaidModalLabel" 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="addpaidModalLabel">Add Paid</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="salari_detail.php?emp_name=<?php echo $supplier_name; ?>" enctype="multipart/form-data">
                            
                                <input type="text" name="emp_name" value="<?php echo $supplier_name; ?>" 
                                style="background:#503F2E; color:#fff;" class="form-control" readonly />
                                <br />
                                <input type="text" name="emp_paid" placeholder="Amount" class="form-control" />
                                <br />
                                 <input type="text" name="emp_des" placeholder="Description" class="form-control" />
                                <br />
                              
                               
                                <button type="submit" name="paid_emp"  class="btn btn-dark">Enter</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

</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 -->
<!-- Include Bootstrap JS (use CDN or local) -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.3/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>

</html>