<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Admin : View Leads</title>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.11.4/css/jquery.dataTables.min.css">
    <?php include("headerlink.php") ?>
</head>

<body class="hold-transition sidebar-mini layout-fixed">
    <div class="wrapper">

        <div class="preloader flex-column justify-content-center align-items-center">
            <img class="animation__shake" src="dist/img/AdminLTELogo.png" alt="AdminLTELogo" height="60" width="60">
        </div>
        <?php include("navbar.php") ?>

        <?php include("sidebar.php") ?>

        <div class="content-wrapper">

            <div class="content-header">
                <div class="container-fluid">
                    <div class="row mb-2">
                        <div class="col-sm-6">
                            <h1 class="m-0 fw-bold text-uppercase ">Payroll</h1>
                        </div>
                        <div class="col-sm-6">
                            <ol class="breadcrumb float-sm-right">
                                <li class="breadcrumb-item"><a href="#">Home</a></li>
                                <li class="breadcrumb-item active">Payroll</li>
                                <li class="breadcrumb-item active">View All</li>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>

            <section class="content">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-12">
                            <div class="card shadow">
                                <div class="card-header" style="background: #343a40; ">
                                    <div class="row">
                                        <div class="col-10">
                                            <h3 class="card-title text-white text-bold text-uppercase pt-1">All Payroll
                                            </h3>
                                        </div>
                                        <div class="col-2 text-end">
                                            <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#modal-success">
                                                <i class="fa fa-plus-circle" aria-hidden="true"></i>
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <div class="card-body">
                                    <div id="example2_wrapper" class="dataTables_wrapper dt-bootstrap4">
                                        <div class="row">
                                            <div class="col-sm-12 col-md-6"></div>
                                            <div class="col-sm-12 col-md-6"></div>
                                        </div>
                                        <div class="row">
                                            <div class="col-sm-12">
                                                <table class="table-responsive">
                                                <table id="example2"
                                                        class="table text-center table-bordered table-hover dataTable dtr-inline display"
                                                        aria-describedby="example2_info">
                                                        <thead>
                                                            <tr>
                                                                <th>Sr No.</th>
                                                                <th>Name</th>
                                                                <th>To be Paid</th>
                                                                <th>Salary Date</th>
                                                                <th>Status</th>
                                                                <th>Action</th>

                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <tr>

                                                                <?php
                                                                include('connection.php');
                                                                $query = "SELECT * FROM hr_payroll";
                                                                $result = mysqli_query($conn, $query);
                                                                while ($row = mysqli_fetch_assoc($result)) {
                                                                ?>
                                                                    <td><?php echo $row['id'] ?></td>
                                                        
                                                                    <td>
                                                                        <a type="button" style="width:100px" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#exampleModal<?php echo $row['id']; ?>">
                                                                            <?php echo $row['name'] ?>
                                                                        </a>
                                                                    </td>
                                                                    <!-- Modal -->

                                                                    <td><?php echo $row['to_be_paid'] ?></td>
                                                                    <td><?php echo $row['salary_date'] ?></td>
                                                                    <td><?php echo $row['status'] ?></td>
                                                                    <td>
                                                                        <a href="delete_payroll.php?delete=<?php echo $row['id']?>" class="btn btn-danger btn-sm" ><i class="fa fa-trash" aria-hidden="true"></i></a>
                                                                        <!-- <a class="btn btn-success btn-sm"><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a> -->
                                                                        <a type="button" class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#editPayroll<?php echo $row['id'] ?>">
                                                                            Update
                                                                        </a>
                                                                    </td>
                                                            </tr>
                                                            <!-- edit model -->
                           
                                                            <div class="modal fade" id="editPayroll<?php echo $row['id'] ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                                                <div class="modal-dialog">
                                                                    <div class="modal-content">
                                                                        <div class="modal-header bg-dark">
                                                                            <h5 class="modal-title" id="exampleModalLabel">Name : <?php echo $row['name'] ?></h5>
                                                                            <button type="button" class="btn-close bg-white rounded-circle shadow" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                        </div>
                                                                        <div class="modal-body">
                                                                            <form action="update_payroll_code.php" method="POST">
                                                                                <div class="row">
                                                                                    <input type="hidden" id="id" name="id" value="<?php echo $row['id'] ?>">
                                                                                    <div class="col-6">
                                                                                        <label for="">Name</label>
                                                                                        <input type="text" name='name' class="form-control" value="<?php echo $row['name'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Total Salary</label>
                                                                                        <input type="text" class="form-control" name="total_salary" value="<?php echo $row['total_salary'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-6">
                                                                                        <label for="">To be Paid</label>
                                                                                        <input type="text" class="form-control" name="to_be_paid" value="<?php echo $row['to_be_paid'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Salary Date</label>
                                                                                        <input type="date" class="form-control" name="salary_date" value="<?php echo $row['salary_date'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-6">
                                                                                        <label for="">Joining Date</label>
                                                                                        <input type="text" class="form-control" name="joining_date" value="<?php echo $row['joining_date'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Status</label>
                                                                                        <input type="text" class="form-control" name="status" value="<?php echo $row['status'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-6">
                                                                                        <label for="">Leaves</label>
                                                                                        <input type="text" class="form-control" name="leaves" value="<?php echo $row['leaves'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Employee Id</label>
                                                                                        <input type="text" class="form-control" name="employee_id" value="<?php echo $row['employee_id'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-6">
                                                                                        <label for="">Per Day Salary</label>
                                                                                        <input type="text" class="form-control" name="per_day_salary" value="<?php echo $row['per_day_salary'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Working Days</label>
                                                                                        <input type="text" class="form-control" name="working_days" value="<?php echo $row['working_days'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-6">
                                                                                        <label for="">Contact Number</label>
                                                                                        <input type="text" class="form-control" name="contact_number" value="<?php echo $row['contact_number'] ?>">
                                                                                    </div>
                                                                                    <div class="col-6">
                                                                                        <label for="">Email</label>
                                                                                        <input type="text" class="form-control" name="email" value="<?php echo $row['email'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <footer>
                                                                                    <button type="submit" class="btn bg-dark mt-3 ">Update</button>
                                                                                </footer>
                                                                            </form>
                                                                        </div>

                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <!-- edit model -->

                                                            <!-- pay roll calculate model -->
                                                            <div class="modal fade" id="exampleModal<?php echo $row['id']; ?>" tabindex="-1" aria-labelledby="exampleModalLabel<?php echo $row['id']; ?>" aria-hidden="true">
                                                                <div class="modal-dialog modal-xl">
                                                                    <div class="modal-content">
                                                                        <div class="modal-header bg-dark">
                                                                            <h5 class="modal-title" id="exampleModalLabel<?php echo $row['id']; ?>"><?php echo $row['name'] ?></h5>
                                                                            <button type="button" class="btn-close bg-white rounded-circle shadow" data-bs-dismiss="modal" aria-label="Close"></button>
                                                                        </div>
                                                                        <div class="modal-body">
                                                                            <form action="">
                                                                                <div class="row">
                                                                                    <div class="col-3">
                                                                                        <label for="">Name</label>
                                                                                        <input type="text" class="form-control" value="<?php echo $row['name'] ?>">
                                                                                    </div>
                                                                                    <div class="col-3">
                                                                                        <label for="">Contact</label>
                                                                                        <input type="text" class="form-control" value="<?php echo $row['contact_number'] ?>">
                                                                                    </div>
                                                                                    <div class="col-3">
                                                                                        <label for="">Email</label>
                                                                                        <input type="text" class="form-control" value="<?php echo $row['email'] ?>">
                                                                                    </div>
                                                                                    <div class="col-3">
                                                                                        <label for="">Joining Date</label>
                                                                                        <input type="date" class="form-control" value="<?php echo $row['joining_date'] ?>">
                                                                                    </div>
                                                                                </div>
                                                                                <div class="row">
                                                                                    <div class="col-2">
                                                                                        <label for="employeeId<?php echo $row['id']; ?>">Employee Id</label>
                                                                                        <input type="text" class="form-control" id="employeeId<?php echo $row['id']; ?>" value="<?php echo $row['employee_id'] ?>">
                                                                                    </div>
                                                                                    <div class="col-2">
                                                                                        <label for="totalSalary<?php echo $row['id']; ?>">Total Salary</label>
                                                                                        <input type="text" class="form-control total-salary" id="totalSalary<?php echo $row['id']; ?>" value="<?php echo $row['total_salary'] ?>">
                                                                                    </div>
                                                                                    <div class="col-2">
                                                                                        <label for="perDaySalary<?php echo $row['id']; ?>">Per-Day Salary</label>
                                                                                        <input type="text" class="form-control per-day-salary" id="perDaySalary<?php echo $row['id']; ?>" value="<?php echo $row['per_day_salary'] ?>">
                                                                                    </div>
                                                                                    <div class="col-2">
                                                                                        <label for="workingDays<?php echo $row['id']; ?>">Working Days</label>
                                                                                        <input type="text" class="form-control working-days" id="workingDays<?php echo $row['id']; ?>" value="<?php echo $row['working_days'] ?>">
                                                                                    </div>
                                                                                    <div class="col-2">
                                                                                        <label for="leaves<?php echo $row['id']; ?>">Leaves</label>
                                                                                        <input type="text" class="form-control leaves" id="leaves<?php echo $row['id']; ?>" value="<?php echo $row['leaves'] ?>">
                                                                                    </div>
                                                                                    <div class="col-2">
                                                                                        <label for="toBePaid<?php echo $row['id']; ?>">To Be Paid</label>
                                                                                        <input type="text" class="form-control to-be-paid" id="toBePaid<?php echo $row['id']; ?>" readonly>
                                                                                    </div>
                                                                                </div>
                                                                            </form>
                                                                        </div>
                                                                        <div class="modal-footer">
                                                                            <button type="button" class="btn btn-dark" data-bs-dismiss="modal">Close</button>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </div>
                                                            <!-- pay roll calculate model -->

                                                        <?php } ?>
                                                        </tbody>

                                                    </table>
                                                </table>
                                            </div>
                                        </div>

                                    </div>
                                </div>

                            </div>

                        </div>

                    </div>

                </div>

            </section>

        </div>
    </div>

    <!-- new payroll added model -->
    <div class="modal fade" id="modal-success">
        <div class="modal-dialog modal-xl">
            <div class="modal-content bg-white">
                <div class="modal-header bg-dark text-white">
                    <h4 class="modal-title fw-bold">Add Staff Salary</h4>
                    <button type="button" class="close text-white" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form action="payroll_code.php" method="POST">
                        <div class="row">
                            <div class="col-6">
                                <label for="">Name</label>
                                <input type="text" name='name' class="form-control" placeholder="Name">
                            </div>
                            <div class="col-6">
                                <label for="">Total Salary</label>
                        
                                <input type="text" class="form-control"  name="total_salary" id="exampleInputName" placeholder="Total Salary" name="number" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <label for="">To be Paid</label>
                                <!-- <input type="text" class="form-control" name="to_be_paid" > -->
                                <input type="text" class="form-control" name="to_be_paid" id="exampleInputName" placeholder="To be Paid" name="number" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                            <div class="col-6">
                                <label for="">Salary Date</label>
                                <input type="date" class="form-control" name="salary_date">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <label for="">Joining Date</label>
                                <input type="date" class="form-control" name="joining_date">
                            </div>
                            <div class="col-6">
                                <label for="">Status</label>
                                <input type="text" class="form-control" name="status" placeholder="Status">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <label for="">Leaves</label>
                                <input type="text" class="form-control" name="leaves" placeholder="Leaves">
                            </div>
                            <div class="col-6">
                                <label for="">Employee Id</label>
                                <input type="text" class="form-control" name="employee_id" placeholder="Employee Id">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <label for="">Per Day Salary</label>

                                <input type="text" class="form-control" id="exampleInputName" name="per_day_salary" placeholder="Per Day Salary" name="number" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                            <div class="col-6">
                                <label for="">Working Days</label>
                                <input type="text" class="form-control" name="working_days" value="30" readonly>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-6">
                                <label for="">Contact Number</label>
                                <!-- <input type="text" class="form-control" name="contact_number"> -->
                                <input type="text" class="form-control" id="exampleInputName" placeholder="Contact Number" name="contact_number" oninput="this.value = this.value.replace(/[^0-9]/g, '');">
                            </div>
                            <div class="col-6">
                                <label for="">Email</label>
                                <input type="email" class="form-control" name="email" placeholder="Enter Your Email...">
                            </div>
                        </div>
                        <footer>
                            <button type="submit" class="btn btn-dark mt-3 ">Submit</button>
                        </footer>
                    </form>
                </div>

            </div>
            <!-- /.modal-content -->
        </div>
        <!-- /.modal-dialog -->
    </div>
<!-- new payroll added model end -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>


    <?php include("footer.php") ?>
    <?php include("footerlink.php") ?>

    <script src="https://cdn.datatables.net/1.11.4/js/jquery.dataTables.min.js"></script>
    <script>
        // Function to calculate and update "To Be Paid" for a specific modal
        function updateToBePaid(modal) {
            const totalSalaryInput = modal.querySelector(".total-salary");
            const perDaySalaryInput = modal.querySelector(".per-day-salary");
            const workingDaysInput = modal.querySelector(".working-days");
            const leavesInput = modal.querySelector(".leaves");
            const toBePaidInput = modal.querySelector(".to-be-paid");

            const totalSalary = parseFloat(totalSalaryInput.value) || 0;
            const perDaySalary = parseFloat(perDaySalaryInput.value) || 0;
            const workingDays = parseFloat(workingDaysInput.value) || 0;
            const leaves = parseFloat(leavesInput.value) || 0;

            const toBePaid = (workingDays * perDaySalary) - (leaves * perDaySalary);

            toBePaidInput.value = toBePaid.toFixed(2);
        }

        // Add an event listener to update "To Be Paid" when any input field changes in a modal
        const modals = document.querySelectorAll(".modal");
        modals.forEach((modal) => {
            modal.addEventListener("input", () => {
                updateToBePaid(modal);
            });
        });
    </script>


    <script>
        $(document).ready(function() {
            <?php if (isset($_GET['error'])) { ?>
                Swal.fire({
                    title: 'Error',
                    text: '<?php echo $_GET['error']; ?>',
                    icon: 'error',
                    confirmButtonText: 'OK',
                    confirmButtonClass: 'btn btn-success btn-lg',
                });
            <?php } ?>
            <?php if (isset($_GET['success'])) { ?>
                Swal.fire({
                    title: 'Success',
                    text: '<?php echo $_GET['success']; ?>',
                    icon: 'success',
                    confirmButtonText: 'OK',
                    confirmButtonClass: 'btn btn-success btn-lg',
                });
            <?php } ?>
        });
    </script>


    <script>
        var $j = jQuery.noConflict();
        $j(document).ready(function() {
            $j('#example2').DataTable();
        });
    </script>

</body>

</html>