(function (window, $, swal) {
    'use strict';

    window.TemplateEngineApp = function ($wrapper) {
        this.$wrapper = $wrapper;

        this.$wrapper.on(
            'click',
            '.js-swal-faq',
            this.handleShowFaqListe.bind(this)
        );

        this.$wrapper.on(
            'click',
            '.js-swal-template',
            this.handleShowSwalTemplate.bind(this)
        );
    };


    $.extend(window.TemplateEngineApp.prototype, {

        handleShowSwalTemplate: function (e) {
            e.preventDefault();

            let id = $(e.currentTarget).data('swal_templateid');

            var formData = {};
            formData['action'] = 'get-swal-template-html';
            formData['id'] = id;

            var self = this;
            $.ajax({
                method: 'POST',
                url: '/ajaxphp-2015/template.php',
                data: JSON.stringify(formData)
            }).then(function(data) {

                swal.fire({
                    width: '90%',
                    html: data.html,
                    focusConfirm: false
                });
            });
        },

        handleShowFaqListe: function (e) {
            e.preventDefault();

            let id = $(e.currentTarget).data('swal_faqid');
            let data = {};

            var formData = {};
            formData['action'] = 'get-faq-liste';
            formData['id'] = id;

            var self = this;
            $.ajax({
                method: 'POST',
                url: '/ajaxphp-2015/template.php',
                data: JSON.stringify(formData)
            }).then(function(data) {

                let html = '<div class="container">';
                let faq_titel = '';
                let root = data.root;
                $.each(data.data, function (key, values) {

                    html += '<div class="row my-3 text-left">' +
                        '<div class="col-12 font-weight-bold">' + values.frage + '</div>' +
                        '<div class="col-12">' + values.antwort;

                    if (values.verlinkung !== 'undefined' && values.verlinkung !== '') {
                        if (values.verlinkung_text !== 'undefined' && values.verlinkung_text !== '') {
                            html += ' <a class="text-info font-weight-bold" target="_blank" ' +
                                'href="' + root + values.verlinkung + '">' + values.verlinkung_text + '</a>';
                        }
                    }

                    html += '</div></div>';
                    faq_titel = values.faq_titel;
                });
                html += '</div>'
                swal.fire({
                    title: faq_titel,
                    width: '90%',
                    html: html,
                    focusConfirm: false
                });
            });
        },

    });


})(window, jQuery, swal);

$(document).ready(function () {
    var $wrapper = $('body');
    var templateEngineApp = new TemplateEngineApp($wrapper);

});
