<?php

include '../assets/constant/config.php';

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
$y = date('Y');

?>

<script>
    function editForm(event, id, file) {
        event.preventDefault(); // Prevent the default link behavior

        // Create a form dynamically
        var form = document.createElement('form');
        form.action = file;
        form.method = 'post';

        // Create a hidden input field for the ID
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'id';
        input.value = id;

        // Append the input field to the form
        form.appendChild(input);

        // Append the form to the body and submit it
        document.body.appendChild(form);
        form.submit();
    }
</script>

<script>
        window.addEventListener('load', function() {
            document.getElementById('preloader').style.display = 'none';
        });
    </script>

<script>
    function delForm1(event, id, file) {
        event.preventDefault(); // Prevent the default link behavior

        // Create a form dynamically
        var form = document.createElement('form');
        form.action = file;
        form.method = 'post';

        // Create a hidden input field for the ID
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = 'del_id';
        input.value = id;

        // Append the input field to the form
        form.appendChild(input);

        // Append the form to the body and submit it
        document.body.appendChild(form);
        form.submit();
    }
</script>

<footer class="footer">
    Copyright <?php echo $y; ?>. All Rights Reserved by Mayuri K. Freelancer
</footer>

</div>
<!-- End Right content here -->

</div>
<!-- END wrapper -->


<!-- jQuery  -->
<script src="../assets/js/jquery.min.js"></script>
<script src="../assets/js/jquery.js"></script>

<!-- validation script -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.3/jquery.validate.min.js"></script>


<!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
<!--<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>-->
<script src="../assets/js/popper.min.js"></script>
<script src="../assets/js/bootstrap.min.js"></script>
<script src="../assets/js/modernizr.min.js"></script>
<script src="../assets/js/detect.js"></script>
<script src="../assets/js/fastclick.js"></script>
<script src="../assets/js/jquery.blockUI.js"></script>
<script src="../assets/js/waves.js"></script>
<script src="../assets/js/jquery.nicescroll.js"></script>


<!-- <script src="../assets/js/space.js"></script> -->


<script src="../assets/plugins/skycons/skycons.min.js"></script>
<!-- <script src="../assets/plugins/fullcalendar/vanillaCalendar.js"></script>  -->

<script src="../assets/plugins/raphael/raphael-min.js"></script>
<script src="../assets/plugins/morris/morris.min.js"></script>

<!-- <script src="../assets/pages/dashborad.js"></script> -->
<!-- Required datatable js -->
<script src="../assets/plugins/datatables/jquery.dataTables.min.js"></script>
<script src="../assets/plugins/datatables/dataTables.bootstrap4.min.js"></script>
<!-- Buttons examples -->
<script src="../assets/plugins/datatables/dataTables.buttons.min.js"></script>
<script src="../assets/plugins/datatables/buttons.bootstrap4.min.js"></script>
<script src="../assets/plugins/datatables/jszip.min.js"></script>
<script src="../assets/plugins/datatables/pdfmake.min.js"></script>
<script src="../assets/plugins/datatables/vfs_fonts.js"></script>
<script src="../assets/plugins/datatables/buttons.html5.min.js"></script>
<script src="../assets/plugins/datatables/buttons.print.min.js"></script>
<script src="../assets/plugins/datatables/buttons.colVis.min.js"></script>
<!-- Responsive examples -->
<script src="../assets/plugins/datatables/dataTables.responsive.min.js"></script>
<script src="../assets/plugins/datatables/responsive.bootstrap4.min.js"></script>

<!-- Datatable init js -->
<script src="../assets/pages/datatables.init.js"></script>
<!-- App js -->
<script src="../assets/js/app.js"></script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<!---main js----->
   
<script src="../assets/js/security.js"></script>


<!--fether ocons js-->
<script src="https://unpkg.com/feather-icons@4.29.1/dist/feather.min.js"></script>
<script>
    feather.replace();
</script>
<!--<script src="../assets/pages/form-advanced.js"></script>-->
<!--------Ck Editot ------>
 <script src="https://cdn.ckeditor.com/ckeditor5/38.1.1/classic/ckeditor.js"></script>
<script>
        ClassicEditor
            .create(document.querySelector('#ckeditor'))
            .catch(error => {
                console.error(error);
            });
    </script>



<link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>



<script>
    // In your Javascript (external .js resource or <script> tag)
    $(document).ready(function() {
        $('.select2').select2();
    });
</script>

<!----------sweetalert---------------------->
 <script language="javascript">
    var today = new Date();
    document.getElementById('time').innerHTML = today;
    
    $(document).ready(function() {
        setTimeout(function() {
            $("#myAlert").alert('close');
        }, 5000); // 1 second
    });
</script>
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert-dev.js"></script> -->


<script type="text/javascript">
    function googleTranslateElementInit() {
        new google.translate.TranslateElement({
            pageLanguage: 'en'
        }, 'google_translate_element');

        var $googleDiv = $("#google_translate_element .skiptranslate");
        var $googleDivChild = $("#google_translate_element .skiptranslate div");
        $googleDivChild.next().remove();

        $googleDiv.contents().filter(function() {
            return this.nodeType === 3 && $.trim(this.nodeValue) !== '';
        }).remove();

    }
</script>
<style>
    .goog-te-gadget .goog-te-combo {
        margin: 0px 0;
        padding: 8px;
        color: #000;
        background: #eeee;
    }
</style>

<script type="text/javascript">
function generatePayslip(id) {
    // Make an AJAX request to fetch payslip data
    $.ajax({
        url: '../app/payslip.php', // Replace 'fetch_payslip.php' with your server-side script to fetch payslip data
        method: 'GET',
        data: { id: id },
        success: function(response) {
            // Call function to construct and display the payslip
            displayPayslip(response);
        },
        error: function(xhr, status, error) {
            console.error('Error fetching payslip:', error);
            // Handle error, show error message, etc.
        }
    });
}

</script>
<script>
function displayPayslip(payslipData) {
    // Construct HTML for the payslip
    var payslipHTML = '<div class="payslip">';
    payslipHTML += '<h2>Payslip</h2>';
    payslipHTML += '<p>Date: ' + payslipData.date + '</p>';
    payslipHTML += '<p>Employee: ' + payslipData.employee + '</p>';
    payslipHTML += '<p>Gross Amount: ' + payslipData.grossAmount + '</p>';
    payslipHTML += '<p>Deduction: ' + payslipData.deduction + '</p>';
    payslipHTML += '<p>Net Amount: ' + payslipData.netAmount + '</p>';
    // Add more data as needed

    // Display the payslip HTML
    $('#payslipContainer').html(payslipHTML);
}</script>










<script>
    function delForm(event, id) {
        event.preventDefault(); // Prevent the default action (i.e., following the link)
        
        // Send an AJAX request to delete the item
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'app/email_crud.php?id=' + id, true);
        xhr.onload = function () {
            if (xhr.status === 200) {
                // Successful deletion, you may want to handle this appropriately
                window.location.href = '../manage_mail.php';
            } else {
                // Error handling
                console.log('Error:', xhr.statusText);
            }
        };
        xhr.send();
        
        return false; // Prevent any further action
    }
</script>

 <script type="text/javascript">
    $(document).ready(function() {
        $(".add-more").click(function(e) {
            e.preventDefault();
            var html = $(".copy").html();
            $(".mydiv").append(html);
        });

        $("body").on("click", ".remove", function(e) {
            e.preventDefault();
            $(this).parents(".subdiv").remove();
        });
    });
</script> 


<!--add row and delete row-->
    <script>
    $(document).ready(function () {
        // Add Row
        $("#dataTable").on("click", ".addRow", function () {
            var markup = "<tr><td><input type='date' name='valuation_date[]' class='form-control'></td><td><input type='text' name='value_amount[]' class='form-control'></td><td><button class='btn btn-danger removeRow' type='button'>Remove</button></td></tr>";
            $("table tbody").append(markup);
        });

        // Remove Row
        $("#dataTable").on("click", ".removeRow", function () {
            $(this).closest('tr').remove();
        });
    });
</script>


<!--- add or remove for admin in payroll-->
<script>
    $(document).ready(function () {
        // Add Row
        $(".add-more").click(function () {
            var html = $(".copy").html();
            $(".after-add-more").after(html);
        });

        // Remove Row
        $("body").on("click", ".remove", function () {
            $(this).parents(".control-group").remove();
        });
    });
</script> 


<!--payroll in extra time and late in time-->
<!--calculate payroll in -->
    

<script>
$(document).ready(function() {
    function calculatePay() {
        var totalPay = 0;
        var totalDeduction = 0;

        $('input[name="extra_time[]"]').each(function() {
            var value = parseFloat($(this).val());
            if (!isNaN(value)) {
                totalPay += value;
            }
        });

        $('input[name="Timein_late[]"]').each(function() {
            var value = parseFloat($(this).val());
            if (!isNaN(value)) {
                totalDeduction += value;
            }
        });

        var netPay = totalPay - totalDeduction;

        $('input[name="total_pay"]').val(totalPay.toFixed(2));
        $('input[name="total_deduction"]').val(totalDeduction.toFixed(2));
        $('input[name="net_pay"]').val(netPay.toFixed(2));
        $('input[name="paid_amount"]').val(netPay.toFixed(2));
    }

    function addRow() {
        var html = '<tr><td><label class="control-label">Extra Time</label><input type="number" name="extra_time[]" class="form-control" step="0.01"></td><td><label class="control-label">Time in Late</label><input type="number" name="Timein_late[]" class="form-control" step="0.01"></td><td><button class="btn btn-danger removeRow" type="button">Remove</button></td></tr>';
        $("#rows").append(html);
        calculatePay();
    }

    function removeRow() {
        $(this).closest('tr').remove();
        calculatePay();
    }

    $(document).on('click', '.addRow', addRow);
    $(document).on('click', '.removeRow', removeRow);
    $(document).on('input', 'input[name="extra_time[]"], input[name="Timein_late[]"]', calculatePay);

    calculatePay();
});
</script>




<!-- Add JavaScript to handle the change event and set employee name and payroll date -->
<script>
   $(document).ready(function() {
    $('select[name="user_name"]').change(function() {
        var selectedUser = $(this).val().trim(); // Get the selected user name
        if (selectedUser !== '') {
            // Set the selected user name as the employee name
            $('input[name="employee_name"]').val(selectedUser);

            // Get the current date in the format YYYY-MM-DD
            var today = new Date();
            var dd = String(today.getDate()).padStart(2, '0');
            var mm = String(today.getMonth() + 1).padStart(2, '0');
            var yyyy = today.getFullYear();
            var currentDate = yyyy + '-' + mm + '-' + dd;

            // Set the current date as the payroll date
            $('input[name="payroll_date"]').val(currentDate);
        }
    });
});
</script>


<!--recurring --->
 <script> 
    $(document).ready(function() {
        $('#recurringSelect').change(function() {
            if ($(this).val() === "0") {
                $('#recurringDetails').show();
            } else {
                $('#recurringDetails').hide();
            }
        });
    });
</script>

<!--Reset button-->
<script>
function resetForm() {
  document.getElementById("add_service_form").reset();
}
</script>

<!-----------Razor payment Online------------>
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>  



<!-- Bootstrap Toasts -->
<div aria-live="polite" aria-atomic="true" style="position: relative; z-index: 9999;">
    <div id="toast-container" class="w-25" style="position: fixed; top: 20px; right: 20px;">
        <!-- Toast markup will be appended here -->
    </div>
</div>

<script>
    function showToast(message, type) {
        var bgColor;
        switch (type) {
            case 'success':
                bgColor = 'bg-success';
                break;
            case 'info':
                bgColor = 'bg-info';
                break;
            case 'danger':
                bgColor = 'bg-danger';
                break;
            default:
                bgColor = 'bg-secondary';
        }

        var toastHTML = `
            <div class="toast ${bgColor}" role="alert" aria-live="assertive" aria-atomic="true" data-delay="5000">
                <div class="toast-header">
                    <strong class="mr-auto">Notification</strong>
                    <small class="text-muted">Just now</small>
                    <button type="button" class="ml-2 mb-1 close" data-dismiss="toast" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="toast-body">
                    ${message}
                </div>
            </div>
        `;
        document.getElementById('toast-container').insertAdjacentHTML('beforeend', toastHTML);
        $('.toast').toast('show');
    }

    // Execute toast notifications based on PHP session variables

 // Execute toast notifications based on PHP session variables
    <?php if (!empty($_SESSION['login_success'])) { ?>
        showToast('<?php echo $_SESSION['login_success']; ?>', 'success');
        <?php unset($_SESSION['login_success']); ?>
    <?php } ?>

    

    <?php if (!empty($_SESSION['success'])) { ?>
        showToast('Data Added Successfully', 'success');
        <?php $_SESSION['success'] = ''; ?>
    <?php } ?>

    <?php if (!empty($_SESSION['update'])) { ?>
        showToast('Record Updated', 'info');
        <?php $_SESSION['update'] = ''; ?>
    <?php } ?>

    <?php if (!empty($_SESSION['delete'])) { ?>
        showToast('Record Deleted', 'danger');
        <?php $_SESSION['delete'] = ''; ?>
    <?php } ?>
</script>

<!-----Set timeout for logout--->
<!-- <script >
   
        // Set the timeout duration (in milliseconds, e.g., 30 minutes = 1800000 ms)
        const TIMEOUT_DURATION =1800000; // 1800000; // 30 minutes  60,000ms = 1 min

        let timeout;

        // Function to reset the timer
        function resetTimer() {
            clearTimeout(timeout);
            timeout = setTimeout(logout, TIMEOUT_DURATION);
        }

        // Function to handle logout
        function logout() {
            window.location.href = '../index.php'; // Redirect to the logout page
        }

        // Set up event listeners to detect user activity
        document.addEventListener('click', resetTimer);
        document.addEventListener('mousemove', resetTimer);
        document.addEventListener('keypress', resetTimer);
        document.addEventListener('scroll', resetTimer);

        // Initialize the timer on page load
        window.onload = resetTimer;
    
</script> -->


<!-----print----->
<!-- <script>
function printContent() {
    // Hide the form and any other content you don't want to print
    document.getElementById('add_service').style.display = 'none'; // Assuming 'add_service' is the ID of your form
    // Print the printable content
    window.print();
    // Show the form again after printing
    document.getElementById('add_service').style.display = 'block';
}
</script> -->









</body>

</html>