import Tab from '@core/component/tab';
import Tooltips from '@core/component/toast/tooltips';
import Form from './form';
import BlockingElements from './blockingElements';
class MultipleForm {
    constructor(config) {
        
        this.formInstance = [];
        
        this.tabInstance = null;
        
        this.deftTabNum = 3;
        
        this.isSDK = false;
        this.setConfig(config);
        this.init();
    }
    init() {
        this.getSystem();
        this.getModuleByDomData();
        this.blockingEvents();
    }
    
    getSystem() {
        this.isSDK = true
    }
    
    renderTab(tabOpts) {
        if (!this.tabInstance && tabOpts && tabOpts.tabs.length) {
            this.tabInstance = new Tab(tabOpts);
        }
    }
    
    changeTitle() {
        $('.J_moduleConf .J_titleBar .J_title').html(
            this.tabInstance.tabsScroll
                .find('.J-item.d-active')
                .attr('data-title')
        );
    }
    
    getModuleByDomData() {
        let domData = this.config.domData;
        if (!domData) {
            throw new Error('domData is illegal');
        }
        let tabOpts = [];
        $.each(domData, (index, item) => {
            let tabItem = {};
            tabItem['title'] = item.title;
            let formInstance = this.initForm({
                centerModules: this.config.centerModules,
                module: this.config.module || {},
                moduleData: this.config.moduleData,
                domData: item,
                isSDK: this.isSDK,
                asyncHandler: this.blockingEvents,
                skin: this.config.skin
            });
            this.setFormInstance(formInstance);
            tabItem['content'] = formInstance.formContent;
            tabOpts.push(tabItem);
        });
        this.renderTab({
            node: this.config.node,
            size: this.deftTabNum,
            tabs: tabOpts,
            afterToggle: () => {
                this.changeTitle();
                this.blockingEvents();
            }
        });
    }
    
    setConfig(config) {
        this.config = $.extend(true, {}, config);
    }
    
    initForm(opts) {
        return new Form(opts);
    }
    
    blockingEvents() {
        
        console.log('multipleForm')
        if (window.page_data.isSDKM || !this.isSDK) return;
        for (let key in BlockingElements) {
            let nodeObj = BlockingElements[key];
            if (!$.isEmptyObject(nodeObj)) {
                let tips = nodeObj.tips;
                if (tips) {
                    let tipsNode = $(tips.elem);
                    tipsNode.attr({
                        'data-tooltips': '',
                        'data-content': tips.content,
                        'data-direction': tips.direction
                    });
                }
                
                if (nodeObj.preventDefault) {
                    $(key).removeAttr('type');
                }
                
                let elemEvents = null;
                $(key).each((i, el) => {
                    let node = $(el);
                    elemEvents = $._data(node[0], 'events');
                    if (elemEvents) {
                        for (let evtKey in elemEvents) {
                            node.off(evtKey);
                        }
                    } else {
                        
                        node.on('click', event => {
                            event.stopPropagation();
                        });
                    }
                });
                new Tooltips();
            }
        }
    }
    
    setFormInstance(formInstance) {
        if (formInstance instanceof Form) {
            this.formInstance.push(formInstance);
        }
    }
    
    getData(index) {
        return new Promise(resolve => {
            let promiseArr = [];
            $.each(this.formInstance, (i, instance) => {
                const moduleData = instance.getData();
                if (index === undefined) {
                    promiseArr.push(Promise.resolve(moduleData));
                } else {
                    if (index == i) {
                        promiseArr.push(Promise.resolve(moduleData));
                    }
                }
            });
            Promise.all(promiseArr).then(data => {
                let result = data.reduce((acc, val) => {
                    return $.extend(acc, val);
                }, {});
                resolve(result);
            });
        });
    }
    dispose() { }
}
export default MultipleForm;