class ModuleBase {
    Opts = {
        testMode: false,
        CSSSelectorContainer: '',
        CSSSelectorForm: '',
        token: null,
        URL: {
            getDataTableContent: null,
            checkReadyToSend: null,
            index: null,
            show: null,
            edit: null,
            create: null,
            store: null,
            update: null,
            destroy: null,
        },
        dataTable: {
            orderDirective: [1, 'asc'],
            CSSSelector: null,
            advanceSearchParameter:{}
        },
    };
    data = null;
    root = this;

    constructor(options, data) {
        this.Opts.CSSSelectorContainer = '.container' + this.constructor.name;
        this.Opts.CSSSelectorForm = '.' + this.constructor.name + 'Form';
        this.Opts.dataTable.CSSSelector = this.Opts.CSSSelectorContainer + ' .dataTable';
        this.root = this;
        if (data !== "undefined" || data != null) {
            this.data = data;
        }
        if (options != null) {
            $.extend(this.Opts, options);
        }
    }

    init(options, data) {
        if (data !== "undefined" || data != null) {
            this.data = data;
        }
        if (options != null) {
            $.extend(this.Opts, options);
        }
    }

    datatableRowActionButtonClick(event,rootObj,options) {
        debug('call ModuleBase.datatableRowActionButtonClick');
        debug(rootObj);
        let node = $(event[0].currentTarget);
        let row = $(rootObj.Opts.dataTable.CSSSelector).DataTable().row(node.parents('tr'));
        let rowData = row.data();
        let actionName = 'datatableRowAction' + node.data('buttontype').capitalize();
        if (typeof rootObj[actionName] === "function") {
            return rootObj[actionName](rowData, node)
        }
    }

    datatableRowActionAttachment(rowData,node,options) {
        debug('call ModuleBase.datatableRowActionAttachment ');
        let fileName='downloadedFile';
        if(typeof options.defaultFileName!== "undefined"){
            fileName=options.defaultFileName;
        }
        if(rowData.fileUrl){
            if(rowData.fileName){
                fileName=rowData.fileName;
            }
            $.ajax({
                url: rowData.fileUrl,
                method: 'GET',
                xhrFields: {
                    responseType: 'blob'
                },
                success: function (data) {
                    let a = document.createElement('a');
                    let url = window.URL.createObjectURL(data);
                    a.href = url;
                    a.download = fileName;
                    document.body.append(a);
                    a.click();
                    a.remove();
                    window.URL.revokeObjectURL(url);
                }
            });

        }else{
            Toast.create("Hiba!", 'Nincs csatolt fájl!', TOAST_STATUS.DANGER, 10000);
        }

    }

    datatableRowActionView(rowData,node,rootObj,options) {
        debug('call ModuleBase.datatableRowActionView');
        let ajaxUrl = rootObj.Opts.URL.show.replace('%id%', rowData.id);
        /*
                console.log(Opts.URL);
        */
        console.log(ajaxUrl);
        APP.loadMainContent(ajaxUrl, rootObj.initViewShow);
        console.log('ModuleBase.datatableRowActionEdit End');
    }

}
window.ModuleBase=ModuleBase;
