/*2017-01-16 add by yb
 * describe:微信前端，用户手机号码绑定
 */
$(document).ready(function(){
	var contextPath = $("#contextPath").val();
    /*
     * js手机号码正则校验
     * value -- 获取手机号
     */
    function checkTel(value){
        //var isMob=/^(?:13\d|15\d|18\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
        var isMob=/^(?:13\d|15\d|18\d|17\d|14\d|19\d|16\d)-?\d{5}(\d{3}|\*{3})$/; //手机号码
        var isMobile=/^((0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/;//电话号码--带区号
        var isMobileq=/^[1-9]{1}[0-9]{5,8}$/;//电话号码--不带区号
        var isMobile4=/^400[0-9]{7}/;//400
        var isMobile8=/^800[0-9]{7}/;//800
		if(!(isMob.test(value)) && !(isMobile.test(value)) && !(isMobileq.test(value)) && !(isMobile4.test(value)) && !(isMobile8.test(value))){
			alert(isMobile8.test(value));
			 return false;
		}
		else{
			 return true;
		}
    }

    $(".logintel").blur(function(event) {
        var _this = $(this);
        var val = _this.val();
        if(!val){
        	$("#tel").addClass("tel-wrong");
        } else {
            var valtest = checkTel(val);  //手机正则校验
            if(!valtest) {
                _this.val("");
                _this.attr({
                    "placeholder": "请输入正确的电话号码"
                });
                $("#tel").addClass("tel-wrong");
            } else {
            	$("#tel").removeClass("tel-wrong");
            }
        }
    });
    
    $(".loginpsw").blur(function(event) {
        var _this = $(this);
        var veriVal = $.trim(_this.val());
        if(!veriVal){
           _this.val("");
           $("#pws").addClass("veri-wrong");
        } else {
           $("#pws").removeClass("veri-wrong");
        }
    });
    
    $("#name").removeAttr("style");
    ///自动填充企业名称--备用
	$("#namebck").autocomplete({
		width : 300,
		max : 10,
		delay : 100,
		minLength : 1,
		autoFocus : true,
		cacheLength : 1,
		highlight : false,
		mustMatch:true,
		matchFromStart: false,
		source : function(request, response) {
			var userId = $("#userId").val();
			var name = $("#name").val();
			if(name) name = name.trim();
			$("#name").attr("disabled",false);
    		$("#name").val(""); 
			$.ajax({
				url : contextPath+'/wechat/signContract/queryEnterpriseList',
				data :{"name":name,"userId":userId},
				type : "POST",
				dataType: "json",
				success : function(data) {
					response($.map(eval(data.list), function(item) {
						return {
							value : item.name,
							name : item.name,
							id : item.id,
							organizationId : item.organizationId
						};
						
					}));
				}
			});
	
		},
		select : function(event, ui) {
			var data = ui.item;
			ui.item.value = data.name;
	    	$("#name").val(data.name);
	    	if(null != data.name && data.name != ""){
	    		$("#name").attr("disabled",true);
	    		$("#name").val(data.name);   
	    		$("#virtualArchitectureId").val(data.id);
	    		$("#organizationId").val(data.organizationId);
	    	}
		}
	
	});
	
    var values = [];
    var valuesId = [];
    var userId = $("#userId").val();
    //加载企业大厦数据 
    $.ajax({
		type:'POST',
		url:contextPath+'/wechat/signContract/queryOrganizationList',
		data:{'userId':userId},
		dataType: 'json',//返回值类型 一般设置为json
		async : false,
		success: function (data)  //服务器成功响应处理函数
		{
			$("#organizationId").empty();
			var subs = data.list;// 获取data中list的数组
			var _array = [];
			_array.push("<option value=''>--请选择--</option>");
			for ( var i = 0; i < subs.length; i++) {//特殊情况，先默认传递校区organizationId，后期改版改成楼栋id
				_array.push("<option value='" + subs[i].organizationId + "'>"+ subs[i].name + "</option>");
				$("#organizationId").html(_array.join(""));
			}
			$("#organizationId").html(_array.join(""));
		}
	});
    
    $("#addVirtualArchitecture").click(function(){
    	var name = $("#name").val();
    	var employees = $("#employees").val();
    	var contacts = $("#contacts").val();
    	var phone = $("#phone").val();
    	var organizationId = $("#organizationId").val();
    	var address = $("#address").val();
    	var userId = $("#userId").val();
    	if(name == null || name ==""){
    		layer.alert("企业名称不能为空");
    		$("#name").focus();
    		return;
    	}
    	if(employees == null || employees ==""){
    		layer.alert("企业人数不能为空");
    		$("#employees").focus();
    		return;
    	}
    	if(contacts == null || contacts ==""){
    		layer.alert("企业联系人不能为空");
    		$("#employees").focus();
    		return;
    	}
    	if(phone == null || phone ==""){
    		layer.alert("企业联系电话不能为空");
    		$("#phone").focus();
    		return;
    	}
    	if(isPhone);
    	if(organizationId == null || organizationId ==""){
    		layer.alert("企业所在大厦不能为空");
    		$("#organizationId").focus();
    		return;
    	}
    	//加载企业大厦数据 
        $.ajax({
    		type:'POST',
    		url:contextPath+'/wechat/signContract/addVirtualArchitecture',
    		data:{'name':name,'employees':employees,'contacts':contacts,'phone':phone,'organizationId':organizationId,'address':address},
    		dataType: 'json',//返回值类型 一般设置为json
    		success: function (data)  //服务器成功响应处理函数
    		{
    			if(data.code == '0'){
    				alert(data.msg);
    				window.location.href=contextPath+"/wechat/signContract/ordersevice?virtualArchitectureId="+data.virtualArchitectureId+"&organizationId="+organizationId+"&userId="+userId;
    			}else{
    				layer.alert(data.msg);
    				$("#fail").show();
    			}
    		}
    	});
    })
    
});

//验证手机号码
function isPhone(val){
	if(val == null || val == "" ){
		layer.alert("手机号码不能为空！");
		return false;
	}
	/*中移动手机号码*/
	var PATTERN_CHINAMOBILE = /^1(3[4-9]|5[012789]|8[23478]|4[7]|7[8])\d{8}$/;
	/*联通手机号码*/
	var PATTERN_CHINAUNICOM =/^1(3[0-2]|5[56]|8[56]|4[5]|7[6])\d{8}$/;
	/*电信手机号码*/
	var PATTERN_CHINATELECOM =/^1(3[3])|(8[019])\d{8}$/;
	if(PATTERN_CHINAMOBILE.test(val) || PATTERN_CHINAUNICOM.test(val) || PATTERN_CHINATELECOM.test(val)){
		return true;
	}
	layer.alert("手机号码格式不正确");
	return false;
}
