<?php
Yii::app()->clientScript->registerCoreScript('jquery.ui');

if (!isset($useSortFunction)) {
    $useSortFunction = "true";
}
?>
<script>
    var useSortFunction = "<?php echo $useSortFunction; ?>";

    if (useSortFunction === "true") {
        $(function () {
            $("#firewall-form tbody").sortable();
            $("#firewall-form tbody").on("sortstop", function (event, ui) {
                $.each($("#firewall-form tbody tr"), function (i, e) {
                    $(e).find('.hiddenSortIndex').find('input.hidenIndex').val(i);
                });
            });
        });
    }

    function aftValidate(form, data, hasError) {
        if (!hasError) {
            var formData = new FormData($('#firewall-form')[0]);
            $.ajax({
                url: form.attr('action'), //server script to process data
                type: 'POST',
                xhr: function () {  // custom xhr
                    myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) { // check if upload property exists
                        console.log(myXhr);
                        myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // for handling the progress of the upload
                    }
                    return myXhr;
                },
                //Ajax events
                beforeSend: function () {
                },
                success: function (data) {
                    $("#saveNewFirewallRuleButton").button('reset');
                    refreshGrid();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    console.log('this is error');
                    console.log(jqXHR);
                    console.log(textStatus);
                    console.log(errorThrown);
                },
                //error: errorHandler,
                // Form data
                data: formData,
                //Options to tell JQuery not to process data or worry about content-type
                cache: false,
                contentType: false,
                processData: false
            });
        } else {
            $("#saveNewFirewallRuleButton").button('reset');
        }
        return false;
    }

    $('#firewall-grid').bind("keyup keypress", function (e) {
        var code = e.keyCode || e.which;
        if (code === 13) {
            e.preventDefault();
            $('#saveNewFirewallRuleButton').click();
        }
    });

    $('#saveNewFirewallRuleButton').click(function () {
        validateForm();
        return false;
    });
    function validateForm() {
        var formData = new FormData($('#firewall-form')[0]);
        var url = '<?php echo url("networkFirewall/default/ajaxValidate"); ?>'; //FIXME
        var isValid = false;
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            data: formData,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                if (data) {
                    var alertBlockTemplate = '<div class="alert alert-error alert-block fade in"><strong></strong><a class="close" data-dismiss="alert" href="#">&times;</a></div>';
                    $('#alertFlash').html(alertBlockTemplate);
                    $('#content .navbar').after($('#alertFlash'));
                    $('#firewall-grid').find('.error').each(function (inx, element) {
                        $(element).removeClass('error');
                    });
                    $.each(data, function (i, error) {
                        $.each(error, function (index, errorMessage) {
                            $('#' + index).addClass('error');
                            $('#alertFlash').find('strong').text(errorMessage);
                        });
                    });
                } else {
                    var btn = $(this);
                    btn.button('loading'); // call the loading function
                    $("#firewallAction").click();
                }
            }
        });
        return isValid;
    }

    function changeFirewallStatus(btn) {
        var firewallDisabledText = '<?php echo __('NETWORK_FIREWALL_NAVBAR_FIREWALL_STATUS_DISABLED'); ?>';
        var firewallEnabledText = '<?php echo __('NETWORK_FIREWALL_NAVBAR_FIREWALL_STATUS_ENABLED'); ?>';
        var url = "<?php echo url('networkFirewall/default/changeFirewallStatus'); ?>";
        $.ajax({
            url: url,
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                if (data === 1) {
                    $('#firewallSwitch').text(firewallEnabledText);
                    $('#firewallSwitch').addClass('btn-success');
                    $('#firewallSwitch').removeClass('btn-danger');
                } else {
                    $('#firewallSwitch').text(firewallDisabledText);
                    $('#firewallSwitch').addClass('btn-danger');
                    $('#firewallSwitch').removeClass('btn-success');
                }
                window.location.reload();
            }
        });
    }
</script>
