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

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

?>


<!-- ============================================================== -->
<!-- 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-lg-12">
                        <div class="card">
                            <div class="card-body p-4">
                                <h4 class="header-title mt-1">
                                    Your Problem / Grievance
                                </h4>
                                <p class="mb-4 text-danger">These Problems are Anonymous.</p>

                                <form id="form_submit" class="parsley-examples">
                                    <div class="row">

                                        <div class="col-md-12">
                                            <div class="form-group">
                                                <label for="grievance">Enter Problem<span class="text-danger">*</span></label>
                                                <textarea name="grievance" id="grievance" class="form-control" cols="30" rows="5" placeholder="Enter Your problem" required></textarea>
                                            </div>
                                        </div>
                                    </div>

                                    <input class="btn btn-primary mr-1" name="grievanceSend" type="hidden">

                                    <div class="form-group text-right mb-0 mt-4">
                                        <button class="btn btn-primary mr-1" name="grievanceSend" type="submit">Send</button>
                                    </div>

                                </form>
                            </div>
                        </div> <!-- end card -->
                    </div>
                    <!-- end col -->

                </div>
                <!-- end row -->

            <?php } ?>

            <?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">
                                    All Grievance / Problems
                                </h4>

                                <table id="basic-datatable" class="table dt-responsive nowrap">
                                    <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Employee Grievance</th>
                                            <th>Date</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>
 $(document).ready(function() {

        // ############################### columns
            let columns = [{data: 'row_num'}, {data: 'grievance'}, {data: 'created_at'}];
        // ############################### columns

        function show_hierarchy_data() {

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

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

            if (setArray.length > 0) {
                loadTable('#basic-datatable', 'get_from_server/grievance_get', columns, [0, 'asc'], setArray);
            } else {
                loadTable('#basic-datatable', 'get_from_server/grievance_get', columns, [0, 'asc']);
            }
            // console.log(setArray);
            setArray = [];
            // console.log(setArray);
        }

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


    function hasEmptyFields(form) {
        // Get all input elements within the form
        const inputs = form.querySelectorAll('input, select, textarea');

        // Check if any input is empty or null
        return Array.from(inputs).some(input => {
                // console.log(input.required, input.value.trim(), input.id);
                return input.required && input.value.trim() === ''
            }
        );
    }

    $('#form_submit').submit(function (e) {
        e.preventDefault();
        let form = document.getElementById('form_submit');
        // let form_data = new FormData(form);
        let form_data = $('#form_submit').serialize();
        // console.log(form_data);

        if(hasEmptyFields(form)){
            failedMessage('Please, Fill all the Fields.','<?php echo $_SESSION['user_name']; ?>');
            return false;
        }
        $.ajax({
            url: 'fun/function',
            type: 'POST',
            data: form_data,
            dataType: 'json',
            success: function(res) {
                console.log(res);
                if (res.status === 200) {
                    successMessage(res.message, '<?php echo $_SESSION['user_name']; ?>');
                    setTimeout(() => {
                        location.reload();
                    }, 2000);
                } else {
                    failedMessage(res.message, '<?php echo $_SESSION['user_name']; ?>');
                }
            },
            error: function(xhr, status, error) {
                console.log(`Error: ${JSON.stringify(error)}`);
            }
        });
    });
</script>