$(function() {

	$(document).ready(function() {


		$('#uploadButton').click(function() {
			$('#imagePic').click();
		});
		$("#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) {
						onloadresult(arg.target.result)


					}
				} 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;
			}
		});
	})



	//---------初始化数据部分-----------
	var contact_number_list = ["+86", "+87", "+88"]
	var location = [{
		icon: "🇨🇳",
		name: "China"
	}, {
		icon: "🇺🇸",
		name: "USA"
	}, {
		icon: "🇪🇸",
		name: "Spain"
	}]

	var province = ["aaa", "bbb", "ccc"]
	var city = ["aaa1", "bbb2", "ccc3"]
	//---------初始化数据部分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)
	})




	$("#contact_number_add").click(function() {
		
		$("#contact_number").append(html_contact_number)

		$("#contact_number_remove").show()
		inithtml()
	})
	$("#contact_number_remove").click(function() {
		$("#contact_number .row-fluid").eq(-1).remove();
		if ($("#contact_number .row-fluid").length == 1) {
			$("#contact_number_remove").hide()
		}
	})

	$("#contact_email_add").click(function() {
		
		$("#contact_email").append(html_contact_email)

		$("#contact_email_remove").show()
		inithtml()
	})
	$("#contact_email_remove").click(function() {
		$("#contact_email input").eq(-1).remove();
		if ($("#contact_email input").length == 1) {
			$("#contact_email_remove").hide()
		}
	})


	$("#contact_location_add").click(function() {
		
		$("#contact_location").append(html_contact_location)

		$("#contact_location_remove").show()
		inithtml()
	})
	$("#contact_location_remove").click(function() {
		$("#contact_location .row-fluid").eq(-1).remove();
		if ($("#contact_location .row-fluid").length == 1) {
			$("#contact_location_remove").hide()
		}
	})




	$.validator.setDefaults({
		submitHandler: function() {
			var submitlogo = $("#uploadButtonInput").val();
			var success = $(".head_success").val();
			var warning = $(".head_warning").val();
			var success1 = $(".signin_success").val();
			var warning1 = $(".signin_warning").val()
			var headcommon2 = $(".signin_headcommon2").val()



			if (!submitlogo) {
				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 = './postproject.html'
					},
					onClosed: function() {
						; //location.href='../signin/signin.html'
					},
					onComplete: function(evn) {
						submitCompany()
						console.log('onComplete')
						// setTimeout(function(){
						// 	location.href='../lanhu/index.html'
						// 	zeroModal.close('1');
						// },3000);
					}
				})
			}
		}
	});
	$("#form").validate()

	function submitCompany() {

		var obj = {

		}
		obj.logo = $("#uploadButtonInput").val();
		obj.introduction = $(".introduction").val()

		$("#form input[type='text']").each(function() {
			var key = $(this).attr("name")
			obj[key] = $(this).val()
		})
		var i = 0;
		var sz1 = [];
		$(".showlistLocation1").each(function() {
			var ob = {}
			ob.location1 = $(this).val()
			ob.location2 = $(".showlistLocation2").eq(i).val()
			ob.location3 = $(".showlistLocation3").eq(i).val()
			ob.avenue = $("input[name=Avenue]").eq(i).val()
			sz1.push(ob)
			i++;
		})
		obj.locationList = sz1;


		i = 0;
		var sz2 = [];
		$(".showqulist").each(function() {
			var ob = {}
			ob.qu = $(this).val()
			ob.contactNumber = $("input[name=contactNumber]").eq(i).val()
			sz2.push(ob)
			i++;
		})
		obj.showqulist = sz2;

		i = 0;
		var sz3 = [];
		$("input[name=contactEmail]").each(function() {
			var ob = {}
			ob.contactEmail = $(this).val()
			sz3.push(ob)
			i++;
		})
		obj.contactEmailList = sz3;

		window.localStorage.setItem("loginCompany", JSON.stringify(obj));
	}


	$("#registerupload2").click(function(){
		submitCompany()
	})

	//页面初始化end
})
var html_contact_number = $("#contact_number").html();
var		html_contact_email = $("#contact_email").html();
var		html_contact_location = $("#contact_location").html();

function getItemloginCompany() {
	 html_contact_number = $("#contact_number").html();
			html_contact_email = $("#contact_email").html();
			html_contact_location = $("#contact_location").html();
	
	var loginCompany = window.localStorage.getItem("loginCompany");
	if (loginCompany) {
		loginCompany = JSON.parse(loginCompany);
		
		onloadresult(loginCompany.logo)
		
		$("#form input[type='text']").each(function() {
			var key = $(this).attr("name")
			console.log(key)
			if(key&&loginCompany[key]){
				$(this).val(loginCompany[key])
			}
		})
		var i = 0;
		loginCompany.locationList.forEach(function(){
			
			if(i==1){
				$("#contact_location_remove").show()
			}
			var ob=loginCompany.locationList[i]
			 $(".showlistLocation1").eq(i).val(ob.location1 )
			 $(".showlistLocation2").eq(i).val(ob.location2 )
			 $(".showlistLocation3").eq(i).val(ob.location3 )
			$("input[name=Avenue]").eq(i).val(ob.avenue )
			i++;
			$("#contact_location").append(html_contact_location)
			
		})
		i = 0;
		loginCompany.showqulist.forEach(function(){
			if(i==1){
				$("#contact_number_remove").show()
			}
			var ob=loginCompany.showqulist[i]
			 $(".showqulist").eq(i).val(ob.qu )
			 $("input[name=contactNumber]").eq(i).val(ob.contactNumber )
		
			i++;
			$("#contact_number").append(html_contact_number)
			
		})
		i = 0;
		loginCompany.contactEmailList.forEach(function(){
			
			if(i==1){
				$("#contact_email_remove").show()
			}
			var ob=loginCompany.contactEmailList[i]
			 $("input[name=contactEmail]").eq(i).val(ob.contactEmail )
			
			i++;
			$("#contact_email").append(html_contact_email)
			
		})
		$("#contact_location .row-fluid").eq(-1).remove();
		$("#contact_number .row-fluid").eq(-1).remove();
		$("#contact_email input").eq(-1).remove();
		
		 $(".introduction").val(loginCompany.introduction)
		
	}
	
}

function onloadresult(result) {
	$("#uploadButtonInput").val(result);
	$("#uploadButton").css("background", "url(" + result + ") no-repeat center")
	$("#uploadButton").css("border", '1px solid #b8b9ba')
	$("#uploadButton").css("border-radius", '10px')
	$("#uploadButton").css("background-size", '100% 100%')
}

function inithtml() {
	try{
			relanguage()
	}catch(e){
		console.log(e)
	}
}
