


String.prototype.format = function() {
    if(arguments.length === 0) return this;
    var param = arguments[0];
    var s = this;
    if(typeof(param) === 'object') {
        for(var key in param)
            s = s.replace(new RegExp("\\{" + key + "\\}", "g"), param[key]);
        return s;
    } else {
        for(var i = 0; i < arguments.length; i++)
            s = s.replace(new RegExp("\\{" + i + "\\}", "g"), arguments[i]);
        return s;
    }
};

var _data = {
    current_page:1,
    page_size:10,
    is_refund:'',
    status:''
};

var order_list_arr = [];
window.onload = function() {
    // $('body').show();return;
	ifLogin(function(flag) {
		$('body').show();
		_data['status'] = '1';
		httpLoadInfo();
	});
};

function httpLoadInfo() {

	$.ajax({
		type: "post",
		url: base_api + 'user/fx-user/order-list',
		data:_data,
		dataType: 'json',
		beforeSend:function(){
            loading.show();
		},
		complete:function(){
            delay(function(){
                loading.close();
			})
		},
		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';
					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'];
				order_list_arr.splice(0,order_list_arr.length);
                order_list_arr=order_list_arr.concat(order_list);
                // 1 待支付    2：已支付     3：已完成     4：已退款      5:已取消
				var html = getOrderListHTML(order_list);
                $('.list_info').html(html);

                if(order_list.length<_data['page_size']) $('.more').hide();
                else $(".more").show();
                if(order_list.length === 0) $('#no_info').show();
                else $('#no_info').hide();


			}
		},
		error: function() {
            $('.more').hide();
            $('#no_info').show();
			alert(infoApiError);
		}
	});
}



function getOrderListHTML(tempListAry){
    var HTML = '';
    for(var k = 0; k < tempListAry.length; k++) {
        var dict = tempListAry[k];
        var temp = getHTMLWithParam(dict,k);
        HTML += temp;
    }
    return HTML;
    // $('.list_info').html(HTML);
    // // if(tempListAry.length<page_size) $('.more').hide();
    //
    // if(tempListAry.length == 0) {
    //     $('#no_info').show();
    // } else {
    //     $('#no_info').hide();
    // }
}


function getHTMLWithParam(dict,index) {

	var htmlObj = dict;
	htmlObj['index']=index;
	htmlObj['prod_arr_html']='';
    htmlObj['bottom_btn_html']='';
    htmlObj['commission_price_html']='';
    htmlObj['order_status_desc'] = '';

    htmlObj['order_status_desc']=$('.div_title_selected').html();


	var prod_arr = dict['prod_arr'];
	var prod_arr_html = '';
	for(var i=0;i<prod_arr.length;i++){
		var item = prod_arr[i];
		var tempHTML = '<div class="ulev0" style="color: rgb(128,136,154);margin-bottom:0.05rem;">'+item['prod_name']+'  <span style="margin-left:0.2rem;color: rgb(128,136,154);">× '+item['prod_cnt']+'</span></div>';
		prod_arr_html += tempHTML;
	}
    htmlObj['prod_arr_html'] = prod_arr_html;
	var see_desc = '<div onclick="see_desc('+index+')" class="bottom_btn see_desc ub ub-ac ub-pc">查看详情</div>';
	var update_price = '<div onclick="update_price('+index+')" class="bottom_btn update_price ub ub-ac ub-pc">改价</div>';
	var refund = '<div onclick="refund_ticket('+index+')" class="bottom_btn refund_ticket ub ub-ac ub-pc">退款</div>';

	var menu_id = $('.div_title_selected').attr('data-menu-id');
	if(menu_id==='1' || menu_id==='2'){
        htmlObj['commission_price_html'] = '<div class="ulev0" style="color: #ef5b4c;">佣金：'+dict['commission_total_money']+'元</div>';
	}else{
        htmlObj['commission_price_html'] = '<div class="ulev0" style="color: #ef5b4c;visibility: hidden;">佣金：'+dict['commission_total_money']+'元</div>';
	}

	if('1' === dict['order_status']){
        htmlObj['bottom_btn_html'] += see_desc;

        //是否可以改价
        if(1 === dict['change_price']){
            htmlObj['bottom_btn_html'] += update_price;
		}
	}else if('4' === dict['order_status']){
        htmlObj['bottom_btn_html'] = see_desc;
	}else{
        if('1' === dict['status']){
            if(1 === dict['refund_flag']){
                htmlObj['bottom_btn_html'] += see_desc;
                htmlObj['bottom_btn_html'] += refund;
            }
        }else if('2' === dict['status']){
            htmlObj['bottom_btn_html'] = see_desc;
        }else if('3' === dict['status']){
            htmlObj['bottom_btn_html'] = see_desc;
        }
	}



	var html = '<div class="bg_color cell_one" data-index="{index}" style="box-shadow: inset 0 -0.5px 0 0 rgba(0, 0, 0, 0.1);">\n' +
        '\t\t\t\t<div class="lightgreen_color ub div_line order_div">\n' +
        '\t\t\t\t\t<div class="ub" style="color: #ef5b4c;padding-left: 0.12rem;">{order_status_desc}</div>\n' +
        '\t\t\t\t\t<div class="tx-l ulev0" style="color: rgb(128,136,154);padding-left: 0.1rem;">{create_time}</div>\n' +
        '\t\t\t\t\t<div class="ub-f1 tx-r ulev0" style="color:#80889a;padding-right: 0.12rem;">订单号：{order_id}</div>\n' +
        '\t\t\t\t</div>\n' +
        '\t\t\t\t<div class="ub ub-ver lightgreen_color">\n' +
        '\t\t\t\t\t<div class="ub ub-ver">\n' +
        '\t\t\t\t\t\t<div class="ub ub-ver" style="padding-left: 0.12rem;">\n' +
        '\t\t\t\t\t\t\t<div class="ui_p_t15 ulev1" style="color: #2b3857;font-weight: 600;font-size:0.14rem;margin-bottom:0.1rem;">{pro_cate_name}</div>\n' +
        '\t\t\t\t\t\t\t<div class="ub" style="padding-bottom:0.085rem;">\n' +
        '\t\t\t\t\t\t\t\t<div class="ub-f1 ub ub-ver" style="width: 1%;">\n' +'{prod_arr_html}'+
        '\t\t\t\t\t\t\t\t</div>\n' +
        '\t\t\t\t\t\t\t\t<div class="ub-f1 ub ub-ver ub-pe ub-ae" style="width: 1%;padding-right: 0.12rem;">\n' +
        '\t\t\t\t\t\t\t\t\t<div class="ub " style="color:#666699">\n' +
        '\t\t\t\t\t\t\t\t\t\t<div class="ub ub-ae" style="color: #535187;">¥</div>\n' +
        '\t\t\t\t\t\t\t\t\t\t<div class="ub-f1 ub ub-ae ulev3" style="color: #535187;line-height: 1;">{order_total_money}</div>\n' +
        '\t\t\t\t\t\t\t\t\t</div>\n' +
		'{commission_price_html}'+
        '\t\t\t\t\t\t\t\t</div>\n' +
        '\t\t\t\t\t\t\t</div>\n' +
        '\t\t\t\t\t\t</div>\n' +
        '\t\t\t\t\t\t<div class="border" style="opacity: 0.1;border-bottom: solid 0.5px #2b3756;"></div>\n' +
        '\t\t\t\t\t</div>\n' +
        '\t\t\t\t</div>\n' +
        '\t\t\t\t<div class="ub ub-pe lightgreen_color" style="padding:0.06rem 0.14rem 0.06rem 0;">\n' +
		'{bottom_btn_html}'+
        '\t\t\t\t</div>\n' +
        '\t\t\t</div>';
	html=html.format(htmlObj);
	return html;

}


function see_desc(index){
    var dict = order_list_arr[index];
    location.href='order_detail.html?order_id='+dict['order_id'];
}
function update_price(index){
    var dict = order_list_arr[index];

    $('.current_price').html('原订单价格：'+dict['order_total_money']+'元，请输入新的价格');
    $('.input_balance').val(dict['order_total_money']);

    $('.balance_info').css('visibility','hidden');
    $('body').css('position','fixed');
    $('.model').show();
    $('.input_balance').focus();
	$('.model').off('click');
    $('.model').on('click',function(e){
        $('body').css('position','');
        $('.model').hide();
    });
    $('.center_div').off('click');
    $('.center_div').on('click',function(e){
        e.stopPropagation();
    });
    $('.btn_update_cancel').off('click');
    $('.btn_update_cancel').on('click',function(){
        $('body').css('position','');
        $('.model').hide();
    });
    $('.btn_update_ok').off('click');
    $('.btn_update_ok').on('click',function(e){
		// e.stopPropagation();


    	var input_balance = $('.input_balance').val();
    	if(input_balance==="")input_balance=0;

    	if(input_balance<=0){
            $('.balance_info').html('只能输入大于0的数');
            $('.balance_info').css('visibility','');
            return;
		}
		if(input_balance === dict['order_total_money']){
            $('.balance_info').html('价格没有变化');
            $('.balance_info').css('visibility','');
    		return;
		}

        var _data = {
        	order_id:dict['order_id'],
			money:input_balance
		};



        $.ajax({
            type: "post",
            url: base_api + 'user/fx-user/change-order-money',
            data:_data,
            async:false,
            dataType: 'json',
            beforeSend:function(){
                loading.show();
            },
            complete:function(){
                delay(function(){
                    loading.close();
                },400);
            },
            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';
                        URL = encodeURI(URL);
                        location.href = res_data['url'] + URL;
                    } else if(codeNotRegister === res_data['code']) {
                        location.href = 'register.html';
                    } else {
                        $('.balance_info').html(res_data['msg']);
                        $('.balance_info').css('visibility','');
                    }
                } else {
                    //..
                    var data = res_data['data'];
                    toast('改价成功',2000);
                    $('body').css('position','');
                    $('.model').hide();
                    $('.balance_info').html('空空');
                    $('.balance_info').css('visibility','hidden');
                    order_list_arr.splice(0,order_list_arr.length);
                    httpList();
                }
            },
            error: function() {
                alert(infoApiError);
            }
        });

    });



}
function refund_ticket(index) {
    var dict = order_list_arr[index];
    var order_id = dict['order_id'];
    var flag = confirm('是否确认退款?\n退款后将无法获取佣金');
    if(flag) {
        var data = {
            order_id:order_id
        };
        ZZLog(base_api + 'prod/prod/cancel-order', data);
        $.ajax({
            type: "post",
            url: base_api + '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 {
                    toast('订单退款成功',300);
                    delay(function(){
                        httpLoadInfo();
					},400);

                }
            }
        });
    }

}

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

	//待支付
	if('0'===type){
        _data['status']='1';
        _data['is_refund']='';
	}else if('1'===type){
		//待返佣
        _data['status']='';
        _data['is_refund']='1';
	}else if('2'===type){
		//已返佣
        _data['status']='';
        _data['is_refund']='2';
	}else if('3'===type){
		//已取消
        _data['status']='4,5';
        _data['is_refund']='';
	}else{}
	_data['current_page']=1;
        $('.more span').text('加载更多');
        $('.more').attr('allow_click','true');
        httpLoadInfo();

}


$('.more').on('click',function(){
    if($(this).attr('allow_click')==='false'){
        return;
    }
	_data['current_page']+=1;
    $('.more span').text('加载中...');
    httpList();
});
function httpList(){
    $.ajax({
        type: "post",
        url: base_api + 'user/fx-user/order-list',
        data:_data,
        dataType: 'json',
        beforeSend:function(){
            loading.show();
        },
        complete:function(){
            $('.more span').text('加载更多');
            loading.close();
        },
        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';
                    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'];
                order_list_arr=order_list_arr.concat(order_list);
                if(order_list.length===0){
                    $('.more span').text('没有更多的数据了');
                    $('.more').attr('allow_click','false');
                }
                // 1 待支付    2：已支付     3：已完成     4：已退款      5:已取消
                var html = getOrderListHTML(order_list_arr);
                $('.list_info').html(html);
            }
        },
        error: function() {
            alert(infoApiError);
        }
    });
}

