<?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['requestDate'])) {
    $firstDate = date("d M Y", strtotime($_GET['requestDate']));
    $secondDate = '';
} else {
    $firstDate = date("d M Y");
    $secondDate = date("d M Y", strtotime('next day'));
}



?>


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

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

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

            <div class="row">
                <div class="col-lg-12">
                    <div class="card">
                        <div class="card-body p-4">
                            <h4 class="header-title mt-1 mb-4">
                                Leave Request
                            </h4>

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

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="leaveDate">Leave Date<span class="text-danger">*</span></label>
                                            <input type="text" name="leaveDate" parsley-trigger="change" required class="form-control" id="dash-daterange">
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="leaveType">Leave Type<span class="text-danger">*</span></label>
                                            <select data-plugin="customselect" name="leaveType" class="form-control custom-select-picker" data-placeholder="Select Leave Type" id="leaveType" required>
                                                <option></option>
                                                <option value="Absent">Absent</option>
                                                <option value="Half Day">Half Day</option>
                                                <option value="Work From Home">Work From Home</option>

                                            </select>
                                        </div>
                                    </div>

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

                                <input type="hidden" name="requestLeave">

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

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

            </div>

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

</div>

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



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

<script>
    flatpickr("#dash-daterange", {
        mode: "range",
        dateFormat: "d M Y",
        minDate: "today",
        maxDate: "<?php echo date("d M Y", strtotime("today +30 day")) ?>",
        defaultDate: [
            "<?php echo $firstDate; ?>",
            "<?php echo $secondDate; ?>"
        ]
    });

    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>