$(function(){
	$("#add_address").click(function(){
		add_address();
	});
     $("#account_money").bind("blur",function(){
         $("#all_use_money").prop("checked","checked");
         count_total();
     });
	$("#all_use_money").click(function(){
        if($("#all_use_money").prop("checked")==false){
            $("#all_use_money").prop("checked",'')
            $("#account_money").val(0);
        }
		count_total();
	});
	$("#all_use_score").click(function(){
		count_total();
	});
	count_total();
});
//添加,修改收货地址 
 function add_address(){
	var query = $("#address_form").serialize();
	var url= $("#address_form").attr('action');
	$.ajax({
		url:url,
		data:query,
		dataType:'json',
		success:function(data){
			if(data.status){
				$.showSuccess(data.msg,0,'hc');
				location.reload();
			}else{
				$.showErr(data.msg,0,'hc');
			}
		}
	}); 
 } 
 //添加,修改收货地址,弹窗
 function add_r(id){
	 if(id>0){
		var title='修改收货地址';
		$.openUrl(APP+'Home/Order/address/id/'+id+'/title/'+title+'.html','修改收货地址',id, '800', function () {
                    return false;
                }, function () {
                    return false;
                }); 
	 }else{
		 var title='添加收货地址';
		 $.openUrl(APP+'Home/Order/address/title/'+title+'.html','添加收货地址', 'ID', '800', function () {
                    return false;
                }, function () {
                    return false;
                });
	 }
 }
//添加修改增值税
function add_vat(){
    var query = $("#sub_vat_form").serialize();
    var url= $("#sub_vat_form").attr('action');
    $.ajax({
        url:url,
        type: "post",
        data:query,
        dataType:'json',
        success:function(obj){
            if(obj.status){
                $.showSuccess(obj.msg,0,'hc');
                window.location.reload();
            }else{
                $.showErr(obj.msg,0,'hc');
            }
        }
    });
}
//获取公司名称
function get_name(obj){
    if($(obj).val()==3){
        $("#con_name").show();
    }else{
        $("#con_name").hide();
    }
}
 //获取优惠券的金额
 function get_ticketmoney(obj,id){
   var money="-"+foramtmoney(obj.value,2);
    // alert(money);
     $("#ticket_money"+id).html(money);
     count_total();
 }
 //改变运费方式
 function get_shipping_free(obj,id){
     var money="-"+foramtmoney(obj.value,2);
     // alert(money);
     $("#shipping_free"+id).html(money);
 }
 //切换收货地址
 $(function(){
    $(".modify").each(function(){
        $(this).bind('click',function(e){
        var id = $(this).attr('rel');
        if(id>0){
            var title='修改收货地址';
            $.openUrl(APP+'Home/Order/address/id/'+id+'/title/'+title+'.html','修改收货地址',id, '800', function () {
                        return false;
                    }, function () {
                        return false;
                    }); 
        }else{
            var title='添加收货地址';
            $.openUrl(APP+'Home/Order/address/title/'+title+'.html','添加收货地址', 'ID', '800', function () {
                        return false;
                    }, function () {
                        return false;
                    });
        }
        return false;
        });
    });
	$("div[id='addr_div']").each(function(k){
		$(this).bind('click',function(e){
            $("div[id='addr_div']").removeClass('on');
			$(this).addClass('on');
			var id = $(this).attr('data');
			$("input[name='addr_id']").val(id);
            var ids=$("#ids").val();
            count_total();
            if(ids){
                window.location.href=APP+"Home/Order/index/addre_id/"+id+"/ids/"+ids;
            }else if(order_info.type==1){
                window.location.href=APP+"Home/Order/index/goods_id/"+order_info.goods_id+"/number/"+order_info.number+"/goods_attr/"+order_info.goods_attr+"/act/"+order_info.act+"/type/"+order_info.type+"/addre_id/"+id;
            }else if(order_info.type==3){
                window.location.href=APP+"Home/Order/index/goods_id/"+order_info.goods_id+"/number/"+order_info.number+"/goods_attr/"+order_info.goods_attr+"/integra/"+order_info.integra+"/type/"+order_info.type+"/addre_id/"+id;
            }

		});
	});
     //切换支付方式
     $(".pay-ways-ul li").each(function(k){
         $(this).bind('click',function(){
             $(".pay-ways-ul li").removeClass('on');
             $(this).addClass('on');
              payment_id =$(this).find("input[name='paytype']").val();
             //alert(payment_id);
             count_total($(this));
         });
     });
 });
//计算订单费用
function count_total(){
	var ids=$("input[name='ids']").val();
    var type=parseInt($("#is_addcart").val());

    var bouns= new Array();
    var shop_id=new Array();
   $(".ticket option:selected").each(function(){
       shop_id.push($(this).attr('data'));
      bouns.push($(this).attr('attr'));
    });
   //alert(bouns);
    //alert(shop_id);
    //alert(act);
	var query=new Object();
	 query.order_info=new Object();
    if(ids){
        query.ids=ids;
    }else if(order_info){
        query.order_info=order_info;
    }

    query.shop_id=shop_id;
    query.bouns=bouns;
	query.addr_id=$("input[name='addr_id']").val();
	if($("#all_use_money").is(':checked')){
		query.use_account=1;
	}
	query.account_money=$("input[name='account_money']").val();
    if(type==3){
        query.score=1;
    }else{
        if($("#all_use_score").is(':checked')){
            query.score=1;
        }
    }
    //alert(payment_id);
	query.payment_id=payment_id;
    if(type==3){
        query.type=3;
    }else{
        query.type=1;
    }
    //alert($("input[name='paytype']:checked").val());
	$.ajax({
		url:APP+'Home/Order/count_total',
		data:query,
        type: "post",
		dataType:'json',
		success:function(obj){
			$("#count_total").html(obj.html);
			$("input[name='account_money']").val(obj.total_info.account_money);
            //alert(obj.total_info.pay_price);
            if(obj.total_info.pay_price==0){
                $(".pay-ways-ul li").each(function(k){
                    $(this).removeClass('on');
                    payment_id=0;
                })
            }
		}
	});
}
//提交订单
var is_submit=false;
function submit_order(){
    if(is_submit){
        return true;
    }
    var ids=$("input[name='ids']").val();
    var type=parseInt($("#is_addcart").val());
    var bouns= new Array();
    var shop_id=new Array();
    $(".ticket option:selected").each(function(){
        bouns.push($(this).attr('attr'));
        shop_id.push($(this).attr('data'));
    });
    var query=new Object();
    if(ids){
        query.ids=ids;
    }else if(order_info){
        query.order_info=order_info;
    }
    query.bouns=bouns;
    query.shop_id=shop_id;
    query.addr_id=$("input[name='addr_id']").val();
    if($("#all_use_money").is(':checked')){
        query.use_account=1;
    }
    query.account_money=$("input[name='account_money']").val();

    query.payment_id=payment_id;
    if(type==3){
        query.type=3;
        query.score=1;
    }else if(type == 6){
        query.type=6;
        query.pt_id = $('#pt_id').val();
        if($("#all_use_score").is(':checked')){
            query.score=1;
        }
    }else{
        query.type=1;
        if($("#all_use_score").is(':checked')){
            query.score=1;
        }
    }
    query.source=0;//来源:电脑
    //发票信息
    query.invoice_type=$("input[name='invoice_type']:checked").val();
    if($("input[name='is_goods']").is(':checked')){
        query.is_goods=1;
    }
    query.con_name=$("input[name='con_name']").val();
  
   /* if(!payment_id && !query.account_money && !query.score){
        $.showErr("请选择支付方式",0,'hc');return false;
    }*/
    //alert($("input[name='paytype']:checked").val());
    $.ajax({
        url: APP + "Order/sub_order",
        type: "post",
        data: query,
        dataType: "json",
        success:function(obj){
            alert(JSON.stringify(obj));
            $.weeboxs.close('tui_box');
            if(obj.status==1){
                if(obj.pay){
                    window.location.href=APP+"Pay/index/id/"+obj.pay;
                }else{
                    $.showSuccess("支付成功",0,'hc')
                    window.location.href=APP+"Member/OrderInfo/index";

                }
            }else if(obj.status==3){
                var html = '交易密码：<input type="password" name="pay_pwd" id="pay_pwd" class="ui-textbox" style="border: 1px solid #ddd;height: 26px;line-height: 26px;width: 200px;margin-left: 5px;"/>';
                $.weeboxs.open(html, {
                    boxid: 'tui_box',
                    contentType: 'text',
                    showButton: true,
                    showCancel: false,
                    showOk: true,
                    title: '输入交易密码',
                    width: 250,
                    type: 'hc',
                    onopen: function () {
                    }, onok: function () {

                        is_submit=true;
                        query.pay_pwd=$("input[name='pay_pwd']").val();
                        $.ajax({
                            url: APP + "Order/sub_order",
                            type: "post",
                            data: query,
                            dataType: "json",
                            success:function(obj){
                                if(obj.status==1){
                                    if(obj.pay){
                                        window.location.href=APP+"Pay/index/id/"+obj.pay;
                                    }else{
                                        $.showSuccess("支付成功",0,'hc')
                                        window.location.href=APP+"Member/OrderInfo/index";

                                    }
                                }else{
                                    $.showErr(obj.msg,0,'hc');is_submit=false;
                                    if(obj.url){
                                        window.location.href=obj.url;
                                    }
                                }
                            }
                        });
                    }
                })
            }else{

                $.showErr(obj.msg,0,'hc');is_submit=false;
                if(obj.url){
                    window.location.href=obj.url;
                }
            }

        }
    });



}
//更改发票类型
function change_invoice(k){
    if(k==3){
        $.openUrl(APP+'Order/vat_invoice.html','增值税专用发票信息','id', '800', function () {
            return false;
        }, function () {
            return false;
        });
    }
    if(k==0){
        $("#invoice_info").hide();
        $("#invoice_remark").hide();
    }else{
        $("#invoice_info").show();
        $("#invoice_remark").show();
    }
}
 