//订单简单的变化，比如商家接单
function order_change(obj,order_id,shipping_status,is_count){
    var query=new Object();
    query.id=order_id;
    query.shipping_status=shipping_status;
    query.type=14;
    $.ajax({
        url:APP+"Mobile/ShopsOrderInfo/order_change",
        data:query,
        dataType:'json',
        success:function(data){
            if(data.status){
                if(is_count>0){
                    window.location.reload();
                }else {
                    var obj_parent = $(obj).parent().parent();
                    $('#em'+order_id).html('未配送');
                    obj_parent.html('<dd class="order-all-btn" id="yes_ship_' + order_id + '"><a onclick="ship(' + order_id + ')" class="pay-order">配送</a></dd>');
                }
            }else{
                $.showSuccess(data.info,0,'hc');
            }
        }
    });
}
$(function(){
    //收货地址
    address_id=$('.shouhuo-information a').attr('address_id');
    //选择的配送时间
    best_time=$('.choice_time_item li:first').attr('ke');
    var best_time_format=$('.choice_time_item li:first').text();
    //计算送达时间
    var delivery_time=$('.choice_time_item li:first').attr('se');
    $('p#scheduled_delivery').html(best_time_format+'（大约'+delivery_time+'送达）');
    //送达时间切换
    $('.choice_time_item li').each(function(){
        $(this).bind('click',function(){
            $("#service_time").next().next().fadeIn();
            $("#service_time").next().animate({"bottom": 0});
            best_time=$(this).attr('ke');
            var best_time_format=$(this).text();
            //计算送达时间
            var delivery_time=$(this).attr('se');
            $('p#scheduled_delivery').html(best_time_format+'（大约'+delivery_time+'送达）');
        });
    });
    //切换支付方式
    $("dd.payment_id").each(function(k){
        $(this).bind('click',function(){
            if($(this).find("input[name='paytype']").is(':checked')) {
                payment_id = $(this).find("input[name='paytype']").val();
            }else{
                payment_id = 0;
            }
        });
    });
    //使用余额按钮点击
    $("#all_use_money").bind('click',function(){
        if($(this).prop("checked")==false){
            $(this).prop("checked",'');
            is_account=0;
        }else{
            $(this).prop("checked",'checked');
            is_account=1;
        }
        count_total();
    });
    //使用积分抵按钮点击
    $("#all_use_score").bind('click',function(){
        if($(this).prop("checked")==false){
            $(this).prop("checked",'');
            is_score=0;
        }else{
            $(this).prop("checked",'checked');
            is_score=1;
        }
        count_total();
    });
    //切换支付方式
    $(".order_pay dd ").each(function(k){
        $(this).bind('click',function(){
            if($(this).find("input[name='paytype']").is(':checked')){
                payment_id =$(this).find("input[name='paytype']").val();
            }else{
                payment_id=0;
            }
            count_total($(this));
        });
    });
    $('#footer #sure_submit').bind("click",function(){
        if(is_submit){
            return;
        } else{
            order_submit();
        }
    });
});
//计算订单费用
function count_total(getbonuid,money){
    is_submit=0;
    var query=new Object();
    query.shop_id = shop_id;//门店id
    query.delivery_id = address_id;//收货地址id
    query.score = is_score;//是否积分抵
    query.use_account = is_account;//是否使用余额
    query.payment_id = payment_id;//支付方式
    query.type=14;//订单类型为外卖
    if(getbonuid){
        query.bonu=parseInt(getbonuid);
        query.cmoney=parseFloat(money);
        $('.addhui').addClass('hui');
        $("#hui"+getbonuid).removeClass("hui");
        $("#bonu_id").val(getbonuid);
    }else{
        query.bonu=parseInt($("#bonu_id").val());
    }
    $.ajax({
        url:APP+'Mobile/Takeout/count_total',
        data:query,
        type: "post",
        dataType:'json',
        success:function(obj){
            //alert(JSON.stringify(obj.status));
            if(obj.status==0){
                is_submit=true;
                if(obj.url) {
                    $.openUrl(obj.url, obj.info, 'ID', '800', 'hc', function () {
                        return false;
                    }, function () {
                        return false;
                    });
                }else{
                    $.showErr(obj.info, 0, 'hc');
                    is_score=0;is_account=0;payment_id=0;is_submit=false;
                }
            }else{
                is_submit=false;
                $(".order_cartp").html(obj.htmlp);
            }
        }
    });
}
//提交订单
function order_submit(){
    if(is_submit){
        return;
    }
    var query=new Object();
    var ids=$("input[name='ids']").val();
    query.shop_id = shop_id;//门店id
    query.addr_id = address_id;//收货地址id
    query.score = is_score;//是否积分抵
    query.use_account = is_account;//是否使用余额
    query.account_money=$("input[name='account_money']").val();
    query.payment_id = payment_id;//支付方式
    query.type=14;//订单类型为外卖
    query.best_time=best_time;//最佳送货时间
    query.temp=$('input[name="temp"]').val();//备注信息
    query.source=1;//来源:手机
    if($("#bonu_id").val()){
       query.boun=parseInt($("#bonu_id").val());//优惠券id
    }
    if(ids){
        query.ids=ids;
    }else{
        query.ids=order_info;
    }
    var pay_pwd=$("#pay_pwd").val();
    query.pay_pwd=pay_pwd;
    $.ajax({
        url:APP+'Mobile/Takeout/sub_order',
        data:query,
        type: "post",
        dataType:'json',
        success:function(obj){
            alert(JSON.stringify(obj));
            var msg='';
            if(obj.info)msg=obj.info;
            if(obj.msg)msg=obj.msg;
            $.weeboxs.close('pay_pwd_box');
            if(obj.status==1){
                if(obj.pay){
                    window.location.href=APP+"Mobile/Order/order_success/id/"+obj.pay;
                }else{
                    window.location.href=APP+"Mobile/Order/order_success/order_ids/"+obj.data;
                }
            }else if(obj.status==3){
                $.showErr(msg,2,'hc',function(){
                    $("#way").html("请输入交易密码");
                    choosefs();
                    $('#order_pay').hide();
                    $('#pass').show();
                    $("#footer").next().next().fadeIn();
                    $("#footer").next().animate({"bottom": 0});
                });
            }else if(obj.status==4){
                $.showErr(msg,2,'hc');is_submit=0;
            }else{
                $.showErr(msg,0,'hc');is_submit=0;
                choosefs();
                if(is_account==0){
                    $("#way").html("请选择支付方式");
                    $('#order_pay').show();
                    $('#pass').hide();
                }else{
                    $('#order_pay').show();
                    $('#pass').show();
                    $("#way").html("请输入本站支付密码并选择支付方式");
                }
                if(obj.url){
                    window.location.href=obj.url;
                }
            }
        }
    });
}
/**
 * 发货
 */
function ship(id,is_count){
    var query=new Object();
    query.order_id=id;
    query.order_type=4;//外卖
    $.ajax({
        type:'GET',
        data:query,
        url:APP+"Mobile/ShopsOrderInfo/ship.html",
        success:function(data){
            if(data.status==1){
                if(is_count>0){
                    window.location.reload();
                }else {
                    var str = "<a>已发货</a>";
                    $('#yes_ship_' + id).html(str);
                    $('#yes_ship').val('已配送');
                    $('#yes_ship').attr("disabled", "true");
                }
            }else{
                $.showErr(data.info,1,'hc');
            }
        }
    });
}