$(document).ready(function () {

    /*
    |--------------------------------------------------------------------------
    | DATATABLE
    |--------------------------------------------------------------------------
    */

    $('#gradeTable').DataTable({

        responsive: true,

        autoWidth: false,

        pageLength: 25,

        ordering: true
    });

    /*
    |--------------------------------------------------------------------------
    | SAVE GRADE
    |--------------------------------------------------------------------------
    */

    $('#gradeForm').on(
        'submit',
        function (e) {

            e.preventDefault();

            const form = $(this);

            $.ajax({

                url: 'process/save-grade-process.php',

                type: 'POST',

                data: form.serialize(),

                dataType: 'json',

                beforeSend: function () {

                    Swal.fire({

                        title: 'Saving Grade...',

                        text: 'Please wait',

                        allowOutsideClick: false,

                        didOpen: () => {
                            Swal.showLoading();
                        }
                    });
                },

                success: function (response) {

                    Swal.close();

                    if (response.success) {

                        toastr.success(
                            response.message
                        );

                        setTimeout(function () {

                            location.reload();

                        }, 800);

                    } else {

                        toastr.error(
                            response.message
                        );
                    }
                },

                error: function (xhr) {

                    Swal.close();

                    let errorMessage =
                        'Something went wrong!';

                    if (
                        xhr.responseJSON &&
                        xhr.responseJSON.message
                    ) {

                        errorMessage =
                            xhr.responseJSON.message;
                    }

                    toastr.error(errorMessage);
                }
            });
        }
    );

    /*
    |--------------------------------------------------------------------------
    | DELETE GRADE
    |--------------------------------------------------------------------------
    */

    $(document).on(
        'submit',
        '.deleteGradeForm',
        function (e) {

            e.preventDefault();

            const form = $(this);

            Swal.fire({

                title: 'Delete Grade?',

                text: 'This action cannot be undone.',

                icon: 'warning',

                showCancelButton: true,

                confirmButtonText: 'Yes, Delete',

                cancelButtonText: 'Cancel'

            }).then((result) => {

                if (!result.isConfirmed) {

                    return;
                }

                $.ajax({

                    url: 'process/delete-grade-process.php',

                    type: 'POST',

                    data: form.serialize(),

                    dataType: 'json',

                    beforeSend: function () {

                        Swal.fire({

                            title: 'Deleting Grade...',

                            text: 'Please wait',

                            allowOutsideClick: false,

                            didOpen: () => {
                                Swal.showLoading();
                            }
                        });
                    },

                    success: function (response) {

                        Swal.close();

                        if (response.success) {

                            toastr.success(
                                response.message
                            );

                            form.closest('tr').remove();

                        } else {

                            toastr.error(
                                response.message
                            );
                        }
                    },

                    error: function (xhr) {

                        Swal.close();

                        let errorMessage =
                            'Something went wrong!';

                        if (
                            xhr.responseJSON &&
                            xhr.responseJSON.message
                        ) {

                            errorMessage =
                                xhr.responseJSON.message;
                        }

                        toastr.error(errorMessage);
                    }
                });
            });
        }
    );
});