/**
 * Created by web_developer_02 on 2017/11/22
 */
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;
    }
};



window.onload = function(){

    ifLogin(function(flag) {
        httpLoadInfo();
    });

};

function httpLoadInfo(){
    var order_id = getPar('order_id');
    if(order_id===""){
        alert('参数异常，请重试');
        return;
    }
    var res_data = httpSync({
        url:'user/fx-user/get-order-info',
        data:{
            order_id:order_id
        }
    });
    $('body').show();
    if(false === res_data['flag']) {
        if(codeNotLogin === res_data['code']) {
            //					alert(infoNotLogin)
            var URL = '/web/fx/order_detail.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'];

        // 1.订单状态
        var order_info = data['order_info'];
        var order_id = order_info['order_id'];
        //      1 待支付    2：已支付     3：已完成     4：已退款      5:已取消
        var status = order_info['order_status'];
        if('1' === status){
            $('.order_status').find('.status_icon').css('background-image','url(images/order/dai_fu_kuan.png)');
            $('.order_status').find('.status_desc').html('待支付');
        }else if('2' === status){
            $('.order_status').find('.status_icon').css('background-image','url(images/order/dai_fan_yong.png)');
            $('.order_status').find('.status_desc').html('已支付');
        }else if('3' === status){
            $('.order_status').find('.status_icon').css('background-image','url(images/order/yi_fan_yong.png)');
            $('.order_status').find('.status_desc').html('已完成');
        }else if('4' === status){
            $('.order_status').find('.status_icon').css('background-image','url(images/order/yi_tui_kuan.png)');
            $('.order_status').find('.status_desc').html('已退款');
        }else if('5'===status){
            $('.order_status').find('.status_icon').css('background-image','url(images/order/yi_tui_kuan.png)');
            $('.order_status').find('.status_desc').html('已取消');
        }else{}

        $('.order_name').html(order_info['order_name']);

        //实付
        if(0 === order_info['discount_type']){
            var cstr = order_info['total_money']+'元';
            $('.shifu_money').html(cstr);
        }else if(1 === order_info['discount_type']){
            var cstr = '{0} - {1} = {2}元';
            var a = Math.floor(order_info['original_price']);
            var b = Math.floor(order_info['discount_money']);
            var c = Math.floor(order_info['total_money']);
            cstr=cstr.format(a,b,c);
            $('.shifu_money').html(cstr);
        }else{
            var cstr = '{0} + {1} = {2}元';
            var a = Math.floor(order_info['original_price']);
            var b = Math.floor(order_info['discount_money']);
            var c = Math.floor(order_info['total_money']);
            cstr=cstr.format(a,b,c);
            $('.shifu_money').html(cstr);
        }
        // 佣金
        $('.commission').html(order_info['commission']+'元');

        // 2. 预订人信息
        var contacts_info = data['content'];
        $('.contacts_name').html(contacts_info['contacts_name']);
        $('.contacts_phone').html(contacts_info['contacts_phone']);


        // 3. prod_arr
        var prod_arr = data['prod_arr'];
        var html = '';
        for(var i=0;i<prod_arr.length;i++){
            var item = prod_arr[i];
            var tempHTML = '<div class="ub center_one_line">\n' +
                '                    <div class="ub txt_left">'+item['prod_name']+'</div>\n' +
                '                    <div class="ub txt_right">× '+item['prod_num']+'</div>\n' +
                '                </div>';
            html+=tempHTML;
        }
        $('.prod_arr').html(html);

        // 4.日志
        var order_log = data['order_log'];
        var html = '<div class="ub center_one_line">\n' +
            '            <div class="ub txt_left">订单编号</div>\n' +
            '            <div class="ub txt_right">'+order_id+'</div>\n' +
            '        </div>';
        for(var i=0;i<order_log.length;i++){
            var item = order_log[i];
            var tempHTML = '<div class="ub center_one_line">\n' +
                '            <div class="ub txt_left">{name}</div>\n' +
                '            <div class="ub txt_right">{time}</div>\n' +
                '        </div>';
            tempHTML=tempHTML.format(item);
            html+=tempHTML;
        }
        $('.order_log').html(html);
    }
}


/**
 * @param {Object} arg
 * @description ajax同步请求
 * */
function httpSync(arg){
    var param={
        type:arg.type || 'post',
        url:arg.url || '',
        async:false,
        dataType:arg.dataType || 'json',
        data:arg.data || {}
    };
    var rep={};
    $.ajax({
        type: param.type,
        url:base_api+param.url,
        async: param.async,
        dataType: param.dataType,
        data:param.data,
        success: function(res_data) {
            ZZLog(res_data);
            rep=res_data;
        },
        error: function() {
            alert(infoApiError);
        }
    });
    return rep;
}
