var hz_shang = dc("hz_shang");
var wm2 = false;
var isDiamond = false;
hz_shang.style.display = 'inline-block';
var barChart = echarts.init(document.getElementById('barChart'));
var types = "7Day";
var getOption = function(chartType) {
	var chartOption = {
		title: {
			subtext: '单位(元)'
		},
		tooltip: {
			trigger: 'axis'
		},
		grid: {
			x: 45,
			x2: 10,
			y: 60,
			y2: 25
		},
		toolbox: {
			show: false,
			feature: {
				saveAsImage: {
					show: true
				}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			data: ['1日', '2日', '3日', '4日', '5日', '6日', '7日']
		}],
		yAxis: [{
			type: 'value'
		}],
		series: [{
			name: '收入',
			type: 'bar',
			itemStyle: {
				normal: {
					color: function(params) {
						var colorList = ['#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc'];
						return colorList[params.dataIndex]
					}
				}
			},
			label: {
				normal: {
					show: true,
					position: 'top'
				}
			},
			data: [0, 0, 0, 0, 0, 0, 0]
		}]
	};
	return chartOption
};
var getDiamondOption = function(chartType) {
	var chartOption = {
		title: {
			subtext: '单位(台)'
		},
		tooltip: {
			trigger: 'axis'
		},
		grid: {
			x: 45,
			x2: 10,
			y: 60,
			y2: 25
		},
		toolbox: {
			show: false,
			feature: {
				saveAsImage: {
					show: true
				}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			data: ['1日', '2日', '3日', '4日', '5日', '6日', '7日']
		}],
		yAxis: [{
			type: 'value'
		}],
		series: [{
			name: '收入',
			type: 'bar',
			itemStyle: {
				normal: {
					color: function(params) {
						var colorList = ['#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc', '#55c0dc'];
						return colorList[params.dataIndex]
					}
				}
			},
			label: {
				normal: {
					show: true,
					position: 'top'
				}
			},
			data: [0, 0, 0, 0, 0, 0, 0]
		}]
	};
	return chartOption
};
mui.ready(function() {
		mui.ajax(base_path+'/statistics/is_sales_man?dates=' + new Date().getTime(), {
			data: {},
			dataType: 'json',
			type: 'get',
			timeout: 15000,
			success: function(dt) {
				if (typeof(dt.isRedirect) == "undefined"){ 
					if(dt.status) {
						var src='';
						if(dt.data.sRoleId==1){
							$("#yewu").show();
							src=base_path + "/statistics/sales_management.html";
						}else if(dt.data.sRoleId==2){
							$("#guanli").show();
							src=base_path + "/statistics/total_management.html";
						}
						mui('body').on('tap', '#guanli .mui-table-view-cell', function() {
							mui.openWindow({url: src});
						});
					}
				}else if(dt.isRedirect){
					mui.openWindow({url:dt.redirectURL});
				}
			},
			error: function(xhr, type, errorThrown) {
				//异常处理；
				console.log(xhr);
			}
	});
	document.querySelector('.loading-bg').style.display = 'none';
	fenxiao_center_data();
	pulldata();
});
mui.each(document.querySelectorAll('#tabbar-chart-bar .mui-control-item'), function(index, el) {
	el.addEventListener('tap', function() {
		switch(index) {
			case 0:
				types = "7Day";
				pulldata();
				break;
			case 1:
				types = "6Mon";
				pulldata();
				break;
			case 2:
				types = "Year";
				pulldata();
				break;
			default:
				break
		}
	}, false)
});

function pulldata() {
	var btnArray = ['否', '是'];
	var url = base_path + '/rebackStatic/statisInfo';
	var count = 1;
	if(isDiamond) {
		url = base_path + '/rebackStatic/getDiamondStaticsInfo';
		count = 2
	}
	mui.ajax(url + '?dates=' + new Date().getTime(), {
		data: {
			"openid": "230",
			"type": types
		},
		dataType: 'json',
		type: 'post',
		timeout: 15000,
		success: function(dt) {
			if(dt.status) {
				var counts = [];
				var dates = [];
				mui(dt.data).each(function(index) {
					if(count == 1) {
						counts[index] = accDiv(this.amount, 100)
					} else if(count == 2) {
						counts[index] = this.salesNumber
					}
					dates[index] = this.strDate
				});
				barChart.setOption({
					xAxis: {
						data: dates
					},
					series: [{
						name: '收入',
						data: counts
					}]
				})
			} else {
				mui.confirm(dt.message, ' ', btnArray, function(e) {
					if(e.index == 1) {
						setTimeout(function() {
							location.href = domanUrl + base_path + "/drp/indexPage?dates=" + new Date().getTime()
						}, 5000)
					}
				})
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
		}
	})
};

function fenxiao_center_data() {
	var btnArray = ['否', '是'];
	mui.ajax(base_path + '/placeInfo/showPlaceInfoCenter?dates=' + new Date().getTime(), {
		dataType: 'json',
		type: 'post',
		timeout: 15000,
		success: function(dt) {
			if(dt.status) {
				$('#salesName').html("下线 · 销量");
				if(dt.data.salesName) {
					isDiamond = true;
					barChart.setOption(getDiamondOption('bar'));
					$('#balanceLi').hide();
					$('#waitAccounts').hide()
				} else {
					isDiamond = false;
					barChart.setOption(getOption('bar'));
					$('#balanceLi').show();
					$('#waitAccounts').show()
				}
				$('#salesName').val(dt.data.salesName);
				document.querySelector('#to_accounts').innerHTML = accDiv(dt.data.unIncomeMoney, 100) + '元';
				document.querySelector('.total_money').innerHTML = accDiv(dt.data.totalMoney, 100) + '元';
				document.querySelector('.tx').innerHTML = '<img class="wechat_tx" src="' + dt.data.headImg + '">';
				if(dt.data.wm2 == "true") {
					wm2 = true;
					document.querySelector(".fenxiao_wm2").style.display = 'inline-block';
					document.querySelector(".fenxiao_share").style.display = 'inline-block'
				}
				document.querySelector('.role_name').innerHTML = dt.data.role;
				document.querySelector('.user_name').innerHTML = dt.data.userName;
				document.querySelector('.wechat_name').innerHTML = dt.data.nickName;
				var shareOpenid = dt.data.shareOpenid;
				var link = domanUrl + base_path + "/drp/share2WM?shareOpenid=" + shareOpenid;
				window.repleFlag = false;
				window.wxFriend = {
					"appId": "",
					"imgUrl": 'http://static.iamberry.com/tooth/images/shop/index.jpg',
					"link": link,
					"desc": "美国标准，4级净化，5档水温，废水比3:1，水质TDS智能显示。",
					"title": "Watero，美国高端净水专家，4级净化，5档水温，免安装饮水一体机。",
					"netError": "您的网络异常，请刷新重试!如多次刷新无效，请向我们反应 ~",
					"shareTimeSuccess": "分享成功",
					"shareTimeCancel": "已取消分享",
					"shareAppSuccess": "分享成功",
					"shareAppCancel": "已取消分享"
				};
				getConfig();
				pulldata()
			} else {
				mui.confirm(dt.message, ' ', btnArray, function(e) {
					if(e.index == 1) {
						setTimeout(function() {
							location.href = domanUrl + base_path + "/drp/indexPage?dates=" + new Date().getTime()
						}, 5000)
					}
				})
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log(xhr);
		}
	})
};
function tixian() {
	window.location.href = base_path + "/drp/withdrawalsList";
};

function getIncomeRecord() {
	window.location.href = base_path + "/drp/UnIncomeOrderList";
};

function shareH5() {
	window.location.href = "http://www.watero.cn/watero_wap_app_1.2/index.html";
};

function xialianlist() {
	if(wm2) {
		window.location.href = base_path + "/drp/xialianUser2";
	} else {
		window.location.href = base_path + "/drp/xialianUser";
	}
};

function weiruzhang() {
	if(wm2) {
		window.location.href = base_path + "/drp/UnIncomeOrderList";
	} else {
		window.location.href = base_path + "/drp/UnIncomeOrderList2";
	}
};

function fenxiao_wm2() {
	window.location.href = "/tooth/wechat/drp/share2WM";
};