<?php
include "pin/header.php";
include "pin/sidenav.php";

$user_id = $_SESSION['user_id'];
$emp_id = $_SESSION['emp_id'];
$user_role = $_SESSION['user_role'];

if (isset($_GET['delete'])) {

    $oldRevision = mysqli_fetch_assoc(mysqli_query($db, "select * from employee_salary where id='$_GET[delete]' "));

    $od = mysqli_query($db, "update employee_salary set revision_end_date='' where employee_id='$oldRevision[employee_id]' and revision_end_date='$oldRevision[revision_start_date]' ");

    if ($od) {

        $q = mysqli_query($db, "DELETE FROM `employee_salary` WHERE `id`='$_GET[delete]' ");

        if ($q) {
            echo "<script>alert('Deleted Successfully !');</script>";
        }
    } else {
        echo mysqli_error($db);
    }
    echo "<script>location.href='all_salary';</script>";
}

// $edit = mysqli_fetch_assoc(mysqli_query($db, "SELECT * FROM `employee_salary` WHERE `id`='$_GET[edit]' "));


?>


<!-- ============================================================== -->
<!-- Start Page Content here -->
<!-- ============================================================== -->

<div class="content-page">
    <div class="content">
        <div class="container-fluid">

            <div class="row page-title align-items-center">
                <div class="col-sm-4 col-xl-6">

                </div>
                <div class="col-sm-8 col-xl-6">
                    <form class="form-inline float-sm-right mt-3 mt-sm-0">
                        <div class="btn-group">
                            <button type="button" class="btn btn-primary" onclick="exportDataIds();">
                                <i class='uil uil-file-alt mr-1'></i>Download Excel<i class="uil uil-down-arrow ml-1"></i>
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body p-4">
                            <div class="d-flex justify-content-between">
                                <h4 class="header-title mt-1 mb-4">All Employees Salary</h4>
                            </div>

                            <div class="row" id="yearlyData">
                                <div class="col-12">
                                    <table id="basic-datatable" class="table dt-responsive nowrap">
                                        <thead>
                                            <tr>
                                                <th><input id="checkAll" type="checkbox"></th>
                                                <th>ID</th>
                                                <th>Employee Id</th>
                                                <th>Name</th>
                                                <th>Fixed Pay</th>
                                                <th>Add On</th>
                                                <th>Deduction</th>
                                                <th>Yearly CTC</th>
                                                <th>Net Payable</th>
                                                <th>Bonus/Incentive</th>
                                                <th>Revision Date</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>

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

                        </div> <!-- end card body-->
                    </div> <!-- end card -->
                </div><!-- end col-->
            </div>
            <!-- end row-->

        </div>
    </div> <!-- content -->

</div>

<!-- ============================================================== -->
<!-- End Page content -->
<!-- ============================================================== -->



<?php
include "pin/footer.php";
?>

<script>

    $(document).on('click', '.salary_key', function() {
        console.log('enter');
        $(this).next('ul').slideToggle();
        // also toggle icon
        $(this).find('i').toggleClass('uil-angle-down uil-angle-up');
    });

    function exportDataIds() {
        if (all_checkboxes_val.length!=0) {

            // console.log(users, from, to);
            if (confirm("Export Salary to Excel....?")) {
                $.ajax({
                    url:'fun/exportEmployeesSalary',
                    type:"GET",
                    data:{users:all_checkboxes_val},
                    dataType: "json",
                    success:function(res){
                        // console.log(res);
                        if (res.status=200) {
                            successMessage(res.message, '<?php echo $_SESSION['user_name'] ?>');
                        } else {
                            failedMessage(res.message, '<?php echo $_SESSION['user_name'] ?>');
                        }
                    },
                    error: function(xhr, status, error) {
                        console.log(`Error: ${JSON.stringify(error)}`);
                    }
                });
            } else {
                return false;
            }

        } else {
            alert("Please Select Employees Salary...");
        }

    }

    $(document).ready(function() {

        function show_hierarchy_data(table_id, file_name, columns, default_order) {

            $("#filter_options").html('');
            form_fields.each(function() {
                search_filter(this);
            });

            if (setArray.length > 0) {
                loadTable(`#${table_id}`, `get_from_server/${file_name}`, columns, default_order, setArray);
            } else {
                loadTable(`#${table_id}`, `get_from_server/${file_name}`, columns, default_order);
            }
            // console.log(setArray);
            setArray = [];
            // console.log(setArray);
        }

        let columns =[{data: 'id'}, {data: 'row_num'}, {data: 'employee_id'}, {data: 'name'}, {data: 'fixed'}, {data: 'add_on'}, {data: 'deduction'}, {data: 'ctc'}, {data: 'net_payable'}, {data: 'bonus'}, {data: 'revision_start_date'}, {data: 'revision_end_date'}];
        show_hierarchy_data('basic-datatable', 'all_salary_yearly_get', columns, [1, "asc"]);

        // columns = [{data: 'row_num'}, {data: 'employee_id'}, {data: 'name'}, {data: 'basic_salary'}, {data: 'hra'}, {data: 'books_per'}, {data: 'special_allowance'}, {data: 'company_epf'}, {data: 'ctc'}, {data: 'epf_status'}, {data: 'esi_status'}, {data: 'tds'}, {data: 'bonus'}, {data: 'incentive'}];
        // show_hierarchy_data('basic-datatable2', 'all_salary_monthly_get', columns, [0, "asc"]);

        // $("#search_filter").click(show_hierarchy_data);
    });

</script>