
$(function(){
	
	Date.prototype.format = function(fmt) { 
	    var o = { 
	       "M+" : this.getMonth()+1,                 //月份 
	       "d+" : this.getDate(),                    //日 
	       "h+" : this.getHours(),                   //小时 
	       "m+" : this.getMinutes(),                 //分 
	       "s+" : this.getSeconds(),                 //秒 
	       "q+" : Math.floor((this.getMonth()+3)/3), //季度 
	       "S"  : this.getMilliseconds()             //毫秒 
	   }; 
	   if(/(y+)/.test(fmt)) {
	           fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	   }
	    for(var k in o) {
	       if(new RegExp("("+ k +")").test(fmt)){
	            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
	        }
	    }
	   return fmt; 
	}   
	
	$("#calendar").val(getDay(-30).format("yyyy-MM-dd"));
	$("#calendar1").val(getDay(-1).format("yyyy-MM-dd"));
	
	$("#calendar2").val(getDay(-30).format("yyyy-MM-dd"));
	$("#calendar3").val(getDay(-1).format("yyyy-MM-dd"));
	
	$("#calendar4").val(getDay(-30).format("yyyy-MM-dd"));
	$("#calendar5").val(getDay(-1).format("yyyy-MM-dd"));
	
	
	getcounter();
	showUserAdd(getDay(-30).format("yyyy-MM-dd"),getDay(0).format("yyyy-MM-dd"));
	showOrder(getDay(-30).format("yyyy-MM-dd"),getDay(0).format("yyyy-MM-dd"));
	showTop();
	if(curLibId==''){
		$("#adminSelect").show();
		$("#libIdSelect").find("option[value='"+selectLibId+"']").attr("selected",true);
		
		if(selectLibId==''){
			$("#counttitle").html('各馆会员总排行 <span style="color: #000;font-size: 12px;    float: right;padding-right: 15px;">未激活</span><span style="color: #000;font-size: 12px;    float: right;padding-right: 15px;">已付费</span>');
			$("#daytitle").html('各馆会员月排行 <span style="color: #000;font-size: 12px;    float: right;padding-right: 15px;">未激活</span><span style="color: #000;font-size: 12px;    float: right;padding-right: 15px;">已付费</span>');
			showCurOpenCar();
			showCountOpenCar();
		}else{
			$("#counttitle").html('会员借阅总排行 ');
			$("#daytitle").html('会员借阅月排行');
			showTopByUser();
			showTopByDayUser();
		}
	}
	
	if(selectLibId=='' &&  curLibId!=''){
		showTopByUser();
		showTopByDayUser();
	}
	
	showSgnCounter();
	$("#userAddBtu").click(function (){
		showUserAdd($("#calendar").val(),$("#calendar1").val());
	});
	
	$("#orderBtu").click(function (){
		showOrder($("#calendar2").val(),$("#calendar3").val());
	});
	
	$("#libIdSelect").change(function(){
		window.location.href=path+"/sys/login/home.html?selectLibId="+$(this).val();
	});

	

});

function showSgnCounter(){
	$.ajax({
        url:path+"/sys/counter/signCounter",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var tmpdata=[],tmptitle=[],obj=data.data.sign;

        	tmpdata.push({value:obj.withinThirty, name:'活跃状态'+obj.withinThirty+"(人)"});
        	tmpdata.push({value:obj.frontThirty, name:'即将沉默'+obj.frontThirty+"(人)"});
        	tmpdata.push({value:obj.withinSixty, name:'已沉默'+obj.withinSixty+"(人)"});
			
        	pieCar.setOption({
          	    title : {
          	        text: '到馆活跃度比例',
          	        subtext: '',
          	        x:'center'
          	    },
          	    tooltip : {
          	        trigger: 'item',
          	        formatter: "{a} <br/>{b} : {c} ({d}%)"
          	    },
          	    legend: {
          	        orient: 'vertical',
          	        left: 'left',
          	        data: ['活跃状态'+obj.withinThirty+"(人)",'即将沉默'+obj.frontThirty+"(人)",'已沉默'+obj.withinSixty+"(人)"]
          	    },
          	    series : [
          	        {
          	            name: '',
          	            type: 'pie',
          	            radius : '55%',
          	            center: ['50%', '60%'],
          	            data:tmpdata
          	            ,
          	            itemStyle: {
          	                emphasis: {
          	                    shadowBlur: 10,
          	                    shadowOffsetX: 0,
          	                    shadowColor: 'rgba(0, 0, 0, 0.5)'
          	                }
          	            }
          	        }
          	    ]
          	});
        }
    });
	
}



function showTop(){
	$.ajax({
        url:path+"/sys/counter/top",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var tmphtml=[],obj=data.data.rows;
			for(var i=0,len=obj.length;i<len;i++){
				tmphtml.push('<li><div class="app-show-title"><span class="num s-index-org">'+(i+1)+'.</span>'+obj[i].title+'<span class="num s-index-org" style="float: right;">'+obj[i].top+'次</span></div></li>');
			}
			$("#top").html(tmphtml.join(''));
        }
    });
}

function showCountOpenCar(){
	$.ajax({
        url:path+"/sys/counter/selectOpenCard",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	iscount:true
        },
        error:function(){return false;},
        success:function(data){
        	var tmphtml=[],obj=data.data.rows;
			for(var i=0,len=obj.length;i<len;i++){
				tmphtml.push('<li><div class="app-show-title"><span class="num s-index-org">'+(i+1)+'.</span>'+obj[i].league_name+'<span class="num s-index-org" style="float: right;width: 10%;">'+obj[i].notactivenum+'人</span><span class="num s-index-org" style="float: right;width: 10%;">'+obj[i].payusernum+'人</span></div></li>');
			}
			$("#topByUser").html(tmphtml.join(''));
        }
    });
}


function showTopByUser(){
	$.ajax({
        url:path+"/sys/counter/topByUser",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var tmphtml=[],obj=data.data.rows;
			for(var i=0,len=obj.length;i<len;i++){
				tmphtml.push('<li><div class="app-show-title"><span class="num s-index-org">'+(i+1)+'.</span>'+obj[i].wx_name+'<span class="num s-index-org" style="float: right;">'+obj[i].num+'本</span></div></li>');
			}
			$("#topByUser").html(tmphtml.join(''));
        }
    });
}
function showTopByDayUser(){
	$.ajax({
        url:path+"/sys/counter/topByDayUser",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var tmphtml=[],obj=data.data.rows;
			for(var i=0,len=obj.length;i<len;i++){
				tmphtml.push('<li><div class="app-show-title"><span class="num s-index-org">'+(i+1)+'.</span>'+obj[i].wx_name+'<span class="num s-index-org" style="float: right;">'+obj[i].num+'本</span></div></li>');
			}
			$("#topByDayUser").html(tmphtml.join(''));
        }
    });
}
function showCurOpenCar(){
	$.ajax({
        url:path+"/sys/counter/selectOpenCard",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	iscount:false
        },
        error:function(){return false;},
        success:function(data){
        	var tmphtml=[],obj=data.data.rows;
			for(var i=0,len=obj.length;i<len;i++){
				tmphtml.push('<li><div class="app-show-title"><span class="num s-index-org">'+(i+1)+'.</span>'+obj[i].league_name+'<span class="num s-index-org" style="float: right;width: 10%;">'+obj[i].notactivenum+'人</span><span class="num s-index-org" style="float: right;width: 10%;">'+obj[i].payusernum+'人</span></div></li>');
			}
			$("#topByDayUser").html(tmphtml.join(''));
        }
    });
}




function showUserAdd(startDate,endDate){
	$.ajax({
        url:path+"/sys/counter/selectCounter",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	type:'USERADD',
        	startDate:startDate,
        	endDate:endDate,
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var timedata=[],datatmp=[],obj=data.data.rows;
			for(var i=obj.length-1;i>=0;i--){
				timedata.push(obj[i].count_time);
				datatmp.push(obj[i].count);
				
			}
			addUserNum.setOption(
					{
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					        data:[]
					    },
					    toolbox: {
					        show : true,
					        feature : {
					            mark : {show: true},
					            dataView : {show: true, readOnly: false},
					            magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
					            restore : {show: true},
					            saveAsImage : {show: true}
					        }
					    },
					    calculable : true,
					    xAxis : [
					        {
					            type : 'category',
					            boundaryGap : false,
					            data :timedata
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value'
					        }
					    ],
					    series : [
					        {
					            name:'会员数(人)',
					            type:'line',
					            stack: '总量',
					            data:datatmp
					        }
					    ]
					}
			);
        }
    });
	
}

function showOrder(startDate,endDate){
	$.ajax({
        url:path+"/sys/counter/selectCounter",
        dataType:'json',
        cache:false,
        type:'get',
        data:{
        	type:'ORDERDAY',
        	startDate:startDate,
        	endDate:endDate,
        	selectLibId:selectLibId
        },
        error:function(){return false;},
        success:function(data){
        	var timedata=[],datatmp=[],obj=data.data.rows;
        	for(var i=obj.length-1;i>=0;i--){
				timedata.push(obj[i].count_time);
				datatmp.push(obj[i].count);
			}
			signNumCar.setOption(
					{
					    tooltip : {
					        trigger: 'axis'
					    },
					    legend: {
					        data:[]
					    },
					    toolbox: {
					        show : true,
					        feature : {
					            mark : {show: true},
					            dataView : {show: true, readOnly: false},
					            magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
					            restore : {show: true},
					            saveAsImage : {show: true}
					        }
					    },
					    calculable : true,
					    xAxis : [
					        {
					            type : 'category',
					            boundaryGap : false,
					            data :timedata
					        }
					    ],
					    yAxis : [
					        {
					            type : 'value'
					        }
					    ],
					    series : [
					        {
					            name:'营业额(元)',
					            type:'line',
					            stack: '总',
					            data:datatmp
					        }
					    ]
					}
			);
        }
    });
	
}

function getcounter(){
		$.ajax({
	        url:path+"/sys/counter/frontCounter",
	        dataType:'json',
	        cache:false,
	        type:'get',
	        data:{
	        	selectLibId:selectLibId
	        },
	        error:function(){return false;},
	        success:function(data){
	        	var obj=data.data.frontCounter
	        	$("#userAddNum").html(obj.userAddNum);
	        	$("#signNum").html(obj.signNum);
	        	$("#borrowNum").html(obj.borrowNum);
	        	$("#orderDay").html(obj.orderDay);
	        	$("#orderCount").html(obj.orderCount);
	        }
	    });
}


function getDay(n){
	var now = new Date;
	now.setDate(now.getDate() + n);
	return now;
}



