var myChart = echarts.init(document.getElementById("orderFee_date"));
var myChart2 = echarts.init(document.getElementById("orderFee_payType"));
let toDayTransCount = 0.00;
let yestDayNowTransAmt = 0.00;
let scale = "0.00%";
let subtract = 0.00;
let oTypeName = "上升";
$(function() {
	var newTimes=[];
	var orderFees = [];
	var channelNames=[];
	var channelNameAmts = [];
	var payTransNum = [];
	var channelNums = [];
	showAdmin();
	
	$.ajax({
		url : ctx + "/platform/firstData",
		type : "POST",
		success : function(data) {
			data = data.data;
			// var platformSum=data.platformSum;
			//粉丝总数量
			$("#fansCount").html(data.fansCountNum);
			//商户数量
			$("#merchantCount").html(data.mercCountNum);
			//交易总额
			$("#transCount").html('￥'+data.transCount);
			//交易总笔数
			$("#transNum").html(data.transNum);
			//会员数量
			$("#memberCount").html(data.memberCountNum);
			//推广员数量
			$("#promoterCount").html(data.promCountNum);
			//推广分润总额
			$("#shareMoneySum").html('￥'+data.profitCountAmt);
			//粉丝奖励总额
			$("#fansShareMoneySum").html('￥'+data.fansCountAmt);
			//收益总计
			$("#earnings").html('￥'+data.profitCountAmt);
			//今日收款总额
			$("#toDayTransSum").html('￥'+data.toDayTransCount);
			//今日支付宝收款
			$("#toDayTransZfbSum").html('￥'+data.toDayTransAliCount);
			//今日微信收款
			$("#toDayTransWxSum").html('￥'+data.toDayTransWxCount);
			//今日云闪付支付
			$("#toDayTransYsfSum").html('￥'+data.toDayTransUnionCount);
			//今日收款笔数
			$("#toDayTransCount").html(data.toDayTransNum);
			//今日支付宝收款笔数
			$("#toDayTransZfbCount").html(data.toDayTransAliNum);
			//今日微信收款笔数
			$("#toDayTransWxCount").html(data.toDayTransWxNum);
			//今日云闪付收款笔数
			$("#toDayTransYsfCount").html(data.toDayTransUnionNum);
			//今日退款金额
			$("#toDayRefundSum").html('￥'+data.toDayRefundCount);
			//今日支付宝退款金额
			$("#toDayRefundZfbSum").html('￥'+data.toDayRefundAliCount);
			//今日微信退款金额
			$("#toDayRefundWxSum").html('￥'+data.toDayRefundWxCount);
			//今日云闪付退款金额
			$("#toDayRefundYsfSum").html('￥'+data.toDayRefundUnionCount);
			//今日新增门店
			$("#toDayStoreCount").html(data.addToDayStoreNum);
			//今日新增会员
			$("#toDayMemberCount").html(data.addToDayMemberNum);
			//今日新增推广员
			$("#toDayPromoterCount").html(data.addToDayPromoterNum);
			//新增总数
			var md = data.addToDayStoreNum == null ? 0 : data.addToDayStoreNum;
			var hy = data.addToDayMemberNum == null ? 0 : data.addToDayMemberNum;
			var tgy = data.addToDayPromoterNum == null ? 0 : data.addToDayPromoterNum;
			$("#toDayAddCount").html(parseInt(md)+parseInt(hy)+parseInt(tgy));
			// //今日推广分润
			$("#toDayShareoneySum").html('￥'+data.toDayShareCount.toFixed(2));
			// //今日粉丝奖励
			$("#toDayFansShareMoneySum").html('￥'+data.toDayFansCount.toFixed(2));
			//
			// //今日收益
			let sjtoDayDayEarnings = parseFloat(data.toDayProfitCount) - parseFloat(data.toDayShareCount) - parseFloat(data.toDayFansCount)
			//今日收益
			$("#toDayEarnings").html('￥'+sjtoDayDayEarnings.toFixed(2));

			$("#toDayAddMoneyCount").html("￥"+data.toDayProfitCount.toFixed(2));
			let yesterDay = data.yesterDay;
			// //昨日收款总额
			$("#yesterDayTransSum").html('￥'+yesterDay.transAmt);
			// //昨日支付宝收款
			$("#yesterDayTransZfbSum").html('￥'+yesterDay.aliAmt);
			// //昨日微信收款
			$("#yesterDayTransWxSum").html('￥'+yesterDay.wxAmt);
			// //昨日云闪付收款
			$("#yesterDayTransYsfSum").html('￥'+yesterDay.ysfAmt);
			// //昨日收款笔数
			$("#yesterDayTransCount").html(yesterDay.transNum);
			// //昨日支付宝收款笔数
			$("#yesterDayTransZfbCount").html(yesterDay.aliNum);
			// //昨日微信收款笔数
			$("#yesterDayTransWxCount").html(yesterDay.wxNum);
			// //昨日云闪付收款笔数
			$("#yesterDayTransYsfCount").html(yesterDay.ysfNum);
			// //昨日退款金额
			$("#yesterDayRefundSum").html('￥'+yesterDay.refundAmt);
			// //昨日支付宝退款金额
			$("#yesterDayRefundZfbSum").html(yesterDay.aliRefundAmt);
			// //昨日微信退款金额
			$("#yesterDayRefundWxSum").html(yesterDay.wxRefundAmt);
			// //昨日云闪付退款金额
			$("#yesterDayRefundYsfSum").html(yesterDay.ysfRefundAmt);
			// //昨日新增会员
			$("#yesterDayMemberCount").html(yesterDay.memberNum);
			// //昨日新增门店
			$("#yesterDayStoreCount").html(yesterDay.storeNum);
			// //昨日新增推广员
			$("#yesterDayPromoterCount").html(yesterDay.promoterNum);

			var ysYh = yesterDay.memberNum == null ? 0 : yesterDay.memberNum;
			var ysMd = yesterDay.storeNum == null ? 0 : yesterDay.storeNum;
			var ysTgy = yesterDay.promoterNum == null ? 0 : yesterDay.promoterNum;
			// //昨日新增总数
			var yesterDayAddCount = parseInt(ysYh)+parseInt(ysMd)+parseInt(ysTgy);
            $("#yesterDayAddCount").html(yesterDayAddCount);

			// //昨日推广分润
			$("#yesterDayShareoneySum").html('￥'+yesterDay.shareAmt);
			// //昨日粉丝奖励
			$("#yesterDayFansShareMoneySum").html('￥'+yesterDay.fansAmt);
			// //昨日收益
			let sjYesterDayEarnings = parseFloat(yesterDay.profitAmt) - parseFloat(yesterDay.shareAmt) - parseFloat(yesterDay.fansAmt)
			$("#yesterDayEarnings").html('￥'+sjYesterDayEarnings.toFixed(2));
			//昨日累计收益
            $("#yesterDayAddMoneyCount").html("￥"+yesterDay.profitAmt);

			toDayTransCount = data.toDayTransCount;

			yestDayNowTransAmt = data.yestDayNowTransAmt;

			scale = data.scale;

			subtract = data.subtract;

			oTypeName = data.oTypeName;

            newTimes = data.timeList;
			orderFees = data.payAmtList;
			channelNames = data.channelList;
			channelNameAmts = data.channelAmtList;
			payTransNum = data.payNumList;
			channelNums = data.channelNumList;
			lineChart('交易金额',newTimes,orderFees);
			pieChart('交易金额',channelNames,channelNameAmts);
		}
	});
	
	$("#date_order .btn").click(function(){
		var index=$("#date_order .btn").index(this);
		if(index==1){
			lineChart('交易笔数',newTimes,payTransNum);
		}else{
			lineChart('交易金额',newTimes,orderFees);
		}
	});
	
	$("#type_order .btn").click(function(){
		var index=$("#type_order .btn").index(this);
		if(index==1){
			pieChart('交易笔数',channelNames,channelNums);
		}else{
			pieChart('交易金额',channelNames,channelNameAmts);
		}
	});
})

function lineChart(title,xValue,yValue){
	var option = {
    		tooltip: {
    	        trigger: 'axis'
    	    },
    	    xAxis: [{
    	        type: 'category',
    	        data: xValue,
                nameTextStyle:{
                	color:'#C6D0D8',
        			fontSize:'14px',
        			fontFamily:'Adobe Heiti Std'
                },
    	        axisLine: {
    	            lineStyle: {
    	                color: "#999"
    	            }
    	        }
    	    }],
    	    yAxis: [{
    	        type: 'value',
    	        nameTextStyle:{
                	color:'#C6D0D8',
        			fontSize:'14px',
        			fontFamily:'Adobe Heiti Std'
                },
    	        splitLine: {
    	            lineStyle: {
    	                type: 'dashed',
    	                color: '#DDD'
    	            }
    	        },
    	        axisLine: {
    	            show: false,
    	            lineStyle: {
    	                color: "#333"
    	            },
    	        },
    	        splitArea: {
    	            show: false
    	        }
    	    }],
    	    grid: {
	            left: '8%',
	            right: '8%',
	            top: '8%',
	            containLabel: true
	           },
    	    series: [{
    	        name: title,
    	        type: 'line',
    	        data: yValue,
    	        lineStyle: {
    	            normal: {
    	                width: 5,
    	                color: '#47B3FF',
                        shadowColor: '#47B3FF',
    	                shadowBlur: 10,
    	                shadowOffsetY: 20
    	            }
    	        },
    	        itemStyle: {
    	            normal: {
    	                color: '#fff',
    	                borderWidth: 10,
    	                /*shadowColor: 'rgba(72,216,191, 0.3)',
    	                shadowBlur: 100,*/
    	                borderColor: "#fff"
    	            }
    	        },
    	        smooth: true
        }]
    };
	
	myChart.setOption(option);
}


function showAdmin(){

	$.ajax({
		url : ctx + "/platform/showAdmin",
		type : "POST",
		success : function(data) {
            var loginName=data.loginName;
			if(loginName!="admin"){
				$(".menuHide").css({"display":"none"});
			}
			else{
				$(".new_fans").css({"display":"none"});
			}
		
		}
	});
}
let index;
function showTips(obj){
	index = layer.tips('<div>' +
		'<p>同时段交易环比</p>' +
		'<p>今日交易金额：'+toDayTransCount+'元</p>' +
		'<p>昨日交易金额：'+yestDayNowTransAmt+'元</p>' +
		'<p>'+oTypeName+':'+subtract+'元</p>' +
		'<p>环比：'+scale+'</p>' +
		'</div>', obj,{time: 0, area: ['13%', '13%']});
}

function closeTips(){
	layer.close(index);
}

function pieChart(title,xValue,yValue){
	let data = [];
	for (let i=0; i<xValue.length; i++) {
		data.push({name: xValue[i], value: yValue[i]})
	}
	var option = {
			tooltip: {
				trigger: 'item',
				formatter: "{a} <br/>{b}: {c} ({d}%)"
			},
			color: ['#0eb8a2', '#2668e2', '#f08b28'],
			legend: {
				orient: 'vertical',
				data: xValue,
				top:'20%',
				right:'10%',
				textStyle:{
                	color:'#4A5A7F',
        			fontSize:'16',
        			fontFamily:'Adobe Heiti Std'
                }
				
			},
			grid: {
	            left: '8%',
	            right: '8%',
	            top: '8%',
	            containLabel: true
	        },
			series: [{
				name: title,
				type: 'pie',
				radius: ['50%', '70%'],
				avoidLabelOverlap: false,
				label: {
					normal: {
						show: true,
						position: 'outside',
						formatter: "{b}\n{c}\t({d}%)"
					},
					emphasis: {
						show: true,
						position: 'center',
						textStyle: {
							fontSize: '16',
							fontWeight: 'bold'
						},
						formatter: "{b}\n{c}\t({d}%)"
					}
				},
				labelLine: {
					normal: {
						show: true
					}
				},
				data: data
			}]
		};
	myChart2.setOption(option);
}

