
var contextPath="";
var actType="";
$(function(){
	initVirtual();
	contextPath=$("#contextPath").val();
	$("#btn-search").click(function(){
		$("#conditionform").submit();
	});
	
	//监控校区发生改变
	$("#organizationId").change(function(){
		initVirtual();
	});
});
	
//翻页
function goPage(pageNo){
	if(pageNo=="gopage"){
		$("#pageNo").val($("#gopage").val());
	}else{
		$("#pageNo").val(pageNo);
	}
		$("#conditionform").submit();
}

/**
 * 查询企业
 */
function initVirtual(){
	var organizationId=$("#organizationId").val();
	var virtualId_=$("#virtualId_").val();
	$("#virtualId").empty();
	if(organizationId!=""){
		var optionStr="";
		$("#virtualId").append("<option value=''>请选择企业</option>");
		$.ajax({
			url :$("#contextPath").val()+"/busi/property/getvirtualArchTypeOne",
			type : "POST",
			data : {organizationId:organizationId},
			dataType:"json",
			success : function(data) {
				$.each(data,function(index,obj){
					if(virtualId_!=""&&virtualId_==obj.id){
						optionStr+="<option selected='selected' value='"+obj.id+"'>"+obj.name+"</option>";
					}else{
						optionStr+="<option value='"+obj.id+"'>"+obj.name+"</option>";
					}
				});
				$("#virtualId").append(optionStr);
			},
			error : function(data) {
			}
		});
	}
	else{
		$("#virtualId").append("<option value=''>请选择企业</option>");
	}
}

function initVirtualForm(organizationId,virtualArchitectureId){
	var url="";
	$("#formvirtualId").empty();
	if(organizationId!=""){
		var optionStr="";
		$("#formvirtualId").append("<option value=''>请选择企业</option>");
		if(virtualArchitectureId!="" && typeof(virtualArchitectureId)!="undefined"){
			url=$("#contextPath").val()+"/busi/property/getvirtualArchTypeOne";
		}else{
			url=$("#contextPath").val()+"/busi/property/getAddvirtualArchTypeOne";
		}
		$.ajax({
			url :url,
			type : "POST",
			data : {organizationId:organizationId},
			dataType:"json",
			success : function(data) {
				$.each(data,function(index,obj){
						if(obj.id==virtualArchitectureId){
							optionStr+="<option value='"+obj.id+"' selected=selected>"+obj.name+"</option>";
						}else{
						optionStr+="<option value='"+obj.id+"'>"+obj.name+"</option>";
						}
				});
				$("#formvirtualId").append(optionStr);
			},
			error : function(data) {
			}
		});
	}
	else{
		$("#formvirtualId").append("<option value=''>请选择企业</option>");
	}
}

/**
 * 新增企业签约记录
 */
function addContractInfomation(){
	actType="save";
	$('#save').show();
	$('#dealtis').hide();
	$('#saveModal').modal('show');
	$("#formOrganizationId").val("");
	initVirtualForm("","");
	$('.form-horizontal').show();
	$("#myModalLabel").html("新增企业签约信息");
	$("#formOrganizationId").attr("disabled",false);
	$("#formvirtualId").attr("disabled",false);
	$("[name = type]:checkbox").each(function () {
		$(this).attr("checked", false);
    });
}

/**
 * 编辑企业签约记录信息
 */
function eidtContractInfomation(id,organizationId,virtualArchitectureId,type){
	actType="eidt";
	$('#save').show();
	$('#dealtis').hide();
	$('#saveModal').modal('show');
	$("#myModalLabel").html("编辑企业签约信息");
	$("#formOrganizationId").attr("disabled",true);
	$("#formvirtualId").attr("disabled",true);
	$('.form-horizontal').show();
	$("#id").val(id);
	var check = document.getElementsByName("type");
	$('#formOrganizationId').val(organizationId);
	initVirtualForm(organizationId,virtualArchitectureId);
	if(type!=null && type!=""){
		var strType=type.split(",");
			$("[name = type]:checkbox").each(function () {
				for(var i=0;i<strType.length;i++){
					if($(this).val()==strType[i]){
						$(this).attr("checked", true);
					}
				}
            });
	}
}

/**
 * 企业签约信息详情
 * @param id
 */
function deailsContractInfomation(id){
	$.ajax({
		url :$("#contextPath").val()+"/admin/contract/businessRecordDetails",
		type : "POST",
		data : {id:id},
		dataType:"json",
		success : function(data) {
			if(data!=null){
				$("#dealtisOrganizationName").html(data.organizationName);
				$("#dealtisVname").html(data.virtualarchitectureName);
				$("#dealtisVphone").html(data.phone);
				$("#dealtisCustomer").html(data.customerNumber);
				$("#dealtisCreateTime").html(data.createTimeFormat);
				$("#dealtisAddress").html(data.address);
				$("#dealtisTypeName").html(data.typeName);
				$('#saveModal').modal('show');
				$('#myModalLabel').html("企业签约信息详情");
				$('.form-horizontal').hide();
				$('#save').hide();
				$('#dealtis').show();
			}else{
				alert("程序异常！");
			}
		},
		error : function(data) {
		}
	});
	
}

function deleteContractIinformation(id,virtualarchitectureName){
	layer.confirm('您确定要删除'+virtualarchitectureName+'企业签约信息吗？', {
		  btn: ['确定','取消'] //按钮
		}, function(){
			layer.closeAll();
			$.ajax({
				url :$("#contextPath").val()+"/admin/contract/deleteBusinessRecord",
				type : "POST",
				data : {id:id},
				dataType:"json",
				success : function(data) {
					if(data.flag){
						alert(data.desc);
						location.href=contextPath+"/admin/contract/businessRecordPage?funcid="+$("#funcid").val();
					}else{
						alert(data.desc);
					}
				},
				error : function(data) {
				}
			});
			
		}, function(){
		
		});
}

function saveUpdateContractInfomation(){
	var url="";
	var organizationId=$("#formOrganizationId").val();
	if(organizationId==""){
		layer.msg('请选择校区！');
		return;
	}
	var virtualArchitectureId=$("#formvirtualId").val();
	if(virtualArchitectureId==""){
		layer.msg('请选择企业！');
		return;
	}
	var type="";
	var id="";
	if(actType=="save"){
		id="";
		url=$("#contextPath").val()+"/admin/contract/saveUpdateBusinessRecord";
	}
	if(actType=="eidt"){
		id=$("#id").val();
		url=$("#contextPath").val()+"/admin/contract/saveUpdateBusinessRecord";
	}
	 var check = document.getElementsByName("type");
	 for (var i = 0; i < check.length; i++) {
         if (check[i].checked) {
        	 type += check[i].value+",";
         }
     }
	 if(type==""){
			layer.msg('请选择服务！');
			return;
		}
	$.ajax({
		url :url,
		type : "POST",
		data : {id:id,organizationId:organizationId,virtualArchitectureId:virtualArchitectureId,type:type,status:"1"},
		dataType:"json",
		success : function(data) {
			if(data.flag){
				alert(data.desc);
				location.href=contextPath+"/admin/contract/businessRecordPage?funcid="+$("#funcid").val();
			}else{
				alert(data.desc);
			}
		},
		error : function(data) {
		}
	});
}