//待返佣
var wait_list = [];
//已返佣
var did_list = [];
//已退款
var out_list = [];

var sel_order_status = '1';

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

function httpLoadInfo() {
	wait_list = [];
	did_list = [];
	out_list = [];
	$.ajax({
		type: "post",
		url: base_api + '/fx/?r=user/fx-user/order-list',
		dataType: 'json',
		success: function(res_data) {
			ZZLog(res_data);
			if(false == res_data['flag']) {
				if(codeNotLogin == res_data['code']) {
					//					alert(infoNotLogin)
					var URL = '/web/fx/order_list.html?fx_uid='+getFxUid();
					URL = encodeURI(URL);
					location.href = res_data['url'] + URL;
				} else if(codeNotRegister == res_data['code']) {
					location.href = 'register.html';
				} else {
					alert(res_data['msg']);
				}
			} else {
				//..
				var data = res_data['data'];
				var order_list = data['order_list'];
				for(var i = 0, m = order_list.length; i < m; i++) {
					var dict = order_list[i];
					if(dict['status'] == '1') { //待返佣
						wait_list.push(dict);
					} else if(dict['status'] == '2') { //已结算
						did_list.push(dict);
					} else if(dict['status'] == '3') { //已退款
						out_list.push(dict);
					}
				}
				//默认展示待返佣
				selectOrderStatus(sel_order_status);

			}
		},
		error: function() {
			alert(infoApiError);
		}
	});
}

function selectOrderStatus(status) {
	var tempListAry = [];
	console.log('did_list', did_list);
	var HTML = '';
	if(status == '1') {
		tempListAry = wait_list;
	} else if(status == '2') {
		tempListAry = did_list;
	} else {
		tempListAry = out_list;
	}
	for(var k = 0; k < tempListAry.length; k++) {
		var dict = tempListAry[k];
		var tempHTML = getHTMLWithParam(dict);
		HTML += tempHTML;
	}
	$('.list_info').html(HTML);

	if(tempListAry.length == 0) {
		$('#no_info').show();
		return;
	} else {
		$('#no_info').hide();
	}
}

function getHTMLWithParam(dict) {
	var useStr = '';
	var fontStr = '';

	//1:待返佣   2:已返佣  3.已退款
	if('1' == dict['status']) {
		if(dict['refund_flag'] == 1) {
			useStr = '<div class="ub-f1 tx-r ulev0" style="color: rgb(66,107,242);padding-right: 0.12rem;" onclick="refundTicket(' + dict['order_id'] + ')">退款</div>';
		}
		fontStr = '<div class="ulev0 ui_p_b20" style="color: #ef5b4c;">佣金：¥' + dict['commission_total_money'] + '</div>'
	} else if('2' == dict['status']) {
		useStr = '';
		fontStr = '<div class="ulev0 ui_p_b20" style="color: #80889a;">佣金：¥' + dict['commission_total_money'] + '</div>';
	} else if('3' == dict['status']) {
		useStr = '<div class="ub-f1 tx-r ulev0" style="color:#80889a; padding-right: 0.12rem;">已退款</div>';
		fontStr = '<div class="ulev0 ui_p_b20" style="color:#ef5b4c;">佣金：¥' + dict['commission_total_money'] + '</div>';
	}
	var html = '<div class="bg_color">' +
		'<div class="lightgreen_color ub div_line order_div">' +
		'<div class="tx-l ulev0" style="color: rgb(128,136,154);padding-left: 0.12rem;">下单时间：' + dict['create_time'] + '</div>' + useStr +
		//					'<div class="ub-f1 tx-r ulev0" style="color: rgb(66,107,242);padding-right: 0.12rem;">退款</div>'+
		'</div>' +
		'<div class="ub ub-ver lightgreen_color">' +
		'<div class="ub ub-ver">' +
		'<div class="ub ub-ver" style="padding-left: 0.12rem;">' +
		'<div class="ui_p_t15 ulev1" style="color: rgb(43,56,87);">' + dict['pro_cate_name'] + '</div>' +
		'<div class="ulev0" style="color: rgb(128,136,154);">订单号：' + dict['order_id'] + '</div>' +
		'</div>' +
		'<div class="ub" style="padding-left: 0.12rem;">' + fontStr +
		//							'<div class="ulev0 ui_p_b20" style="color: #ef5b4c;">待返佣：10元</div>'+
		'<div class="ub ub-f1 ub-pe" style="padding-right: 0.12rem;width:1%">' +
		'<div class="ub" style="color: #535187;">' +
		'<div class="ub" style="padding-top: 0.12rem;color: #535187;padding-right:0.02rem">¥</div> ' +
		'<div class="ub-f1 ub ub-ac ulev3" style="color: #535187;">' + dict['order_total_money'] + '</div>' +
		'</div>' +
		'</div>' +
		'</div>' +
		'</div>' +
		'</div>' +
		'</div>';
	return html;

}

//切换订单状态
function changeTheStatus(obj, type) {
	$('.order_type').removeClass('div_title_selected');
	$('.order_type').addClass('div_title_common');
	$(obj).addClass('div_title_selected');
	selectOrderStatus(type);
}

function refundTicket(order_id) {

	var flag = confirm('是否确认退款?\n退款后将无法获取佣金');
	if(flag) {
		var data = {
			order_id:order_id
		}
		ZZLog(base_api + '/fx/?r=prod/prod/cancel-order', data);
		$.ajax({
			type: "post",
			url: base_api + '/fx/?r=prod/prod/cancel-order',
			data: data,
			dataType: 'json',
			success: function(res_data){
				ZZLog(res_data);
				if(false == res_data['flag']) {
					if(codeNotLogin == res_data['code']) {
						location.href = auth_login_url;
					} else if(codeNotRegister == res_data['code']) {
						location.href = 'register.html';
					} else {
						alert(res_data['msg']);
					}
				} else {
					alert('订单退款成功');
					httpLoadInfo();
				}
			},error:function(){
				alert(infoApiError);
			}
		});
	}

}