$(function(){
    /*关闭省市区选项*/
    $("#areaMasks").click(function () {
        var clos = $(this);
        clockAreas(clos);
    });
     $("#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();
	});
    $("input[name='paytype']").bind("click",function(){
        count_total();
    });
	count_total();
	//切换支付方式
	$(".pay_moneyStyle ul li ").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));
		});
	});
});
//计算订单费用
function count_total(){
	var ids=$("input[name='id']").val();
    var url = APP+'Mobile/Index/count_total';

	var query=new Object();
    query.id=ids;
	if($("#all_use_money").is(':checked')){
		query.use_account=1;
	}
	query.account_money=$("input[name='account_money']").val();
    if(!payment_id)payment_id =$("input[name='paytype']:checked").val();
    query.payment_id=payment_id;
	$.ajax({
		url:url,
		data:query,
        type: "post",
		dataType:'json',
		success:function(obj){
			// alert(JSON.stringify(obj));return;
			$("#count_total").html(obj.html);
			//$("input[name='account_money']").val(obj.total_info.account_money);
            if(obj.total_info.payment_info){
                $("#pay_style").html(obj.total_info.payment_info.name);
            }else{
                $("#pay_style").html("请选择支付方式");
            }
            if(obj.total_info.pay_price==0){
                payment_id=0;
            }

		}
	});
}
//提交订单
function submit_order(){
	if($('.btn_checkout').attr('disabled')=='disabled')return false;
    $('.btn_checkout').attr('disabled',true);
    $('.btn_checkout').text('支付中...');
    var ids=$("input[name='id']").val();
    var query=new Object();
    query.id=ids;
    if(!payment_id)payment_id =$("input[name='paytype']:checked").val();
    if($("#all_use_money").is(':checked')){query.use_account=1;}
    query.account_money=$("input[name='account_money']").val();
    query.payment_id=payment_id;
    query.source=1;//来源:手机
    var pay_pwd=$("#pay_pwd").val();
    if(pay_pwd)query.pay_pwd=$("#pay_pwd").val();

	// alert(JSON.stringify(query));return;
     $.ajax({
        url: APP + "Mobile/Index/sub_order",
        type: "post",
        data: query,
        dataType: "json",
        success:function(obj){
            $('.btn_checkout').attr('disabled',false);
            $('.btn_checkout').text('立即支付');
            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){
                if(obj.msg != '请输入支付密码'){
					$.showErr(obj.msg,2,'hc');
				}else{
                    $("#sub_order").next().next().fadeIn();
                    $("#sub_order").next().animate({"bottom": 0});
                    $('#order_pay').attr('style','display:none');
                    $('#paypassword').attr('style','display:block;');
                    $('#payname').html('请输入支付密码');
                }

            }else{

                if(obj.url){
                    window.location.href=obj.url;
                }
            }
        }
   });
}
/*关闭省市区选项*/
function clockAreas(obj) {
    $(obj).fadeOut();
    $(obj).prev().animate({"bottom": "-100%"});
}
