$(function(){
	
 	  $(document).ready(function () {
		  var companyList=["CompanyAAA","CompanyBBB","CompanyCCC"]
		  
			$(".ulboxinput").on('input propertychange',(function(){
				var value=$(this).val()
				if(value!=""){
					$(".ulbox").empty()
					
					var clist=companyList.filter(function(item){
						if(item.indexOf(value)>-1){
							var li=$("<li>"+item+"</li>")
							$(".ulbox").append(li);
							return true
						}else{
							return false
						}
					})
					if(clist.length){
						$(".ulbox").show()
					}else{
						
						var li=$("<li>测试数据 CompanyAAA CompanyBBB CompanyCCC</li>")
						$(".ulbox").append(li);
						$(".ulbox").show()
					}
				}else{
					$(".ulbox").hide()
				}
			}))
		
		  $(".btnUpload").height($(".btnUpload").width()) 
		  	var getspan=$(".imguploadspan").html();
			$(".imguploadspan").append(getspan)
			$(".imguploadspan").append(getspan)
			
			var list=$(".imguploadlist").html();
			$(".imguploadlist").append(list);
			$(".imguploadlist").append(list);
			
			if(window.location.search){
				var postproject =window.localStorage.getItem("postproject")
				if(postproject){
					postproject=JSON.parse(postproject)
					
					$(".Projectdescriptiontxt").val(postproject.txt)
					$("input[name=completion]").val(postproject.date)
					$(".showlistLocation1").val(postproject.showlistLocation1)
					$(".showlistLocation2").val(postproject.showlistLocation2)
					$(".showlistLocation3").val(postproject.showlistLocation3)
					
					for(var i in postproject.list){
						var input=$(".btnUpload").eq(i)
						
						onloadimg(input,postproject.list[i])
					}
					
					$("#form input[type='text']").each(function() {
						var key = $(this).attr("name")
						console.log(key)
						if(key&&postproject[key]){
							$(this).val(postproject[key])
						}
					})
					$('#selectDisciplines').selectpicker('val', postproject.Discipline.split(','));//默认选中
					
				}
				
			}
			
			
		   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(../../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;
	      }
	    });
	  })
	  
	  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)
	  	
	  }
	  
	//---------初始化数据部分-----------
	var  contact_number_list=["+86","+87","+88"]
	var  location=[{
		icon:"🇨🇳",
		name:"China"
	},{
		icon:"🇺🇸",
		name:"USA"
	},{
		icon:"🇪🇸",
		name:"Spain"
	}
	]
	
	var province=["河南省","安徽省","福建省","甘肃省"]
	var city=["南阳市","开封市","漯河市","许昌市","新乡市","济源市"]
	//---------初始化数据部分end-----------
	//页面初始化
	contact_number_list.forEach(item=>{
		var li=$("<option>"+item+"</option>")
		$(".showqulist").append(li)
	})
	location.forEach(item=>{
		var li=$("<option>"+item.icon+' '+item.name+"</li>")
		$(".showlistLocation1").append(li)
	})
	province.forEach(item=>{
		//var li=$("<li class='contact_location2_li' txt="+item+"><a >"+item+"</a></li>")
		var li=$("<option>"+item+"</option>")
		$(".showlistLocation2").append(li)
	})
	city.forEach(item=>{
		//var li=$("<li class='contact_location3_li' txt="+item+"><a >"+item+"</a></li>")
		var li=$("<option>"+item+"</option>")
		$(".showlistLocation3").append(li)
	})
	
		$.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){
					zeroModal.alert({
						unique: '1',
						opacity: 0.8,
						content: warning,
						contentDetail: warning1,
						width: '300px',
						height: '260px',
						cancel:true,
						ok: false,
						onClosed: function() {
							; //location.href='../signin/signin.html'
						},
						onComplete: function(evn) {
							console.log('onComplete')
							setTimeout(function() {
					
								zeroModal.close('1');
							}, 3000);
						}
					})
		       }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 = '../lanhu/index.html'
				  	},
				  	onClosed: function() {
				  		; //location.href='../signin/signin.html'
				  	},
				  	onComplete: function(evn) {
				  		submitCompany()
				  		console.log('onComplete')
				  		setTimeout(function(){
				  		window.location.href = '../lanhu/index.html'
				  			zeroModal.close('1');
				  		},3000);
				  	}
				  })
			   }
		    }
		});
		
		
		
		$("#form").validate()
		function submitCompany(){
			
			var time=(1900+new Date().getYear())+"."+(new Date().getMonth()+1)+"."+new Date().getDate()
			var postmoment={
				txt:$(".Projectdescriptiontxt").val(),
				date:$("input[name=completion]").val(),
				showlistLocation1:$(".showlistLocation1").val(),
				showlistLocation2:$(".showlistLocation2").val(),
				showlistLocation3:$(".showlistLocation3").val(),
				list:[]
			}
			$("#form input[type='text']").each(function() {
				var key = $(this).attr("name")
				postmoment[key] = $(this).val()
			})
			
			postmoment['Discipline']=$("#Discipline").val();  //Discipline 改为下拉框后单独取值
		
			
			$(".imginput").each(function(){
				var value=$(this).val()
				if(value){
					postmoment.list.push(value)
				}
			})
			window.localStorage.setItem("postproject",JSON.stringify(postmoment))
			
		}
	
	initDisciplinesSelect();
		
	//页面初始化end
})

function initDisciplinesSelect(){
	
	var opts = [" Masterplanning"," Architecture"," Interiors", "Lighting"," Landscape"," AV & Acoustics"," MEP"," Art work & Accessories"," Graphics & Signage"," Branding"," Furniture, Lighting"];
	$("#selectDisciplines").empty();
	let html=[];
	opts.forEach(function(item) {
		html.push(`<option>${item}</option>`);
	})
	$("#selectDisciplines").append(html.join(""));
	
	$('#selectDisciplines').selectpicker({
		template: {
			caret: '<span class="sel-icon glyphicon glyphicon-menu-down"></span>'
		}
	});
	$('#selectDisciplines').on('changed.bs.select', function (e, clickedIndex, isSelected, prestate,currentVal) {
	   console.log(currentVal);
	   $("#Discipline").val(currentVal);
	});

}