import { tpl } from './miniAppsTpl';
import { Messager } from '@core/component/msgDistribute';
import {fetch} from '@core/component/httpClient';
import Message from '@core/component/message';
import LinkTool from '../../linkTool';
import SLD from '@core/util/domain';
const MiniApps = (($) => {
    
    const DefaultConfig = {
        tpl
    };
    
    
    class MiniApps {
        
        constructor(config) {
            
            this._config = this._getConfig(config);
            this.el = this._config.node;
            this.$el = $(this.el);
            this._renderDom();
            this._config.onDomUpdate();
            this._addEvent();
        }
        
        
        _addEvent() {
            
            let _this = this;
            
            
            this.linkChangeKey = Messager.on('linkChange', (req, res) => {
                this.$el.find('.J_miniAppsLink').val(res.link)
            })
            
            
            
            this.$el.on('input.miniAppsLink change.miniAppsLink', '.J_miniAppsLink', function() {
                let input = $(this);
                let link = input.val();
                let linkType = LinkTool.Type.MiniApps;
                let originalData = link;
                Messager.trigger('linkChange', { link, linkType, originalData });
            })
            
            
            this.$el.on('click.miniAppsLink', '.J_ok', async () => {
                let url = $('.J_linkInput').val();
                if (url === '') {
                    errorHint('链接不能为空');
                    return false;
                }
                url = url.replace(/https:\/\/|\/\/|http:\/\//g, '').trim();
                let reg = /^pages/;
                if(!reg.test(url)){
                    errorHint('请输入合法的链接');
                }else{
                    this.$el.find('.J_miniAppsLink').val(url).trigger('keyup');
                    Messager.trigger('confirm');
                }
                
                function errorHint(msg) {
                    $('.J_miniAppsLink').addClass('cd-error');
                    $('.J_errorMsg').text(msg);
                }
            })
            
            this.$el.on('focus', '.J_miniAppsLink', function() {
                $(this).removeClass('cd-error');
            })
            
            
            this.$el.on('click.miniAppsLink', '.J_cancel', function() {
                Messager.trigger('cancel');
            })
            
        }
        
        
        _getConfig(config) {
            return $.extend(true, {}, DefaultConfig, config);
        }
        
        
        _renderDom() {
            let config = this._config;
            let html = template.render(config.tpl, config);
            this.$el.html(html);
            
        }
        
        
        dispose() {
            Messager.off(this.linkChangeKey);
            this.$el.off('click.miniAppsLink').off('linkChange.miniAppsLink').off('change.miniAppsLink')
        }
    }
    
    return MiniApps;
    
})($);
export default MiniApps;