var isSubmit = true;
$(document).ready(function(){

	if ($('#certificateForm').length>0){
		$('#certificateForm').validate({
			
			rules : {
				"name" : {
					required : true					
				}
				
				
				
			},
			messages:{
				"name" : {
					required : "不能为空"					
				}

			},

	        
	        
	        submitHandler: function(form){  
	        	certificate.save(form);
	        }
	        
	        
	        
		});
		
		
	}
	

	
	
	
	
	
})

var certificate = {
	
	remove : function(id){
		
		bootbox.confirm({
			buttons: {
				cancel: {
					label: '取消',
					className: 'btn-default'
				},
				confirm: {
					label: '确认',
					className: 'btn-myStyle'
				}
				
			},
			message: '请确认是否删除?',
			callback: function(result) {
				if(result) {
					$.ajax({
						  type: 'GET',
						  url: webPath + '/api/certificate/del?id='+id,
						  dataType:'json',
						  success: function(data){
							  window.location.reload();
						  }
					})
				} else {
					
				}
			}
		    });
		
	},
	save : function(form){
		if (isSubmit){
			isSubmit = false;
						
			var formData = new FormData(); 
			formData.append('file', $('#input_file')[0].files[0]);
			formData.append('id', $('#id').val());
			formData.append('name', $('#name').val());
			formData.append('status', $('#status').val());
			formData.append('uploadImage', $('#uploadImage').val());
			if (typeof($('#position').val()) != "undefined" ){
				formData.append('position', $('#position').val());
			}
			

			$.ajax({
			    url: webPath + '/api/certificate/save',
			    type: 'POST',
				cache: false,
				data: formData,
				processData: false,
				contentType: false,
				dataType:"json",
				beforeSend: function(){
				    
				},
				success: function(data){
				  var status=data.status;
				  var entity = data.certificate;
				  
				  if(status==1){//成功
						bootbox.dialog({
							message:"上传证书成功",
								buttons:{
									"success":{
										"label":"下一步",
										"className":"btn-sm btn-primary",
										"callback": function () {
											window.location.href = webPath + '/certificate/add?id=' + entity.id;
				                        }
									}
						
								}
							});
						
					} else if (status == 2){
						isSubmit = true;
						bootbox.dialog({
							message:"修改成功",
								buttons:{
									"success":{
										"label":"确定",
										"className":"btn-sm btn-primary",
										"callback": function () {
											window.location.reload();
				                        }
									}
						
								}
							});
						
						
					}
				  
				  
				}
			});
		}
			
	},
	editpic : function() {
		var jsonstr = [];
		if ($("#position").val() != ""){
			jsonstr = JSON.parse($("#position").val());
		}
		
		window.c = imageLabel({
            img: $("#pic").attr("src"),
            editPop: true,
            data:jsonstr,
            close: function(t) {
                return true;
            },
            clickArea: function() {},
            edit: function(t) {},
            startArea: function() {},
            confirm: function(t) {
            	//alert(JSON.stringify(t))
            	if ( t != null ){
            		$("#position").val(JSON.stringify(t));
            	}
                //return t.length && alert(JSON.stringify(t)), true
            	return true;
            }
        })
		
	},
	test : function(id) {
		$.ajax({
		    url: webPath + '/certificate/test?id='+id,
		    type: 'GET',
			cache: false,
			processData: false,
			contentType: false,
			dataType:"json",
			beforeSend: function(){
			    
			},
			success: function(data){
				if ("success" == data.status){
					var html = '<img src="'+webPath+'/wx/rainbowplan/loadResource?imageName=\\mdp\\certificate\\test\\test.png" height="100%"/>';
					layer.open({
						  type: 1,
						  skin: 'layui-layer-rim', //加上边框
						  area: ['auto', '100%'], //宽高
						  content: html
						});
				}
			}
		})
	}
	
}


















