$(function(){
	
 	  $(document).ready(function () {
		
		
		  $(".btnUpload").height($(".btnUpload").width()) 
		  	var getspan=$(".imguploadspan").html();
			$(".imguploadspan").append(getspan)
			$(".imguploadspan").append(getspan)
			
			var list=$(".imguploadlist").html();
			$(".imguploadlist").append(list);
			$(".imguploadlist").append(list);
			
			var obj=window.localStorage.getItem("postmoment")
			if(obj){
				obj=JSON.parse(obj)
				$("[name=Project_title]").val(obj.title)
				$(".Projectdescriptiontxt").val(obj.txt)
				for(var i in obj.list){
					var input=$(".btnUpload").eq(i)
					onloadimg(input,obj.list[i])
				}
				$(".Publish").hide()
				$(".Renew").show()
				
				//onloadimg
			}
			
			
		   var btnUpload=null;
	    $('.btnUpload').click(function () {
			if(bl){
				$('#imagePic').click();
				btnUpload=$(this);
			}else{
				bl=true
			}
	    
	    });
		var bl=true;
		$('.imgdel').click(function () {
			bl=false
			$(this).parent().css("background", "url(/lanhu-bootstrap/assets/img/img_add.png) no-repeat center")
			$(this).parent().css("border", '0')
			$(this).parent().css("background-size", "100% 100%")
			
			$(this).hide()
			$(this).parent().removeAttr("disabled")
			
			$(this).parent().find(".imginput").val("")
			
		});
		
	    $("#imagePic").on("change", function (e) {
			
	      var file = e.target.files[0]; //获取图片资源
	      var fileTypes = ["bmp", "jpg", "png", "jpeg"];
	      var bTypeMatch = false
	      for (var i = 0; i < fileTypes.length; i++) {
	        var start = file.name.lastIndexOf(".");
	        var fileType = file.name.substring(start + 1);
	        if (fileType.toLowerCase() == fileTypes[i]) {
	          bTypeMatch = true;
	          break;
	        }
	      }
	      if (bTypeMatch) {
	        if (file.size <= 1024 * 1024 * 10) {
	          var reader = new FileReader();
	          reader.readAsDataURL(file); // 读取文件
	          // 渲染文件
	          reader.onload = function (arg) {
				 onloadimg(btnUpload,arg.target.result)
	          $("#imagePic").val("")
	          }
	        } else {
				jpalert($(".head_10M").val(),null,"alert");
	     
 	       	$("#imagePic").val("")
	          // btnUploadText = '上传'
	          // $("#uploadButton").text(btnUploadText)
	          return false;
	        }
	      } else {
	       
			jpalert($(".head_jpgpng").val(),null,"alert");
			
			 	$("#imagePic").val("")
	        // btnUploadText = '上传'
	        // $("#uploadButton").text(btnUploadText)
	        return false;
	      }
	    });
	  })
	//---------初始化数据部分-----------
	
	//---------初始化数据部分end-----------
	//页面初始化
	function onloadimg(btn,img){
		$(btn).css("background", "url("+img+")   no-repeat center")
		$(btn).css("background-size", "100% 100%")
		
		$(btn).css("border", '1px solid #b8b9ba')
		$(btn).css("border-radius",'10px')
		$(btn).attr("disabled","disabled")
		$(btn).find(".imgdel").show()
		$(btn).find(".imginput").val(img)
		
	}
	
		$.validator.setDefaults({
		    submitHandler: function() {
				var bl=true;
				$(".imginput").each(function(){
					if($(this).val()!=''){
						bl=false;
						return
					}
				});
				
				var success = $(".head_success").val();
				var warning = $(".head_warning").val();
				var success1 = $(".signin_success").val();
				var warning1 = $(".signin_warning").val()
				
				
		       if(bl){
				   jpalert(warning1,null,"alert");
				   
				
		       }else{
				  zeroModal.success({
				  	unique: '1',
				  	opacity: 0.8,
				  	content: success,
				  	contentDetail: success1,
				  	width: '300px',
				  	height: '280px',
				  	//okTitle: headcommon2,
				    ok:false,
				  	okFn: function() {
				  
				  		window.location.href = '../moments/index.html'
				  	},
				  	onClosed: function() {
				  		; //location.href='../signin/signin.html'
				  	},
				  	onComplete: function(evn) {
				  		submitCompany()
				  		console.log('onComplete')
				  		setTimeout(function(){
				  		window.location.href = '../moments/index.html'
				  			zeroModal.close('1');
				  		},3000);
				  	}
				  })
			   }
		    }
		});
		$("#form").validate()
		function submitCompany(){
			debugger
			var company=window.localStorage.getItem("loginCompany")
			company=JSON.parse(company)
			
			var time=(1900+new Date().getYear())+"."+(new Date().getMonth()+1)+"."+new Date().getDate()
			var postmoment={
				title:$("[name=Project_title]").val(),
				txt:$(".Projectdescriptiontxt").val(),
				date:time,
				list:[],
				name:company.companyName,
				logo:company.logo,
			}
			$(".imginput").each(function(){
				var value=$(this).val()
				if(value){
					postmoment.list.push(value)
				}
			})
			window.localStorage.setItem("postmoment",JSON.stringify(postmoment))
			
		}
	
		
		
	//页面初始化end
})