<template>
    <div>
        <silnet-privacy-policy-popup v-show="canShow" v-bind:content-url="contentUrl" v-bind:widget-type="widgetType"></silnet-privacy-policy-popup>
    </div>
</template>

<script>
module.exports = {    
    //Parametr wymagany
    name: 'silnet-privacy-policy',

    //Props -> atrybuty przekazywane z zewnątrz, np: /<silnet-privacy-policy container-selector="" content-url="" widget-type=""></silnet-privacy-policy>
    props: {'containerSelector':null,'contentUrl':null,'showPopup':false,'widgetType':null},

    methods: {
        getDocumentLanguage: function(defaultLanguage) {
            if(document.querySelector('html') && document.querySelector('html').getAttribute('lang')) {
                return document.querySelector('html').getAttribute('lang').split('-')[0];
            }
            return (defaultLanguage) ? defaultLanguage : null;
        }
    },

    created:function() {
        if(this.containerSelector && this.contentUrl) {
            try {
                var domElement = document.querySelector(this.containerSelector);
                if(domElement) {
                    //Update language
                    var contentUrl = this.contentUrl;
                    contentUrl += contentUrl.match(/\?/) ? '&lang=' : '?lang=';
                    contentUrl += this.getDocumentLanguage('pl');

                    axios.get(contentUrl)
                        .then(function(response){
                            html = response.data;
                            var macros = html.match(/#@javascript:(.+?):javascript@#/g);
                            if(macros&&macros.length)  {
                                macros.forEach(function(r1){
                                    if(r1.match(/#@javascript:(.+):javascript@#/m)) {
                                        try {
                                            var r2 = r1.match(/#@javascript:(.+):javascript@#/m);
                                            var r3 = eval(r2[1].trim());
                                            html = html.replaceAll(r1, r3);
                                        } catch(e) {
                                        }
                                    }
                                });
                            }
                            domElement.innerHTML = html;
                        })
                        .catch(function(response){
                            domElement.innerHTML = 'Nieznany błąd (unknown error)...';
                        });
                }
            } catch(e) {
            }
        }
        return true;
    },

    computed: {
        canShow: function() {
            //Props może być stringiem (przekazanie w html)
            return (this.showPopup===true || String(this.showPopup).toLowerCase()==="true");
        }
    }
}
</script>

<style scoped>
    .h3 {
        margin:0;
        margin-bottom:0.5em;
        font-size:1.2275em;
    }
</style>