/* 
* @Author: anchen
* @Date:   2017-07-05 10:02:51
* @Last Modified by:   anchen
* @Last Modified time: 2017-07-07 15:41:43
*/

$(document).ready(function(){
    //关闭选择楼宇弹窗

    function showBuild(){
        $(".buildingmask").show().find(".selectbuilding").animate({
            bottom: 0
        }, 500);
    }
    

    $(".closebuild").click(function(){
        closeBuild();
    })

    $(".searchbuild").click(function(){
        var _this = $(this);
        _this.parents("#opSearchbar").hide();
        $("#opSearchbarentry").show();
        $("#opName").focus();
    })

    // 搜索框有值时显示 ‘x’图标 -- 无值时隐藏 ‘x’图标    
    $("#opName").bind('input propertychange', function() {
		var value = $(this).val();
		if(value){
            $("#opIconclear").show();
        } else {
            $("#opIconclear").hide();
        }
		loadProvincecityByCity();
	});

    // 点击搜索框的'x'图标时清楚搜索框里的字
    $("#opIconclear").click(function(){
        var _this = $(this);
        $("#opName").val("");
        _this.hide();
        loadProvincecityByCity();
    });

    $(".buildinglist").on("click", ".buildingcity", function(){
    	var _this = $(this).find(".arrowcity");
    	var cityCode=_this.attr('id');
    	var params=$("#opName").val();
    	if(opName!=""){
    		loadOrganizationLists(cityCode);
    	}else{
    	    loadOrganizationList(cityCode,_this);
    	}
    });
});

/**
 * 加载城市
 */
function loadProvincecityByCity(){
	var contextPath=$("#contextPath").val();
	var params=$("#opName").val();
	var developId=$("#developId").val();
	$.ajax({
		type:'POST',
		url:contextPath+'/wechat/appUser/getProvincecityByCityList',
		data:{params:params,developId:developId},
		dataType: 'json',
		success: function (data){
			$(".buildinglist").empty();
			if(data!=null && data!="" && data.length>0){
				var str="";
				$.each(data,function(index,item){
					if(item.cityCode==$("#cityCode").val()){
						str="<li><div class='buildingcity' id='buildingcity_"+item.cityCode+"'><span>"+item.name+"</span><strong class='arrowcity' id='"+item.cityCode+"'><i></i></strong></div></li>";
					}else{
						var html="<li><div class='buildingcity' id='buildingcity_"+item.cityCode+"'><span>"+item.name+"</span><strong class='arrowcity' id='"+item.cityCode+"'><i></i></strong></div></li>";
						$(".buildinglist").append(html);
						if(params!=""){
							loadOrganizationLists(item.cityCode);
						}
					}
				});
				if(str!="" && params==""){
					$(".buildinglist").prepend(str);
				}
				if(str!="" && params!=""){
					$(".buildinglist").prepend(str);
					loadOrganizationLists($("#cityCode").val());
				}
			}
		}
	});
}


/**
 * 加载楼宇
 */
function loadOrganizationLists(cityCode){
	var contextPath=$("#contextPath").val();
	var developId=$("#developId").val();
	var params=$("#opName").val();
	if($("#projectlist_"+cityCode).length>0){
		$("#buildingcity_"+cityCode).parents("li").toggleClass("buildchioce");
		if($("#buildingcity_"+cityCode).parents("li").hasClass("buildchioce") ){
			$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideDown(200);
		} else {
			$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideUp(200);
		}
		//$("#buildingcity_"+cityCode).parents("li").siblings().removeClass("buildchioce").find("projectlist").slideUp(200);
	}else{
		$.ajax({
			type:'POST',
			url:contextPath+'/wechat/appUser/getOrganizationListByName',
			data:{params:params,cityCode:cityCode,developId:developId},
			dataType: 'json',
			success: function (data){
				if(data!=null && data!="" && data.length>0){
					$("#projectlist_"+cityCode).empty();
					var html="<ul class='projectlist' id='projectlist_"+cityCode+"'>";
					$.each(data,function(index,item){
						html+="<li value='"+item.ID+"' onclick='getValue(this)'>"+item.name+"</li>"
					});
					html+="</ul>"
				}
				$("#buildingcity_"+cityCode).after(html);
				$("#buildingcity_"+cityCode).parents("li").toggleClass("buildchioce");
				$("#buildingcity_"+cityCode).siblings("#projectlist_"+cityCode).slideDown(800);
			}
		});
	}
}


/**
 * 加载楼宇
 */
function loadOrganizationList(cityCode,obj){
	var contextPath=$("#contextPath").val();
	var params=$("#opName").val();
	var developId=$("#developId").val();
	if($("#projectlist_"+cityCode).length>0){
		obj.parents("li").toggleClass("buildchioce");
		if(obj.parents("li").hasClass("buildchioce") ){
			obj.parent(".buildingcity").siblings(".projectlist").slideDown(200);
		} else {
			obj.parent(".buildingcity").siblings(".projectlist").slideUp(200);
		}
		obj.parents("li").siblings().removeClass("buildchioce").find(".projectlist").slideUp(200);
	}else{
		$.ajax({
			type:'POST',
			url:contextPath+'/wechat/appUser/getOrganizationListByName',
			data:{params:params,cityCode:cityCode,developId:developId},
			dataType: 'json',
			success: function (data){
				if(data!=null && data!="" && data.length>0){
					$("#projectlist_"+cityCode);
					var html="<ul class='projectlist' id='projectlist_"+cityCode+"'>";
					$.each(data,function(index,item){
						html+="<li value='"+item.ID+"' onclick='getValue(this)'>"+item.name+"</li>"
					});
					html+="</ul>"
				}
				$("#buildingcity_"+cityCode).after(html);
				obj.parents("li").toggleClass("buildchioce");
				if(obj.parents("li").hasClass("buildchioce") ){
					obj.parent(".buildingcity").siblings(".projectlist").slideDown(800);
				} else {
					obj.parent(".buildingcity").siblings(".projectlist").slideUp(800);
					
				}
				obj.parents("li").siblings().removeClass("buildchioce").find(".projectlist").slideUp(800);
				
				
			}
		});
	}
}

function getValue(obj){
	 var _this = $(obj);
     var _html = _this.html();
     if(_html){
  	   $("#organizationId").val(_this.val());
         $(".js_build").find("em").html(_html).parent().removeClass("placeholder");
         closeBuild();
         $(".js_company").find("strong").html("请选择公司").parent().attr("class","placeholder");
         $("#virtualArchId").val("");
         searchVirtualArch(_this.val());
     }
}

function getLocationiconValue(){
 	   $("#organizationId").val($("#locationiconId").val());
 	  $(".js_build").find("em").html($("#locationiconName").html()).parent().removeClass("placeholder");
        closeBuild();
        $(".js_company").find("strong").html("请选择公司").parent().attr("class","placeholder");
        $("#virtualArchId").val("");
        searchVirtualArch($("#locationiconId").val());
}

function closeBuild(){
	var height = $(window).height();
    $(".selectbuilding").animate({
        bottom: -height + "px"
    }, 500, function(){
        $("#opSearchbar").show();
        $("#opSearchbarentry").hide();
        $(this).parents(".buildingmask").hide();
    });
}

function addressOp(_this){
        _this.parents("#opSearchbar").hide();
        $("#opSearchbarentry").show();
        $("#opName").focus();
        if($("#opName").val()){
            $("#opIconclear").show();
        } else {
            $("#opIconclear").hide();
        }
}