$(function () {
    "use strict";
    // wyświetlanie pytań
    $('.selectCampaign').change(function () {
        $('#selectCampaignForm').submit();
    });
    $('.selectLogDate').change(function () {
        $('#selectLogDateForm').submit();
    });
    // wyświetlanie buttonów opcji newsa
    $('#sendersList tr').mouseenter(
        function () {
            $('.buttonsItems', this).css('display', 'block');
        }
    )
        .mouseleave(
            function () {
                $('.buttonsItems', this).css('display', 'none');
            }
        );
    //wysyłanie formularza paginacji
    $('select[name=per_page]').change(function () {
        $(this).parent().submit();
    })

    //walidacja formularza dodawanie i edycja newsa
    $('#wi_admin_newsletter_messagecontent').submit(function () {
        if($('.chosen-search-input').val()=="Select Some Options"){
            $('#contentValidationCat').css('display', 'block');
            return false;
        }
    });

});

$(function () {
    "use strict";
    if($('.tooltipster-top').length>0){
        $('.tooltipster-top').tooltipster({
            position: "top"
        });
    }
});

$(function () {
    "use strict";
    if($('#recipientsInfo').length>0){
        $('#recipientsInfo').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            "dom": '<f<t><"bottom"ilp>><"clear">',
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#sendersList').length>0){
        $('#sendersList').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            "columns": [
                null,
                null,
                { "width": "5%" },
                { "width": "5%" }
              ],
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#configInfo').length>0){
        $('#configInfo').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            "columns": [
                { "width": "30%" },
                { "width": "45%" },
                { "width": "20%" },
                { "width": "5%" }
            ],
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#campaignsInfo').length>0){
        $('#campaignsInfo').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            "columns": [
                { "width": "5%" },
                { "width": "50%" },
                { "width": "15%" },
                { "width": "15%" },
                { "width": "15%" }
              ],
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#campaignInfo').length>0){
        $('#campaignInfo').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            "columns": [
                { "width": "5%" },
                { "width": "10%" },
                { "width": "70%" },
                { "width": "15%" }
              ],
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#logsInfo').length>0){
        $('#logsInfo').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            "columns": [
                { "width": "10%" },
                { "width": "20%" },
                { "width": "20%" },
                { "width": "25%" },
                { "width": "20%" },
                { "width": "5%" }
              ],
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#recipientsList').length>0){
        $('#recipientsList').dataTable({
            ordering: false,
            searching: true,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#messageList').length>0){
        $('#messageList').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    if($('#campaignMessage').length>0){
        $('#campaignMessage').dataTable({
            ordering: false,
            searching: false,
            paging: false,
            info: false,
            dom: '<f<t><"bottom"ilp>><"clear">',
            language: {
                url: language,
            }
        });
    }
});

$(function () {
    "use strict";
    // usuwanie pytania
    $('#delete').change(function () {
        if (this.checked) {
            $('#deleteButton').removeAttr('disabled');
        } else {
            $('#deleteButton').attr('disabled', 'disabled');
        }
    });
    // usuwanie pytania
    $('#deleteRecipient').change(function () {
        if (this.checked) {
            $('#QuestionDeleteInput').removeAttr('disabled');
        } else {
            $('#QuestionDeleteInput').attr('disabled', 'disabled');
        }
    });

    // usuwanie kategorii
    $('#deleteSender').change(function () {
        if (this.checked) {
            $('#QuestionDeleteInput').removeAttr('disabled');
        } else {
            $('#QuestionDeleteInput').attr('disabled', 'disabled');
        }

    });

    // usuwanie kategorii
    $('#deleteCampaign').change(function () {
        if (this.checked) {
            $('#QuestionDeleteInput').removeAttr('disabled');
        } else {
            $('#QuestionDeleteInput').attr('disabled', 'disabled');
        }

    });

    // usuwanie kategorii
    $('#deleteMessageContent').change(function () {
        if (this.checked) {
            $('#QuestionDeleteInput').removeAttr('disabled');
        } else {
            $('#QuestionDeleteInput').attr('disabled', 'disabled');
        }

    });

});


// Date time picker
$(function () {
    "use strict";
    if($('.datePicker').length>0){
        $('.datePicker').datetimepicker({
            timepicker:false,
            format:'d-m-Y',
            closeOnDateSelect:true,
            scrollInput: false
        });
    }
});

$(function () {
    "use strict";
    if($('.chosenSelect').length>0){
        $('.chosenSelect').chosen();
    }
});

$(function () {
    "use strict";
    if($('.chosenSelectCampaign').length>0){
        $('.chosenSelectCampaign').chosen();
    }
});

// ograniczony tineMCE dla messages
$(function () {
    "use strict";
    if ($('textarea').hasClass('tinyMCE')) {
        tinymce.init({
          selector: "textarea",  // change this value according to your HTML
          plugins: [
                "advlist autolink lists charmap print preview hr anchor pagebreak",
                "searchreplace wordcount visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
                "save table contextmenu directionality template link image paste textcolor"
            ],
          height: '600',
          relative_urls: false,
          remove_script_host: false,
          toolbar1: "insertfile undo redo | styleselect | fontselect | bold italic | alignleft aligncenter alignright alignjustify | numlist outdent indent | link image | preview template | forecolor backcolor ",
          templates: "//templates.php",
          valid_children: '+body[style]',
        });
    }
});


// edycja configu
$(function () {
    $('.icon-pencil').click(function () {

        var id = this.id;
        var i = 1;

        if($('.icon-pencil').hasClass('icondDaysOfWeek')) {
            $('.checkboxDaysOfWeek').each(function() {

                var paramValueId = 'paramValue_'+id+'_'+i ;

                var paramValue = document.getElementById(paramValueId);

                $(paramValue).removeAttr('disabled');
                i=i+1;
            });
            i=1;
        }

        var paramValueId = 'paramValue_'+id ;
        var aidValueId = 'aidValue_'+id;

        var paramValue = document.getElementById(paramValueId);
        var aidValue = document.getElementById(aidValueId);

        $(paramValue).removeAttr('disabled');
        $(aidValue).removeAttr('disabled');

        if($('#'+paramValueId+'_chosen')) {
            $(paramValue).attr('disabled', false).trigger("chosen:updated");
        }

        if($(paramValue).hasClass("switchConfig")) {
            if($(paramValue).next().hasClass("labelSwitch")) {
               $(paramValue).next().removeClass('labelSwitch');
            }
        }

        $('#'+paramValueId).css('background-color', 'white');
        $('#'+aidValueId).css('background-color', 'white');

        if( document.getElementById(id).classList.contains('icon-pencil') )
        {
            document.getElementById(id).classList.toggle('icon-floppy');
            $(this).closest('.buttonsItems').css('display', 'block');
        }

        if($(document.getElementById(paramValueId)).hasClass("switchConfig")) {
            var oldAidValue = document.getElementById(aidValueId).value;

            if(document.getElementById(paramValueId).checked) {
                oldParamValue = 1;
            }
            else {
                oldParamValue = 0;
            }
        }
        else if($(document.getElementById(id)).hasClass('icondDaysOfWeek')) {

            var oldAidValue = document.getElementById(aidValueId).value;
            var oldParamValue='';

            $('.checkboxDaysOfWeek').each(function() {
                var paramValueId = 'paramValue_'+id+'_'+i ;

                if(document.getElementById(paramValueId).checked) {
                    oldParamValue = oldParamValue+document.getElementById(paramValueId).value+',';
                }
                i=i+1;
            });
        }
        else {
            var oldParamValue = document.getElementById(paramValueId).value;
            var oldAidValue = document.getElementById(aidValueId).value;
        }

        $(this).click(function () {

            var id = this.id;
            var paramNameId = 'paramName_'+id;
            var paramValueId = 'paramValue_'+id;
            var aidValueId = 'aidValue_'+id;

            if($(document.getElementById(paramValueId)).hasClass("switchConfig")) {
                var paramName = $('#'+paramNameId).text();
                var aidValue = document.getElementById(aidValueId).value;

                if(document.getElementById(paramValueId).checked) {
                    paramValue = 1;
                }
                else {
                    paramValue = 0;
                }

                $('#'+paramValueId).css('background-color', '#f9f9f9');
                $('#'+aidValueId).css('background-color', '#f9f9f9');

                $('#'+paramValueId).prop('disabled', true);
                $('#'+aidValueId).prop('disabled', true);
            }
            else if($(document.getElementById(id)).hasClass('icondDaysOfWeek')) {

                var paramNameId = 'paramName_'+id;
                var aidValueId = 'aidValue_'+id;

                var paramName = $('#'+paramNameId).text();
                var aidValue = document.getElementById(aidValueId).value;
                var paramValue='';

                $('.checkboxDaysOfWeek').each(function() {
                    var paramValueId = 'paramValue_'+id+'_'+i ;

                    if(document.getElementById(paramValueId).checked) {
                        paramValue = paramValue+document.getElementById(paramValueId).value+',';
                    }
                    i=i+1;
                });

                $('#'+paramValueId).css('background-color', '#f9f9f9');
                $('#'+aidValueId).css('background-color', '#f9f9f9');

                $('#'+paramValueId).prop('disabled', true);
                $('#'+aidValueId).prop('disabled', true);
            }
            else {
                var paramName = $('#'+paramNameId).text();
                var paramValue = document.getElementById(paramValueId).value;
                var aidValue = document.getElementById(aidValueId).value;

                $('#'+paramValueId).css('background-color', '#f9f9f9');
                $('#'+aidValueId).css('background-color', '#f9f9f9');

                $('#'+paramValueId).prop('disabled', true);
                $('#'+aidValueId).prop('disabled', true);
            }

            if(oldParamValue!=paramValue || oldAidValue!=aidValue) {
                $.ajax({
                    url: Routing.generate('wi_admin_newsletter_mainsite_ajaxs'),
                    type: 'post',
                    data: {paramName: paramName, paramValue: paramValue, aidValue: aidValue, id: id},
                    success: function(){
                        window.location.reload();
                    }
                });
            }
            else {
                window.location.replace('');
            }
        });
    });
});

$(function () {
    "use strict";
   ($(".switchConfig")).each(function() {
        var id = this.id;;
        var paramValue = document.getElementById(id).value;
        if(paramValue==1) {
            $(this).prop('checked', true);
        }
        else {
            $(this).prop('checked', false);
        }
    });
});

// Date time picker do configu
$(function () {
    "use strict";
    if ($('.date_start').length > 0) {
        $('.date_start').datetimepicker({
            timepicker: false,
            format: 'Y-m-d',
            closeOnDateSelect: true,
            scrollInput: false
        });
    }
});
$(function () {
    "use strict";
    if ($('.date_end').length > 0) {
        $('.date_end').datetimepicker({
            timepicker: false,
            format: 'Y-m-d',
            closeOnDateSelect: true,
            scrollInput: false
        });
    }
});

$(function () {
    "use strict";
    if($('.selectCampaignParam').length>0){
        $('.selectCampaignParam').chosen();
    }
});

// -----------------------------------------------------------------------------

$(function () {
    "use strict";
    if ($('.chosenSelect').length > 0) {
        $('.chosenSelect').chosen();
    }
});

$(function () {
    "use strict";

    var ids = [];

    $('#select_all').click(function () {
        if (this.checked) {
            $('.input-checkbox').each(function () {
                this.checked = true;
            });
        } else {
            $('.input-checkbox').each(function () {
                this.checked = false;
            });
        }
    });

    $('#select_action').change(function (e) {
        ids = [];
        var option = $(this).val();

        $('.input-checkbox:checked').each(function () {
            ids.push(parseInt($(this).val()));
        })

        console.log(ids);

        if (option != '#') {
            sendAjax(option);
        }
    });

    function sendAjax(option) {
        $.ajax({
            data: {
                option: option,
                ids: ids
            },
            dataType: 'json',
            type: 'post',
            url: Routing.generate('wi_admin_newsletter_recipient_group_multiple'),
            success: function (response) {
                window.location.reload();
            }
        });
    }
});
