<?php
require $_SERVER['DOCUMENT_ROOT'] . '/includes/init.php';

?>

<!doctype html>
<html lang="en" class="h-100">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title><?php echo $site['NAME'] . $title; ?></title>
    <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/css.php' ?>
    <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/js.php' ?>
</head>

<body class="d-flex h-100 text-center text-white bg-dark">

    <div class="cover-container d-flex w-100 h-100 p-3 mx-auto flex-column">

        <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/header.php'; ?>

        <?php
        if ($error['MESSAGE']) {
            include $_SERVER['DOCUMENT_ROOT'] . '/error.php';
        } else {
            include $_SERVER['DOCUMENT_ROOT'] . '/' . $pages[$page]['URL'];
        }
        ?>

        <?php include $_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php'; ?>
    </div>

    <!-- //START: Action Modal -->
    <div class="modal fade" id="ActionModal" tabindex="-1" aria-labelledby="ActionModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content bg-dark">
                <div class="modal-header">
                    <h5 class="modal-title ActionModal-title">Modal title</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    Modal Body
                </div>
                <div class="modal-message mb-3"></div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary btn-modal-close" data-bs-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-success btn-modal-submit">Save changes</button>

                </div>
            </div>
        </div>
    </div>
    <script>
        $(document).ready(function() {
            $("#ActionModal").on('hide.bs.modal', function() {
                $('#ActionModal').find('.modal-title').text('Modal Title');
                $('#ActionModal').find('.modal-body').text('Modal Body');
                $('#ActionModal').find('.modal-message').removeClass('text-success').removeClass('text-danger').text('');
                $('#ActionModal').find('.btn-modal-submit').text('Save changes');
            });
            $(document).on('click', '.btn-modal-submit', function(event) {
                FormName = $('#ActionModal').find('#ActionFormName').val();
                FormVal = {};
                FormValCount = 0;
                $('.ActionFormInput').each(function(index, item) {
                    name = $(item).attr('name');
                    val = $(item).val();
                    if (val) {
                        FormVal[name] = val;
                        FormValCount++;
                    }
                });


                if (!FormValCount) {
                    $('#ActionModal').find('.modal-message').removeClass('text-success').addClass('text-danger').text('ERROR: All field are empty.');
                } else {
                    FormFieldRequired = {};
                    i = 0;
                    $('.ActionFormInputRequired').each(function(index, item) {
                        FormFieldRequired[i] = $(item).attr('name');
                        i++
                    });

                    $.ajax({
                        url: "actions.php",
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            action: FormName,
                            data: JSON.stringify(FormVal),
                            required: JSON.stringify(FormFieldRequired)
                        },
                        success: function(response) {
                            //console.log('response:' + response['ENDINGACTION']);
                            if (response['STATUS'] == "ERROR") {
                                $('#ActionModal').find('.modal-message').removeClass('text-success').addClass('text-danger').html('ERROR: ' + response['MESSAGE']);
                            } else if (response['STATUS'] == "OK") {
                                $('#ActionModal').find('.modal-message').removeClass('text-danger').addClass('text-success').text('Record has been saved.');
                                if (response['ENDINGACTION'] == "RELOAD-TABLE") {
                                    $('.table').DataTable().ajax.reload();
                                    setTimeout(function() {
                                        $('#ActionModal').modal('hide');
                                    }, 1000);
                                } else if (response['ENDINGACTION'] == "RELOAD-PAGE") {
                                    setTimeout(function() {
                                        location.reload();
                                    }, 1000);
                                }
                            }
                        },
                        error: function(jqXHR, textStatus, errorThrown) {
                            $('#ActionModal').find('.modal-message').removeClass('text-success').addClass('text-danger').text('ERROR: ' + errorThrown);
                        }
                    });
                }

                //Closing the Modal.


                return false;
            });
        });
    </script>
    <!-- //END: Action Modal -->

</body>

</html>