window.onload = function() {
	ifLogin(function(flag) {
		$('body').show();
		httpFirstData();
	})
}

//初始化数据
function httpFirstData() {
	var data = getStorJson('my_commission_20170315');
	var remaining_sum = data.remaining_sum;
	var nickname = data.nick_name;
	$('#remaining_sum').text('¥' + remaining_sum);
	$('#nick_name').text(nickname);
}

//查看充值记录
function seeList() {
	window.location.href = 'recharge_list.html?'+version;
}

//微信支付
function weixPay() {
	
	var money = $('.input_balance').val();
	if (money == '') {
		money = 0;
	}
	if(parseInt(money) > 50000) {
		alert('单笔充值金额最高为50000');
		return;
	}
	if(parseInt(money) <= 0) {
		alert('充值金额不能为0');
		return;
	}
	$('#loadingToast').show();
	var data = {
		amount: money
	}
	var urlstr = base_api + 'user/fx-user/recharge';
	$.ajax({
		type: "post",
		url: urlstr,
		data:data,
		dataType: "json",
		success: function(res_data) {
			ZZLog(res_data);
			$('#loadingToast').hide();
			if(false == res_data['flag']) {
				if(codeNotLogin == res_data['code']) {
					location.href = res_data['url'] + '/web/fx/';
				} else if(codeNotRegister == res_data['code']) {
					location.href = 'register.html';
				} else {
					alert(res_data['msg']);
				}
			} else {
				ZZLog(res_data);
				call(res_data['data']);
			}
		},
		error: function() {
			$('#loadingToast').hide();
			alert(infoApiError);
		}
	});
}

function call(pay_data) {
	if(typeof WeixinJSBridge == "undefined") {
		if(document.addEventListener) {
			document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		} else if(document.attachEvent) {
			document.attachEvent('WeixinJSBridgeReady', jsApiCall);
			document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		}
	} else {
		jsApiCall(pay_data);
	}
}
//调用微信JS api 支付
function jsApiCall(pay_data) {
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest',
		pay_data,
		function(res) {
			WeixinJSBridge.log(res.err_msg);
			if(res.err_msg == 'get_brand_wcpay_request:ok') {
				setTimeout(function() {
					location.href = 'pay_success.html?from=balance';
				}, 1000);
			} else if(res.err_msg == 'get_brand_wcpay_request:cancel') {
				alert('支付取消');
			} else {
				alert('支付失败');
			}
		})
}