<?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>

            <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">
                                Send PIP
                            </h4>

                            <form id="form_submit" class="needs-validation" novalidate>
                                <div class="row">

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="employee_id">Employee<span class="text-danger">*</span></label>
                                            <select data-plugin="customselect" name="employee_id" class="form-control custom-select-picker-ajax" data-search="employees" data-param="<?php echo htmlspecialchars(json_encode(['default'=>$edit['employee_id'], 'current_page'=>$currentPageName])); ?>" data-placeholder="Select Employee" id="employee_id" required>
                                                <option></option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="col-md-6">
                                        <div class="form-group">
                                            <label for="until">Until Date<span class="text-danger">*</span></label>
                                            <input type="date" name="until" required placeholder="Enter Until Date" class="form-control" id="until" value="">
                                        </div>
                                    </div>
                                </div>

                                <input type="hidden" name="add_pip_submit" id="add_pip_submit" value="1">

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

                            </form>
                        </div>
                    </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">
                                All PIPs
                            </h4>

                            <table id="basic-datatable" class="table dt-responsive nowrap">
                                <thead>
                                    <tr>
                                        <th>ID</th>
                                        <th>Employee ID</th>
                                        <th>Name</th>
                                        <th>Email</th>
                                        <th>Until</th>
                                        <th>Mail Send</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: 'id'}, {data: 'employee_id'}, {data: 'name'}, {data: 'email'}, {data: 'until'}, {data: 'mail_send'}];

        // ############################### 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/pips_get', columns, [0, 'asc'], setArray);
            } else {
                loadTable('#basic-datatable', 'get_from_server/pips_get', columns, [0, 'asc']);
            }
            // console.log(setArray);
            setArray = [];
            // console.log(setArray);
        }

        show_hierarchy_data();
    });

    $('#form_submit').submit(function (e) {
        e.preventDefault(); // stop normal form submission

        let form = document.getElementById('form_submit');

        // check form validation
        if (!form.checkValidity()) {
            form.classList.add('was-validated');
            return false;
        }

        // let form_data = new FormData(form);
        let form_data = $('#form_submit').serialize();
        // console.log(form_data);
        // return false;

        $.ajax({
            url: 'fun/manage_pip',
            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.href='pip';
                    }, 2000);
                } else {
                    failedMessage(res.message, '<?php echo $_SESSION['user_name']; ?>');
                }
            },
            error: function(xhr, status, error) {
                console.log(`Error: ${JSON.stringify(error)}`);
            }
        });
    });
</script>