<?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'])){
    $q = mysqli_query($db,"DELETE FROM request_leave WHERE id='$_GET[delete]' ");

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

?>


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

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

            <div class="mt-5"></div>

            <?php if ($user_role > 0) { ?>

                <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">
                                    Your All Leave Request
                                </h4>

                                <table id="basic-datatable" class="table dt-responsive nowrap">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Date</th>
                                            <th>Type</th>
                                            <th>Reason</th>
                                            <th>Request Time</th>
                                            <th>Status</th>
                                            <th>Remark</th>
                                        </tr>
                                    </thead>

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

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

            <?php } ?>

            <?php if ($user_role < 2) { ?>


                <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">
                                    Requests For Leave
                                </h4>

                                <table id="basic-datatable2" class="table dt-responsive nowrap">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Employee ID</th>
                                            <th>Name</th>
                                            <th>Date</th>
                                            <th>Type</th>
                                            <th>Reason</th>
                                            <th>Request Time</th>
                                            <th>Status</th>
                                            <th>Remark</th>
                                            <th>Change Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>

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

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


            <?php } ?>

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

</div>

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



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

<script>
    function changeStatus(id, status) {

        let remark = $("#remark" + id).val();

        if (remark == '') {
            alert("Please Enter Remark !");
        } else {
            $.ajax({
                url: "fun/function",
                type: "POST",
                data: {
                    req_status: status,
                    remark: remark,
                    request_id: id,
                },
                success: function(res) {
                    // console.log(res);
                    if (res == 1) {
                        if (status == 1) {
                            $("#requestButton" + id).html('<button type="button" class="btn btn-sm btn-soft-success" title="Approved"><i class="uil uil-check-circle mb-0" style="font-size:20px;"></i></button>');
                            $("#requestStatus" + id).html('<span class="text-success">Approved</span>')
                            successMessage('Request Approved Successfully !');
                        } else {
                            $("#requestButton" + id).html('<button type="button" class="btn btn-sm btn-soft-danger" title="Rejected"><i class="uil uil-times-circle mb-0" style="font-size:20px;"></i></button>');
                            $("#requestStatus" + id).html('<span class="text-danger">Rejected</span>')
                            successMessage('Request Rejected Successfully !');
                        }
                    } else {
                        failedMessage('Some Error Occured !');
                    }

                }
            });
            // location.href = "fun/function?req_status=" + status + "&remark=" + remark + "&request_id=" + id;
        }

    }

    $(document).ready(function() {

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

            // ############################### setArray
            // ############################### setArray

            $("#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);
        }

        // ############################### columns
        columns = [{data: 'row_num'}, {data: 'leave_date'}, {data: 'leave_type'}, {data: 'leave_reason'}, {data: 'created_at'}, {data: 'leave_status'}, {data: 'remark'}];
        // ############################### columns
        show_hierarchy_data('basic-datatable', 'all_leaveRequest_self_get', columns, [0, 'asc']);

        columns =[{data: 'row_num'}, {data: 'employee_id'}, {data: 'name'}, {data: 'leave_date'}, {data: 'leave_type'}, {data: 'leave_reason'}, {data: 'created_at'}, {data: 'leave_status'}, {data: 'remark'}, {data: 'leader'}, {data: 'id'}];
        show_hierarchy_data('basic-datatable2', 'all_leaveRequest_employee_get', columns, [0, 'asc']);

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

</script>