$(function(){
     $("#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();
	});
    $("input[name='paytype']").bind("click",function(){
        count_total();
    });
    $("input[name='ticket']").bind("click",function(){
        count_total();
    });
	count_total();

	//切换收货地址
	$("div[id='addr_div']").each(function(k){
		$(this).bind('click',function(){
			$("div[id='addr_div']").removeClass('on');
			$(this).addClass('on');
			var id = $(this).attr('data');
			$("input[name='addr_id']").val(id);
			count_total();
		});
	});
	//切换支付方式
	$(".pay_styles dl dt ").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='ids']").val();
    var type=parseInt($("#is_addcart").val());
    var bonus_list=document.getElementsByName("ticket");
    var bouns= new Object();
    var shop_id=new Array();
    if(order_info==10){
        var supplierid=$("input[name='supplier_id[]']");
        for(var i=0;i<supplierid.length;i++){
            shop_id.push($(supplierid[i]).val());
        }
        var url = APP+'Mobile/PurOrder/count_total';
    }else{
        for(var i=0;i<bonus_list.length;i++){
            if (bonus_list[i].checked) {
                bouns.id=$(bonus_list[i]).attr('attr');
                bouns.money=$(bonus_list[i]).val();
                $(".ticket_style").html("￥"+$(bonus_list[i]).val()+"<i class='iconfont'>&#xe662;</i>");
                $(bonus_list[i]).prev().html("已使用");

            }else{
                $(bonus_list[i]).prev().html("立即使用");
            }
        }
        var url = APP+'Mobile/Order/count_total';
    }
	var query=new Object();
    if(ids){
        query.ids=ids;
    }else if(order_info){
        query.order_info=order_info;
    }
    var sendtype = $("input[name='send_type']:checked") .val();
    query.send_type=sendtype;
    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(!payment_id)payment_id =$("input[name='paytype']:checked").val();
    query.payment_id=payment_id;
    if(type==3){
        query.type=3;
        query.score=1;
    }else if(type==14){
        query.type=14;
        if($("#all_use_score").is(':checked')){
            query.score=1;
        }
        query.send_type=$("input[name='send_type']:checked") .val();
    }else if(type==6) {
        query.type = 6;
        if ($("#all_use_score").is(':checked')) {
            query.score = 1;
        }
        query.send_type = $("input[name='send_type']:checked").val();
    }else if(type==11) {
        query.type = 11;
        if ($("#all_use_score").is(':checked')) {
            query.score = 1;
        }
        query.send_type = $("input[name='send_type']:checked").val();
    }else{
        query.type=1;
        if($("#all_use_score").is(':checked')){
            query.score=1;
        }
    }
    if(order_info == 10){
        query.type=10;
    }
    console.log(url)
	$.ajax({
		url:url,
		data:query,
        type: "post",
		dataType:'json',
		success:function(obj){
            console.log(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='ids']").val();
    var type=parseInt($("#is_addcart").val());
    var bonus_list= document.getElementsByName("ticket");
    var bouns= new Object();
	if(type == 11 || type == 12 || type == 14){
		var shop_id = $('#s_id').val();
        for(var i=0;i<bonus_list.length;i++){
            if (bonus_list[i].checked) {
                bouns.id=$(bonus_list[i]).attr('attr');
                bouns.money=$(bonus_list[i]).val();
            }
        }
	}
    
    var query=new Object();
    if(ids){
        query.ids=ids;
    }else if(order_info){
        query.order_info=order_info;
    }
if(!payment_id)payment_id =$("input[name='paytype']:checked").val();
    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;
	var temp = $("input[name='temp']").val();
	query.temp=temp;
    if(type==3){
        query.type=3;
        query.score=1;
        var shop_id = $('#s_id').val();
        var sendtype = $("input[name='send_type']:checked") .val();
        var dis_status = $("input[name='dis_status']").val();
        if(!sendtype && dis_status==1){
            $.showErr('请选择配送方式',0,'hc');

            $('.btn_checkout').attr('disabled',false);
            $('.btn_checkout').text('立即支付');
            return false;
        }
        if(!sendtype || sendtype==2){
            if(dis_status==1){
                query.send_type=2;
            }else if(dis_status==2){
                query.send_type=3;
            }
        }else{
            query.send_type=sendtype;
        }
    }else if(type == 6){
        query.type=6;
        query.pt_id = $('#pt_id').val();
        query.pid = $('#pid').val();
        var sendtype = $("input[name='send_type']:checked") .val();
        var dis_status = $("input[name='dis_status']").val();
        if(!sendtype && dis_status==1){
            $.showErr('请选择配送方式',0,'hc');

            $('.btn_checkout').attr('disabled',false);
            $('.btn_checkout').text('立即支付');
            return false;
        }
        if(!sendtype || sendtype==2){
            if(dis_status==1){
                query.send_type=2;
            }else if(dis_status==2){
                query.send_type=3;
            }
        }else{
            query.send_type=sendtype;
        }
        if($("#all_use_score").is(':checked')){query.score=1;}
    }else if(type == 11){
        query.type=11;
        var service_type=$("input[name='service_type']").val();
		if(service_type == 3){
			service_type = 1;
		}
        // var best_time=$("input[name='best_time']").val();
        // query.best_time = best_time;
        query.service_type = service_type;
		query.spec_attr = $('#spec_attr').val();
        if($("#all_use_score").is(':checked')){query.score=1;}
    }else if(type == 12){
		query.type=12;
	}else if(type == 14){
        query.type=14;
        var sendtype = $("input[name='send_type']:checked") .val();
        var dis_status = $("input[name='dis_status']").val();
        if(!sendtype && dis_status==1){
            $.showErr('请选择配送方式',0,'hc');

            $('.btn_checkout').attr('disabled',false);
            $('.btn_checkout').text('立即支付');
            return false;
        }
        if(!sendtype || sendtype==2){
            if(dis_status==1){
                query.send_type=2;
            }else if(dis_status==2){
                query.send_type=3;
            }
        }else{
            query.send_type=sendtype;
        }
        if($("#all_use_score").is(':checked')){query.score=1;}
    }else{
        query.type=1;
        if($("#all_use_score").is(':checked')){query.score=1;}
    }
    query.source=1;//来源:手机
    var con_name=$("#con_name").val();
    var invoice_type=$("#invoice_type").val();
    query.invoice_type=invoice_type;//发票
    query.con_name=con_name;//发票
    var pay_pwd=$("#pay_pwd").val();
    if(pay_pwd)query.pay_pwd=$("#pay_pwd").val();
	
	// zjn 预约时间
	// var times = $("li[name='times[]']");
	// for(var i=0; i<times.length; i++){
	// 	var aa = $(times[i]).attr('class');
	// 	if(aa == 'change_time'){
	// 		query.best_time_day =  $(times[i]).attr('ke');
	// 		break;
	// 	}
	// }
	// var shows = $("li[name='show[]']");
	// for(var i=0; i<shows.length; i++){
	// 	var aa = $(shows[i]).attr('class');
	// 	if(aa == 'change_time'){
	// 		query.best_time_hour =  $(shows[i]).attr('ke');
	// 		break;
	// 	}
	// }
    // ------zjn  end-----
	// alert(JSON.stringify(query));return;
     $.ajax({
        url: APP + "Mobile/Order/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.app_paydata=='app_notpay'){
                    showtoast(obj.msg);
                    var body = $('body').css("position");
                    if(body=='relative'){$('body').css("position","");}
                    $("#sub_order").next().next().fadeIn();
                    $("#sub_order").next().animate({"bottom": 0});
                    if(query.use_account==1){
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:block');
                        $('#payname').html('选择支付方式并输入本站支付密码');
                    }else{
                        $('#order_pay').attr('style','display:block');
                        $('#paypassword').attr('style','display:none');
                        $('#payname').html('请选择支付方式');
                    }
                }else{
                    showtoast(obj.msg);
                    var body = $('body').css("position");
                    if(body=='relative'){
                        $('body').css("position","");
                    }
                }
                if(obj.url){
                    window.location.href=obj.url;
                }
            }
        }
   });
}
 function get_name(obj){
     var name=$(obj).val();
     if(name==3){
        $("#con_name").show();
     }else{
         $("#con_name").hide();
     }
 }
 //切换服务模式
 function change_type(a){
		if(a == 2){
			$("#type_one").attr('style',"display:none;");
			$("#type_two").attr('style',"display:block;");
			$("#service_type").val(2);
		}else{
			$("#type_one").attr('style',"display:block;");
			$("#type_two").attr('style',"display:none;");
			$("#service_type").val(1);
		}
	}
//切换配送方式
function change_send(){
    var send_type = $("input[name='send_type']:checked") .val();
    if(send_type == 2){
        $("#mendian").attr('style',"display:none;");
        $("#now_show").html('立即配送');
        var show_time = $("#show_time").html();
		if(show_time == '立即预约'){
			$("#show_time").html('立即配送');
		}
    }else{
        $("#mendian").attr('style',"display:block;");
        $("#now_show").html('立即预约');
        var show_time = $("#show_time").html();
		if(show_time == '立即配送'){
			$("#show_time").html('立即预约');
		}
    }
    count_total();
}
 //提交订货订单
function submit_porder(){
    if($('.btn_checkout').attr('disabled')=='disabled')return false;
    $('.btn_checkout').attr('disabled',true);
    $('.btn_checkout').text('支付中...');
    var ids=$("input[name='ids']").val();
    var shop_id=new Array();
    var temp =new Array();
    var supplierid=$("input[name='supplier_id[]']");
    for(var i=0;i<supplierid.length;i++){
        shop_id.push($(supplierid[i]).val());
        temp.push($('#temp'+$(supplierid[i]).val()).val());
    }
    var query=new Object();
    if(ids){
        query.ids=ids;
    }else if(order_info){
        query.order_info=order_info;
    }
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.type=10;
    query.source=1;//来源:手机
    query.shop_id=shop_id;
    query.temp=temp;
    var pay_pwd=$("#pay_pwd").val();
    if(pay_pwd)query.pay_pwd=$("#pay_pwd").val();
     $.ajax({
        url: APP + "Mobile/PurOrder/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/PurOrder/order_success/id/"+obj.pay;
                }else{
                    window.location.href=APP+"Mobile/PurOrder/order_success/order_ids/"+obj.data;
                }
            }else if(obj.status==3){
                $("#sub_order").next().next().fadeIn();
                $("#sub_order").next().animate({"bottom": 0});
                $.showErr(obj.msg,2,'hc');
                if(obj.url){
                    window.location.href=obj.url;
                }
            }else{
                $.showErr(obj.msg,0,'hc');
                if(obj.url){
                    window.location.href=obj.url;
                }
            }
        }
   });
}