import './style.scss';
const PowerForm = ( $ => {
    class PowerForm{
        constructor(el, conf){
            let $el = $(el);
            this.$el = $el;
            this.conf = conf;
            this.nodeName = $el.get(0).nodeName;
            if(!this.$el.data('powerForm')){
				this.init(el, conf);
				this.$el.data('powerForm', this);
			}
        }
        init(){
            this.maxLength = parseInt(this.$el.attr('maxlength'));
            let $count = $('<span class="cd-input-count"><em class="js-textNum">'+ this.maxLength + '</em>/' + this.maxLength+'</span>');
            if(this.maxLength > 0){
                this.$el.after($count).parent().css({'position': 'relative'});
                $count = this.$el.nextAll('.cd-input-count');
                if(this.nodeName == 'TEXTAREA'){
                    this.$el.css({'paddingBottom': $count.outerHeight() + 5 + 'px'});
                    let _top = this.$el.position().top + this.$el.outerHeight() - $count.outerHeight() - 7;
                    $count.css({'right': '5px', 'top': _top + 'px'});
                }
                if(this.nodeName == 'INPUT'){
                    let outWidth = $count.outerWidth() + 5;
                    this.$el.css({'paddingRight': outWidth + 'px'});
                    let _martop = this.$el.position().top + (this.$el.outerHeight() - $count.outerHeight()) / 2;
                    $count.css({'right': '5px', 'top': _martop + 'px'});
                }
                this.bindEvent();
            }
        }
        bindEvent(){
            let self = this;
            this.$el.nextAll('.cd-input-count').find('.js-textNum').text(this.$el.val().length);
            this.$el.on('input change', function(){
                let _len = $(this).val().length;
                $(this).removeClass('cd-error');
                $(this).nextAll('.cd-input-count').find('.js-textNum').text(_len);
                if(self.$site) self.$site.find('.cd-embed-error').hide();
            })
        }
        error(){
            let content = arguments.length > 1 ? arguments[1] : arguments[0];
            let type = arguments.length > 1 ? arguments[0] : 'block';
            if(!this.$el.hasClass('cd-error')) this.$el.addClass('cd-error');
            if(content && content != ''){
                if(this.conf.msgParent){
                    if($(this.conf.msgParent).find('.cd-embed-error').length < 1){
                        $(this.conf.msgParent).append('<p class="cd-embed-error">'+content+'</p>');
                    }else{
                        $(this.conf.msgParent).find('.cd-embed-error').text(content).show();
                    }
                    this.$site = $(this.conf.msgParent);
                }else{
                    if(this.$el.nextAll('.cd-embed-error').length < 1){
                        this.$el.after('<p class="cd-embed-error">'+content+'</p>');
                    }else{
                        this.$el.nextAll('.cd-embed-error').text(content).show();
                    }
                    this.$site = this.$el.parent();
                }
                if(type == 'inline'){
                    this.$site.find('.cd-embed-error').addClass('cd-inline');
                }
                if(type == 'block'){
                    this.$site.find('.cd-embed-error').addClass('cd-block');
                }
            }
        }
        cleanError(){
            this.$el.removeClass('cd-error');
            if(this.$site) this.$site.find('.cd-embed-error').hide();
        }
        static Nodes = ['TEXTAREA', 'INPUT', 'SELECT'];
        static DEFAULTS = { msgParent: null };
    }
    $.fn.powerForm = function(opts) {
		this.each(function() {
            var $this = $(this),
                nodeName = this.nodeName,
                data = $this.data('powerForm'),
				conf = $.extend({}, PowerForm.DEFAULTS, typeof opts === 'object' && opts);
			if (!data && $.inArray(nodeName, PowerForm.Nodes) > -1){
                data = new PowerForm(this, conf);
            }
			if (typeof opts == 'string') data[opts]();
		});
		return this;
	};
    return PowerForm;
})($);
export default PowerForm;