(function(KDApi, $){

	var valueControlList = ['valueBooleanSelectDiv', 'valueTextDiv', 'valueNumberDiv', 'valueEnumSelectDiv', 'valueDateDiv'];
	
    function TargetResult (model) {
        this._setModel(model)
    }

    TargetResult.prototype = {
        _setModel: function(model) {
            this.model = model;
			model.pageDom = $("#" + model.pageId);
        },
        init: function(props){
            initFunc(this.model, props)
        },
        update: function(props){
            updateFunc(this.model, props)
        },
        destoryed: function(){
        }
    }

    // Other Code
    var initFunc = function(model, props) {
        // KDApi.loadFile可以通过路径加载js或css文件，并且在html文件头生成script或者link标签，第一个参数是路径，第二个参数是model，第三个参数是加载完成后执行的回调函数
		KDApi.loadFile('./css/targetresult.css', model, function() {
			// 后端插件通过setData传给前端的数据，前端可以通过props.data去获取
			var text = props.data && props.data.text || ''
			// 通过路径去获取html字符串，第一个参数是路径，第二个参数是model，第三个参数是HTML模板中变量的值
			KDApi.getTemplateStringByFilePath('./html/targetresult.html', model, {
				text: text
			}).then(function(data) {
				model.dom.innerHTML = data
				// 绑定DOM事件
				initEvent(model, props)
				
				model.invoke('initControlDataEvent', {controlKey : model.key});
			})        
		})
    }
	
	//事件绑定
    var initEvent = function(model, props){	
	
		$(model.dom).on('keydown', '.valueDate:visible', function(){
			return false;
		});
		
		$(model.dom).on('keydown', '.valueNumber:visible', function(e){
			var code = e.keyCode;
			//只允许输入数字和小数点
			if(!((code >= 48 && code<=57) || code== 190 || (code>=96&&code<=105) || code==110 || code==8 || code==37 || code==39 || code==46)){
				return false;
			}
		});
		
		$(model.dom).on('change', '.valueBoolean:visible', function(){	
			setValue(model, $(this).val());
		})
		
		$(model.dom).on('change', '.valueEnum:visible', function(){	
			setValue(model, $(this).val());
		})
		
		$(model.dom).on('blur', '.valueNumber:visible', function(){
			var value = $(this).val();
			value.replace(/^0*/, "").replace(/^0*\./, "0.");
			$(this).val(value)
			setValue(model, value);
		})
		
		$(model.dom).on('blur', '.valueText:visible', function(){
			setValue(model, $(this).val());
		})
		
		$(model.dom).on('click', '.valueSearch:visible', function(){
			model.invoke("openParamF7Event", {controlKey : model.key});	
        })
		
		var key = model.key.replace("control","");			
		$(model.dom).on('click', '.valueDateDiv:visible', function(){
			$("#" + key + "date", model.pageDom).find("input").click();
			
			var top = $(this).offset().top;
			var left = $(this).offset().left;
			setTimeout(function(){
				if(document.body.scrollHeight < top + 28 + $(".j-left").parent().height()){
					$(".j-left").parent().css("top", top - $(".j-left").parent().height());
				}else{
					$(".j-left").parent().css("top", top + 28)
				}
				$(".j-left").parent().css("left", left)
				$(".j-left").attr("id", model.pageId  + key + "date")
			}, 50);
		});		
		
		$("#kd-theme").on("click", '#' + model.pageId + key + "date .rdtDay", function(){
			setTimeout(function(){	
				$("#" + key + "date", model.pageDom).find("input").blur(); 
			}, 100);
		})
    }

    var updateFunc = function(model, props) {		
		if(props.data){	
			if(props.data.type){
				showControlByType(model, props.data);
			}			
			if(props.data.value !== "undefined"){//只要传了，就可以更新
				updateValue(model, props.data);
			}			
		}
    }	
	
	var clearValue = function(model){
		$(model.dom).find("select:visible").val('');
		$(model.dom).find("input:visible").val('');
	}
	
	var showControlByType = function(model, data){
		clearValue(model);
		switch(data.type) {
			case "enum":
				showEnumSelectDiv(model, data.enumList);				
				break;
			case "string":
				showTextDiv(model, data.F7);
				break;
			case "number":
				showNumberDiv(model);
				break;
			case "date":
				showDateDiv(model);
				break;
			case "boolean":
				showBooleanSelectDiv(model);
				break;
			default:
				showTextDiv(model, data.F7);
		} 
	}
	
	var showNumberDiv = function(model){
		showControl(model, 'valueNumberDiv');
	}
	
	var showTextDiv = function(model, flag){
		showControl(model, 'valueTextDiv');
		if(flag){							
			$(model.dom).find(".valueSearch").show();
			$(model.dom).find(".valueText").attr("disabled", true);
		}else{
			$(model.dom).find(".valueSearch").hide();
			$(model.dom).find(".valueText").attr("disabled", false);
		}
	}
	
	var showBooleanSelectDiv = function(model){
		showControl(model, 'valueBooleanSelectDiv');
		updateValue(model, {value : "true"});
	}
	
	var showEnumSelectDiv = function(model, enumList){
		showControl(model, 'valueEnumSelectDiv');
		createValueEnumControl(model, enumList);
	}
	
	var showDateDiv = function(model){
		showControl(model, 'valueDateDiv');
	}	
	
	var showControl = function(model, controlKey){
		$.each(valueControlList, function(i, valueControlKey){
			if(valueControlKey == controlKey){
				$(model.dom).find('.' + valueControlKey).show();
			}else{
				$(model.dom).find('.' + valueControlKey).hide();
			}
		});	
	}
	
	var createValueEnumControl = function(model, enumList){
		$(model.dom).find('.valueEnum').html('');
		$.each(enumList,function(i, obj){
			$(model.dom).find('.valueEnum').append('<option value ="' + obj.value + '">' + obj.name.zh_CN + '</option>');
		});
	}
	
	//更新值
	var updateValue = function(model, data){
		$(model.dom).find("select:visible").val(data.value);		
		$(model.dom).find("input:visible").val(data.value);		
	}	
	
	var setValue = function(model, date){
		model.invoke("setValueEvent", {controlKey : model.key, value : date});
	}	
	
    KDApi.register('targetresult', TargetResult)
})(window.KDApi, jQuery)
