var getProcessBuild ;
$(function() {
	
	var adminId=$("#adminId").val();
	/*打开小区回填列表*/
	$('#download_commName').click(function() {
		communityList.backfillFun = function(data) {
			console.log('下载区域');
			$('#download_commId').val(data.id);
			$('#download_commName').val(data.name);
		}
//		var data={};
		var adminId=$("#adminId").val();
		var data={"adminId":adminId};
		communityList.data=data;
		communityList.popCommunityList(data);
	});
	/*打开小区回填列表*/
	$('#upload_commName').click(function() {
		communityList.backfillFun = function(data) {
			console.log('上传区域');
			$('#upload_commId').val(data.id);
			$('#upload_commName').val(data.name);
		}
//		var data={};
		
		var data={"adminId":adminId};
		communityList.data=data;
		communityList.popCommunityList(data);
		
		
	});
	$("#progressRow").hide();
	$("#uplodTipRow").hide();
	$("#successTipRow").hide();
    $('#upload_btn').removeAttr('onclick');
	//重复执行某个方法 
	getProcessBuild = window.setInterval("getProcessCacheB()",1000); 
});

/**
 * 点击下载按钮
 */
$("#download_btn").click(function() {
	var commId = $("#download_commId").val();
	if (commId == null || commId.length <= 0) {
		var x = ($(window).width() - 260 + $('div.viewFramework-sidebar').width())/2;
		var y = ($(window).height() / 2 - 73);
		layer.alert('请选择小区。', {
			icon : 0,
			  title:'提示',
			  area: ['260px', '146px'],
			  offset: [y+'px', x+'px']
		});
		return;
	}
	$("#download_form").submit();
});
function uploadExcel(){
	var commId = $("#upload_commId").val();
	var communityName=$("#upload_commName").val();
	var x = ($(window).width() - 260 + $('div.viewFramework-sidebar').width())/2;
	var y = ($(window).height() / 2 - 73);
	if (commId == null || commId.length <= 0 ||communityName == null || communityName.length<=0) {
		layer.alert('请选择小区。', {
			icon : 0,
			  title:'提示',
			  area: ['260px', '146px'],
			  offset: [y+'px', x+'px']
		});
		return;
	}
	var file = $("[name=excelFile]").val();
	if (file == null || file.length <= 0) {
		layer.alert('请选择上传文件。', {
			icon : 0,
			  title:'提示',
			  area: ['260px', '146px'],
			  offset: [y+'px', x+'px']
		});
		return;
	}
	var formData = new FormData($("#uploadForm")[0]);
	var ajaxExcel = $.ajax({
		url : ctx + "/adm/excel/build_excelin",
		type : 'POST',
		data : formData,
		timeout : 300000, //超时时间设置，单位毫秒
		async : false,
		cache : false,
		contentType : false,
		processData : false,
		success : function(json) {
			if (isOK(json)) {
				//var json=$.parseJSON(repJson);
				if (json.code == 0) {
					var x = ($(window).width() - 260 + $('div.viewFramework-sidebar').width())/2;
					var y = ($(window).height() / 2 - 73);
					layer.alert(json.msg, {
						icon : 1,
						  title:'提示',
						  area: ['260px', '146px'],
						  offset: [y+'px', x+'px']
					});
                    $('#upload_btn').removeAttr('onclick');
					//重复执行某个方法 
					getProcessBuild = window.setInterval("getProcessCacheB()",1000); 
				}else if(json.code==undefined){//没有该功能权限
     				addForm(json);
     			} else {
					//页面层
					var x = ($(window).width() - 420 + $('div.viewFramework-sidebar').width())/2;
					var y = ($(window).height() / 2 - 120);
					layer.open({
						type : 1,
						skin : 'layui-layer-rim', //加上边框
						area : [ '420px', '240px' ], //宽高
						content : json.msg,
						  title:'提示',
						  offset: [y+'px', x+'px']
					});
				}
			}
		},
		error : function() {
			console.log("请求楼栋导入方法--error");
		},
		complete:function(XMLHttpRequest,status){
			console.log("请求楼栋导入方法--complete");
			waitingStop();
			if(status=='timeout'){//超时,status还有success,error等值的情况
				ajaxExcel.abort();
  				alert("excel导入请求超时");
  			}
		},
		beforeSend:function(){
			console.log("请求楼栋导入方法--beforeSend");
			waiting();
		}
	});
}

function uploadFile(){
	$('#excelFile').click();
}

function changeFile(){
	var file=$('#excelFile').val();
	var pos=file.lastIndexOf("\\");
    var fileName=file.substring(pos+1);
    var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
    if (".xls" != ext){
        alert("对不起，只能上传以下格式的文件:  "
            + ".xls" + "\n请重新选择符合条件的文件"
            + "再上传.");
        $('#excelFile').val("");
        $("#upload_a").html("点击选择文件");
        $("#upload_a").attr("title","点击选择文件");
        return;
    }
	$("#upload_a").html(fileName);
	$("#upload_a").attr("title",fileName);
}


/**
 * 获取缓存信息，判断是否有正在上传的excel
 */
function getProcessCacheB(){
	var cacheId = $("#cacheId").val();
	var data={};
	data.cacheId=cacheId;
	doExcelCachePost(data,false,ctx+"/common/excel/cache",function(json){
		//var json=$.parseJSON(repJson);
		if(json.code==0){
//			alert(json.data.process);
			var phtml='<div class="progress-bar progress-bar-success progress-bar-striped active" style="width: '+json.data.process+'%;"><div class="progress-value">'+json.data.process+'%</div></div>';
			$("#excelProgressDiv").html(phtml);
			if(json.data.status==2 ){
				if(json.data.process==100){
					$("#progressRow").show();
					$("#successTipRow").show();
				}else{
					$("#progressRow").hide();
					$("#successTipRow").hide();
				}
				
				$("#uplodTipRow").hide();
				$('#upload_btn').attr('onclick','uploadExcel()');
				//去掉定时器的方法 
				window.clearInterval(getProcessBuild);
				//释放提交按钮
			}else if(json.data.status==-1){//上传失败
				$("#progressRow").show();
				$("#uplodTipRow").show();
				$("#successTipRow").hide();
				$('#upload_btn').attr('onclick','uploadExcel()');
				//去掉定时器的方法 
				window.clearInterval(getProcessBuild);
			}else{
				$("#progressRow").show();
				$("#successTipRow").hide();
				$("#uplodTipRow").hide();
			}
			$("#progerssDetail").html("小区名称："+json.data.communityName+"；上传文件："+json.data.fileName);
		}else{
			
		}
	});
} 

/**
 * 查看上传失败原因
 */
function queryFailReson(){
	var cacheId = $("#cacheId").val();
	var data={};
	data.cacheId=cacheId;
	doExcelCachePost(data,false,ctx+"/common/excel/cache",function(json){
		//var json=$.parseJSON(repJson);
		if(json.code==0){
			if(json.data.status==-1){//上传失败
				//页面层
				var x = ($(window).width() - 420 + $('div.viewFramework-sidebar').width())/2;
				var y = ($(window).height() / 2 - 120);
				layer.open({
					type : 1,
					skin : 'layui-layer-rim', //加上边框
					area : [ '420px', '240px' ], //宽高
					content : json.data.errorMessage,
					  title:'提示',
					  offset: [y+'px', x+'px']
				});
			}
		}
	});
}


/**
 * 清除进度条
 */
function clearProgress(){
	$("#progressRow").hide();
	$("#uplodTipRow").hide();
	$("#successTipRow").hide();
	
	var cacheId = $("#cacheId").val();
	var data={};
	data.cacheId=cacheId;
	doExcelCachePost(data,false,ctx+"/common/excel/delCache",function(json){
		//var json=$.parseJSON(repJson);
		if(json.code==0){
			getProcessCacheB();
		}
	});
}

	
