//违纪卷
var tagInfo = function(option, success) {
    var object = new TagInfo(option);
    success();
    return object;
}

function TagInfo(option) {
    this.markControl = option.markControl;
    this.url = option.url;
    this.context = option.markControl.context;
    this.init();
    this.markControl.on('task.get.before', this, function(event, context, eventObject) {
        this.task = undefined;
        this.reset();
    });
    this.markControl.on('task.get.success', this, function(event, context, eventObject) {
        this.task = context.task;
        this.reset();
    });
    this.markControl.on('task.get.none', this, function(event, context, eventObject) {
        this.reset();
    });
    this.markControl.on('task.get.error', this, function(event, context, eventObject) {
        this.reset();
    });
}

TagInfo.prototype.init = function() {
    this.container = getDom(this.container_dom, this.markControl).appendTo(this.markControl.container.assistant);
    this.container.list = this.container.find('#type-info-list');
    this.container.title = this.container.find('.popover-title');
    this.popover = getDom(this.popover_dom, this.markControl);
    this.popover.remarkInput = this.popover.find('input.remark-input');
    this.popover.submitButton = this.popover.find('a.btn');
    this.popover.cancelButton = this.popover.find('p.image-close');
    this.popover.appendTo(this.markControl.container);
    var self = this;
    $.post(this.url, function(result) {
        for (var i = 0; i < result.length; i++) {
            var button = getDom(self.button_dom, self.markControl).appendTo(self.container.list);
            button.attr('data-value', result[i].value);
            button.html(result[i].name);
        }

        self.container.list.find('.tag-button').click(function(event) {
        	self.currButton = $(this);
            $(this).toggleClass('curr');
            if($(this).hasClass('curr')){
            	self.toggle(true,$(this));
            }else{
            	self.toggle(false,$(this));
            }
        });
        
        self.popover.submitButton.click(function() {
        	var remark = self.popover.remarkInput.val();
        	if (remark.length > 20) {
                self.popover.find('i.remark').html('长度不能超20字');
                return false;
            }
        	self.popover.find('i.remark').html('');
        	self.currButton.attr('data-remark',remark);
        	self.toggle(false);
        });
        
        self.popover.cancelButton.click(function() {
        	self.toggle(false);
        });
    });
}

TagInfo.prototype.reset = function() {
    //this.container.list.find('.tag-button').removeClass('curr');
	var self = this;
    if (this.task != undefined) {
    	if(this.task.tagInfoList!=undefined){
    		this.container.list.find('.tag-button').each(function(index, obj) {
    			var value = $(obj).attr('data-value');
    			for (var i = 0; i < self.task.tagInfoList.length; i++) {
    				var tagInfo = self.task.tagInfoList[i];
    				if(tagInfo.value == value){
    					$(obj).addClass('curr');
    					$(obj).attr('data-remark',tagInfo.remark);
    					break;
    				}else{
    					$(obj).removeClass('curr');
    				}
    			}
    		});
    	}else{
    		this.container.list.find('.tag-button').each(function(index, obj) {
    			$(obj).removeClass('curr');
    			$(obj).removeAttr('data-remark');
    		});
    	}
        this.container.show();
    } else {
        this.container.hide();
    }
}

TagInfo.prototype.onTypeSelect = function() {
	var self = this;
    if (this.task != undefined) {
        var array = [];
        this.container.list.find('.tag-button').each(function(index, obj) {
            if ($(obj).hasClass('curr')) {
            	 var tagObj = {
                         value: $(obj).attr('data-value'),
                         remark: $(obj).attr('data-remark'),
                     }
                array.push(tagObj);
            }
        });

        if (array.length > 0) {
            this.task.tagInfoList = array;
        } else {
            this.task.tagInfoList = undefined;
        }
    }
}

TagInfo.prototype.toggle = function(enable,button) {
	var self = this;
    if (enable == true) {
    	self.enable = true;
    	var remark = button.attr('data-remark');
    	self.popover.remarkInput.val(remark);
    	self.popover.show();
        self.context.listenKeyboard = false;
    } else {
    	self.enable = false;
    	self.popover.hide();
        self.context.listenKeyboard = true;
    }
    self.onTypeSelect();
}

TagInfo.prototype.container_dom = '<h3 class="popover-title">违纪标注</h3>\
<div class="popover-content"><p id="type-info-list" class="popover-list">\
</p></div>';

TagInfo.prototype.button_dom = '<a class="tag-button" href="#"></a>';

TagInfo.prototype.popover_dom = '<div class="message-popover" style="display:none"><div class="popover-header">\
	<p class="title">违纪标注</p><p class="image-close"><img src="{staticServer}/mark-new/images/images-close.png" /></p></div>\
	<div class="popover-cont"><input class="remark-input" placeholder="请输入备注" type="text"/>\
	<i class="wrong remark"></i></div>\
	<a href="#" class="btn btn-small btn-info text-userInfo">确定</a>\
	</div>';