(function ($) {
    'use strict';    
    
    $(document).ready(function () {
        $(document).find('.ays_survey_user_history').DataTable({
        	paging: 5,
            "bDestroy": true,
            "lengthMenu": [[5, 10, 25, 50, 100, -1], [5, 10, 25, 50, 100, surveyLangDataTableObj.all]],
            "language": {
                "sEmptyTable":     surveyLangDataTableObj.sEmptyTable,
                "sInfo":           surveyLangDataTableObj.sInfo,
                "sInfoEmpty":      surveyLangDataTableObj.sInfoEmpty,
                "sInfoFiltered":   surveyLangDataTableObj.sInfoFiltered,
                "sInfoPostFix":    "",
                "sInfoThousands":  ",",
                "sLengthMenu":     surveyLangDataTableObj.sLengthMenu,
                "sLoadingRecords": surveyLangDataTableObj.sLoadingRecords,
                "sProcessing":     surveyLangDataTableObj.sProcessing,
                "sSearch":         surveyLangDataTableObj.sSearch,
                "sUrl":            "",
                "sZeroRecords":    surveyLangDataTableObj.sZeroRecords,
                "oPaginate": {
                    "sFirst":    surveyLangDataTableObj.sFirst,
                    "sLast":     surveyLangDataTableObj.sLast,
                    "sNext":     surveyLangDataTableObj.sNext,
                    "sPrevious": surveyLangDataTableObj.sPrevious,
                },
                "oAria": {
                    "sSortAscending":  surveyLangDataTableObj.sSortAscending,
                    "sSortDescending": surveyLangDataTableObj.sSortDescending
                }
            }
        });

         // for details
	    $.fn.aysModal = function(action){
	        var $this = $(this);
	        switch(action){
	            case 'hide':
	                $(this).find('.ays-modal-content').css('animation-name', 'zoomOut');
	                setTimeout(function(){
	                    $(document.body).removeClass('modal-open');
	                    $(document).find('.ays-modal-backdrop').remove();
	                    $this.hide();
	                }, 250);
	                break;
	            case 'show':
	            default:
	                $this.show();
	                $(this).find('.ays-modal-content').css('animation-name', 'zoomIn');
	                $(document).find('.modal-backdrop').remove();
	                $(document.body).append('<div class="ays-modal-backdrop"></div>');
	                $(document.body).addClass('modal-open');
	                break;
	        }
	    }

         // Modal close
         $(document).find('.ays-close').on('click', function () {
            $(this).parents('.ays-modal').aysModal('hide');
        });

        $(document).on("keydown", (event) => {
            if (event.key === 'Escape') {
                if($("#show-detailed-info").css('display') != 'none' && $("#show-detailed-info .ays-preloader").css('display') == 'none') {
                    var modal = $("#show-detailed-info");
                    modal.aysModal('hide');
                    $(document).find( '.ays-survey-show-submission-button').blur();
                }
            }
        });

        //removes id dublicates
        if($(document).find('div#show-detailed-info').length > 1){
            $(document).find('div#show-detailed-info').each(function () {
                $('[id="' + this.id + '"]:gt(0)').remove();
            });
        }

        $(document).find('div#show-detailed-info').appendTo($(document.body));
        
        $(document).on('click','.ays-survey-show-submission-button',function(e){
			var $this = $(document).find( '.ays-survey-show-submission-button' );

			var $this = $(this);
			var modal = $('#show-detailed-info');
			
			modal.find('.ays-preloader').css({
				'display' : 'flex',	
			});

			modal.find('.ays-modal-body').html("");
			modal.find('.ays-modal-header h2').html("");

			modal.aysModal('show');

            var survey_id = $this.attr('data-survey-id');
			var submission_id = $this.attr('data-id');
			var user_id = $this.attr('data-user-id');
            var ajaxData = {};
            ajaxData.action = 'ays_survey_popup_submission';
            ajaxData.function = 'ays_survey_popup_submission';
            ajaxData.survey_id = survey_id;
            ajaxData.submission_id = submission_id;
            ajaxData.user_id = user_id;

            $.ajax({
                url: window.aysSurveyMakerAjaxUserHistory.ajaxUrl,
                data: ajaxData,
				dataType: "json",
				method: 'POST',
                success: function(response){
                    if(response.status){
                        modal.find('div.ays-preloader').css('display', 'none');
                        modal.find('.ays-modal-body').html(response.content);
                        modal.find('.ays-modal-header h2').html(response.title);
                    }
                    else{
					    modal.aysModal('hide');
                    }

                },
				error: function(response){
					modal.aysModal('hide');
				}
            });
        });

        $(document).on('click', 'body', function(e){
            if($(e.target).hasClass('ays-modal')){
                $(e.target).find(".ays-close").trigger("click");
            }
        });

    });
    
})(jQuery);