<?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'])) {

    $ul = mysqli_fetch_assoc(mysqli_query($db,"SELECT * FROM utilize_leave where id='$_GET[delete]' "));
    $leave_date = date('M d Y', strtotime($ul['leave_date']));
    $ac = mysqli_fetch_assoc(mysqli_query($db,"SELECT * FROM attendance_change WHERE employee_id='$ul[employee_id]' AND which_month='$leave_date' "));


    $ad = mysqli_query($db,"UPDATE attendance SET attendance_status='$ac[attendance_status]', attendance_change='0', attendance_change_reason=NULL WHERE id='$ac[attendance_id]' ");
    if($ad){
        $acd = mysqli_query($db,"DELETE FROM attendance_change WHERE id='$ac[id]' ");
        if($acd){
            $q = mysqli_query($db, "DELETE FROM utilize_leave WHERE id='$ul[id]' ");

            if ($q) {
                echo "<script>alert('Utilize Leave Undo Successfully !');</script>";
                echo "<script>location.href='utilize_leaves';</script>";
            }else{
                echo "ul => ". mysqli_error($db);
            }
        }else{
            echo "ac => ". mysqli_error($db);
        }
    }else{
        echo "a => ". mysqli_error($db);
    }


}

?>


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

<div class="content-page">
    <div class="content">
        <div class="container-fluid">
            <div class="mt-5"></div>

            <div class="row mb-3" id="filter_block">
                <div class="col-md-3">
                    <div class="form-group">
                        <label class="form-label" for="employee_id">User (Name, E-mail, Phone)</label>
                        <select required id="employee_id" name="employee_id" data-plugin="customselect" class="form-control custom-select-picker-ajax" data-search="employees" data-param="<?php echo htmlspecialchars(json_encode(['default'=>'', 'current_page'=>$currentPageName])); ?>" data-placeholder="Select user">
                            <option></option>
                        </select>
                    </div>
                </div>

                <div class="col-md-3">
                    <div class="form-group">
                        <label class="form-label" for="leave_date">Utilize Leave Date</label>
                        <input type="date" name="leave_date" id="leave_date" class="form-control flatpickr_date" data-isDate="true" required>
                    </div>
                </div>
              
                <div class="col-md-2">
                    <div class="d-flex flex-column mb-3">
                        <button type="button" class="btn btn-danger btn-padding mb-1" name="clear_filter" id="clear_filter">Clear</button>
                        <button class="btn btn-success btn-padding" type="button" name="search_filter" id="search_filter">Search</button>
                    </div>
                </div>
                <div class="col-sm-12">
                    <div id="filter_options" class="d-flex align-items-center flex-wrap" style="gap:0.25rem"></div>
                </div>
            </div>

            <div class="row">
                <div class="col-12">
                    <div class="card">
                        <div class="card-body p-4">
                            <h4 class="header-title mt-0 mb-1 mb-4">
                                Utilized Leaves
                            </h4>

                            <table id="basic-datatable" class="table dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Employee Id</th>
                                        <th>Name</th>
                                        <th>Leave Status</th>
                                        <th>Leave Count</th>
                                        <th>Leave Date</th>
                                        <th>Undo</th>
                                    </tr>
                                </thead>

                            </table>
                            <!-- </div> -->

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

        </div>
    </div> 

</div>



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

<script>

  $(document).ready(function() {
        
        // ############################### columns
        let columns = [{data: 'row_num'}, {data: 'employee_id'}, {data: 'name'}, {data: 'leave_status'}, {data: 'leave_count'}, {data: 'leave_date'}, {data: 'id'}];

        // ############################### columns
        function show_hierarchy_data() {

            $("#filter_options").html('');
            form_fields.each(function() {
                search_filter(this);
            });
            if (setArray.length > 0) {
                loadTable('#basic-datatable', 'get_from_server/utilize_leaves_get', columns, [0, 'asc'], setArray);
            } else {
                loadTable('#basic-datatable', 'get_from_server/utilize_leaves_get', columns, [0, 'asc']);
            }
            // console.log(setArray);
            setArray = [];
            // console.log(setArray);
        }

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