order.js

$(function(){
    serviceHeight();
    $('#seconds').hide();
    inputFocus();
/*开始接受订单*/
$('.btn_order').click(function(){
    if( !$(this).hasClass('btn_stop') ){
        getOrder();
    }
    $(this).toggleClass('btn_stop');
    $('.layer_center').toggleClass('refuse');

    //return false;
    
})
    
    
    
    
$('input.btn_order').focus(function(){
    $(this).blur();
});    

/*隔行换色*/
    stripesTable();

/*退回订单*/
$('.back').click(function(){
    var orderID = $.trim($('.order_no strong').text());
    if(confirm('您确定要退回订单吗?')){
        ajaxSetup('back',orderID);
    }
})

/*取消订单*/
$('.esc').click(function(){
    var orderID = $.trim($('.order_no strong').text());
    if(confirm('您确定要取消订单吗?')){
        ajaxSetup('cancel',orderID);
    }
})



/*form 提交表单*/
$('#from_menu').submit(function(event){
    event.preventDefault();
    /*发送修改 删除记录*/
    var modifyNode = $('ul.uldishes li.modify');
    var modify_items= [];
    for(var i=0;i<modifyNode.length;i++){
        var cur_modify =  modifyNode.eq(i);
        var ID = cur_modify.find('.num').text();
        var num = cur_modify.find('.amount').find('.inp').val();
        var msg = cur_modify.find('.msg').text();
        var item = '{orderID: '+ID+', num: '+ num +', msg: '+msg+'}';
        modify_items.push(item); 
    }
    console.log(modify_items);
    console.log(del_items);
    var a={modify:[modify_items]};
    /*ajax发送修改 删除的菜品*/
    $.ajax({
        type:'GET',
        url:'ajax.txt',
        data:{ modify:[modify_items],remove:del_items,add:'111'},
        success:function(){
            console.log('modify:['+ modify_items+']');
        }
    })
    /*提交成功反馈*/
     $.ajax({
        type:'GET',
        url:'http://huangchen.redcms.com/order/edit.html',
        data:'type=submit&act=edit&oid=1',
        success:function(txt){
            alert('提交成功');
            if( txt == 1){
                $.fancybox.close();
                setTimeout(function(){
                    $.fancybox({
                        'href':'#order_tips01',
                        'showCloseButton':false
                    });
                },350);
                setTimeout(function(){
                    $.fancybox.close();
                    maskLayer();
                },1000);
                /*自动接受订单*/
                
                setTimeout(function(){
                    if( $('.btn_order').hasClass('btn_stop')){
                        getOrder();
                    }
                },1500);
            }else{
                alert('提交失败');
            }
        }
     })
    /*暂停接受订单 复选框选中*/
    if( $('.pause').is(':checked') ){
        $('.btn_order').removeClass('btn_stop');
        $('.btn_order').addClass('btn_start');
        $('.layer_center').removeClass('refuse');
    }
    
})

/*退回订单/取消订单 ajax*/
function ajaxSetup(operation,ID){
        $.ajax({
            type:'GET',
            url:'http://huangchen.redcms.com/order/edit.html',
            data:'type='+operation+'&act=edit&oid='+ID+'',
            success:function(){
                console.log('成功'+operation+'订单');
            },
            error:function(){
                console.log(operation+'订单失败');
            }
        });
        $.fancybox.close();
        clearTimeout(movement);
        /*遮罩层消失*/
        maskLayer();
}

/*Input 聚焦*/
function inputFocus(){
    $('input.inp_msg').val('此处为菜品备注信息');
    $('input.inp_msg').css('color','#c6c6c6');
    $('input.inp_msg').focus(function(){
        if( $(this).val() == '此处为菜品备注信息') {
            $(this).val('');
            $(this).css('color','#000');
        }
    }).blur(function(){
        if( $(this).val() == ''){
            $(this).val('此处为菜品备注信息');
            $(this).css('color','#c6c6c6');
        }
    })
    /*textarea*/
    $('.mtext').css('color','#c6c6c6');
    $('.mtext').val('此处为订单备注信息');
    $('.mtext').focus(function(){
        if( $(this).val() == '此处为订单备注信息'){
            $(this).val('');
            $(this).css('color','#000');    
        }
    }).blur(function(){
        if( $(this).val() == ''){
            $(this).val('此处为订单备注信息');
            $(this).css('color','#c6c6c6');
        }
    })
}

/*数量增加*/
$('.add').live('click',function(){
        var inp_text = $(this).siblings('.inp_short');
        var num = inp_text.val();
        if( num > 0 ){ num++}
        inp_text.val(num);
        /*总价*/
        totalPrice($(this));
        mealFee();
        service();
        total();
        $(this).parents('li').addClass('modify');
        return false;
})
/*数量减少*/
$('.minus').live('click',function(){
        var inp_text = $(this).siblings('.inp_short');
        var num = inp_text.val();
        if( num > 1){ num--};
        inp_text.val(num);
        /*总价*/
        totalPrice($(this));
        mealFee();
        service();
        total();
        $(this).parents('li').addClass('modify');
        return false;
})
$('.amount .inp_short').live('blur',function(){
        totalPrice($(this));
        mealFee();
        service();
        total();
})


/*总价*/
$('.price').each(function(){
    totalPrice($(this));
})



/*删除某菜单*/
var del_items=[];
$('.del').live('click',function(){
    if(confirm('确定要删除此菜品吗?')){
        var deleteId = $(this).parents('li').find('.num').text();
        del_items.push(deleteId);
        $(this).parents('li').remove();
        mealFee();
        service();
        total();
        stripesTable();
        $('.addmenu').find('i').text('');
    }
    return false;
})


/*补差金额输入框*/
$('.supply').blur(function(){
    addFee();
    service();
    total();
})


/*餐费*/
mealFee();


/*补差金额*/
addFee();


/*服务费*/
service();


/*合计*/
total();

var flag_done = 0;
/*搜索建议*/
$('.inp_match').val('');
$('.inp_match').keyup(function(event){
    var keycode = event.which;
    matchSearch($(this),keycode);
    $('.inp_match').removeClass('inp_adopt');
})
$('.inp_match').focus(function(){
    matchSearch($(this));
    $('.addmenu').find('i').text('');
    $('.inp_match').removeClass('inp_adopt');
    $('ul.uldishes li').removeClass('already');
})


$('.inp_match').blur(function(){
    $('#jsmenulist').fadeOut('fast');
})

$('#jsmenulist li').live('click',function(){
    $('#jsmenulist li').removeClass('adopt');
    $(this).addClass('adopt');
    $('.inp_match').val($(this).find('span').text() +'  '+$(this).find('em').text());
    $('.inp_match').addClass('inp_adopt');
})

/*键盘上下键*/


/*添加菜品*/
$('#js_addmenu').click(function(){
    if( $.trim( $('.inp_match').val())==''){ $('.addmenu').find('i').text('请填写菜品名称或者编号!'); return false;}
    console.log('inp_adopt内容:'+$('.inp_adopt').html());
    if( ! $('.inp_match').hasClass('inp_adopt') ){
        alert('不存在inp_adopt');
        console.log($('.inp_adopt').parent().html());
        curmenuSearch();
        $('.inp_match').removeClass('.inp_adopt');
        $('.inp_match').val('');
        var maxScroll = $('ul.uldishes li').height()*($('ul.uldishes li').length - 1);
        $('ul.uldishes').scrollTop(maxScroll);    
        inputFocus();
    }else{
        alert('存在inp_adopt');
        console.log('第二次存在inp_adopt:'+$('.inp_adopt').parent().html());
        //if( $('.inp_match').hasClass('inp_entered')){ $('.addmenu').find('i').text('该菜品已存在列表中!'); return false;} 
        var linow = $('#jsmenulist').find('li.adopt');
        var ID = linow.find('span').text();
        var name = linow.find('em').text();
        var price = linow.attr('data-key');
        /*查找是否有同编号的存在*/
        var dishes=dishesList();
        console.log('dishes:'+dishes);
        for(var i=0;i<dishes.length;i++){
            var num = dishes[i];
            if( num != ID.toLowerCase()){
                continue;
            }
            else{
                $('.addmenu').find('i').text('此菜品已存在!');
                $('ul.uldishes li').eq(i).addClass('already');
                var offsetY = $('ul.uldishes li.already').height() *  i;
                console.log(offsetY);
                $('ul.uldishes').scrollTop(offsetY);
                $('.inp_match').removeClass('.inp_adopt');
                $('.inp_match').val('');
                return false;
            }
        }
        $('ul.uldishes').append('<li><div><span class=\'num\'>'+ID+'</span><span class=\'dishname\'>'+name+'</span><span class=\'per\'>'+price+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'此处为菜品备注信息\' type=\'text\'></div></li>');
        //$('.inp_match').addClass('inp_entered');
        //flag_done = 0;
        mealFee();
        service();
        total();
        stripesTable();
        $('.inp_match').removeClass('.inp_adopt');
        $('.inp_match').val('');
        var maxScroll = $('ul.uldishes li').height()*($('ul.uldishes li').length - 1);
        $('ul.uldishes').scrollTop(maxScroll);        
        inputFocus();
     }

})

/*iframe高度自适应*/
$("#edit").load(function(){
    var editheight = $(this).contents().find("body").height();
    $(this).height(editheight);
}); 




})

/*列表中存在的菜品*/
function dishesList(){
    var liNode = $('ul.uldishes li');
    var dishes= new Array();
    for(var i=0;i<liNode.length;i++){
        var key = $.trim(liNode.eq(i).find('.num').text()).toLowerCase();
        dishes.push(key);
    }
    return dishes;
}




/*搜索框 ajax请求---搜索建议*/
function matchSearch(elem,keycode){
    var matchVal = $('.inp_match').val();
    if( $.trim(matchVal) == ''){ return false; };
    $.ajax({
        type:'GET',
        url:'http://huangchen.redcms.com/order/edit.html',
        data:'act=select&sname='+matchVal+'',
        success: function(result){
            eval('var data='+ result);
            if( data.length == 0 ) return false;
            $('#jsmenulist').fadeIn('fast');
            $('#jsmenulist').html('<ul></ul>');
            var menu_amount = data.length;
            console.log(data);
            for(var i=0;i<menu_amount;i++){
                $('#jsmenulist ul').append('<li data-key=\''+data[i].order_price+'\''+'><span>'+data[i].menu_code+'</span><em>'+data[i].menu_name+'</em></li>');
            };
        }
    });
    /*键盘上下键*/
    changekey(elem,keycode);
    
}

/*键盘上下键操作*/
function changekey(src,keycode){
    console.log('changekey');
    var len = $(src).parent().find('#jsmenulist ul').length;
    $('#jsmenulist ul').data('key',0);
    var i = $('#jsmenulist ul').data('key');
    /**/
    if(keycode == 38){
        
    }
    /**/
    if(keycode == 40){
        if(i < len){
            $('#jsmenulist ul').find('li').eq(0).css('background-color','#eee');
            //i++;
        }
        
    }
}





/*ajax请求 查找当前菜品*/
var timer;
function curmenuSearch(){
    var inpVal = $('.inp_match').val();
    var dishes=dishesList();
    console.log(dishes);
    $.ajax({
        type:'GET',
        url:'http://huangchen.redcms.com/order/edit.html',
        data:'act=select&sname='+inpVal+'',
        success:function(result){
            eval('var data='+ result);
            console.log('response数据:'+data.length);
            if( data.length == 0 ){ $('.addmenu').find('i').text('此菜品不存在!'); return false;}
            if( data.length > 1){ $('.addmenu').find('i').text('需填写菜品编号!'); return false;}
            if( data.length == 1){
                console.log($('.inp_adopt').html());
                for(var i=0;i<dishes.length;i++){
                    var num = dishes[i];
                    console.log(dishes[i]);
                    //console.log(data[i]);
                    if( num != data[0].menu_code.toLowerCase()){
                        continue;
                    }
                    else{
                        $('.addmenu').find('i').text('此菜品已存在!');
                        $('.inp_match').removeClass('.inp_adopt');
                        $('.inp_match').val('');
                        console.log(i);
                        $('ul.uldishes li').eq(i).addClass('already');
                        var offsetY = $('ul.uldishes li.already').height() *  i;
                        console.log(offsetY);
                        $('ul.uldishes').scrollTop(offsetY);
                        return false;
                    }
                }
                console.log('append');
                $('ul.uldishes').append('<li><div><span class=\'num\'>'+data[0].menu_code+'</span><span class=\'dishname\'>'+data[0].menu_name+'</span><span class=\'per\'>'+data[0].order_price+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+data[0].order_price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'此处为菜品备注信息\' type=\'text\'></div></li>');    
                stripesTable();        
            }

        }
    })

}




/*餐费*/
function mealFee(){
    var dish_amount = $('.uldishes li').length;
    var total_meal = 0;
    for(var i=0;i<dish_amount;i++){
        var per_meal = parseInt($('.uldishes li').eq(i).find('.price').text());
        total_meal += per_meal; 
    }
    $('.mealfee').find('strong').text(total_meal.toFixed(2));
}

/*补差金额*/
function addFee(){
    if( $('.supply').val() == '' ){ $('.supply').val(0);}
    var txt = $('.supply').val();
    var supply = parseInt(txt);
    $('.supplement').find('strong').text(supply.toFixed(2));
}

/*服务费*/
function service(){
    var mealfee = parseInt( $('.mealfee').find('strong').text());
    var supple = parseInt( $('.supplement').find('strong').text());
    var fee = (mealfee + supple)* 0.15;
    $('.service').find('strong').text(fee.toFixed(2));

}


function total(){
    var mealfee = parseInt( $('.mealfee').find('strong').text());
    var supple = parseInt( $('.supplement').find('strong').text());
    var service = parseInt( $('.service').find('strong').text());
    var fee = mealfee + supple + service;
    $('.total').find('strong').text(fee);
}

/*每个菜的总价*/
function totalPrice(elem){
    var liNode = elem.parents('li');
    var per = parseInt(liNode.find('.per').text());
    var num = parseInt( liNode.find('.amount .inp_short').val());
    var total = (parseInt(per) * parseInt(num)).toFixed(2);
    liNode.find('.price').text(total);
}




/*服务台 高度*/
function serviceHeight(){
    $('.maincon').css('height','auto !important');
    var screenY = $(window).height();
    $('.service').css('min-height',screenY-30);
}


/*判断有无订单号*/
function getOrder(){
    $.ajax({
        url:'http://wangyang.redcms.com/order/list.html?oid=1&act=view',
        success:function(msg){
            eval('var json='+msg);
            if( json.res == null) {return false;};
            /*订单号存在*/
                $.fancybox({
                    'href':'#order_details',
                    'overlayOpacity':'0.5',
                    'showCloseButton':false
                });
                var start = new Date();
                countDown(start);
                $('.uldishes li').remove();
                /*将数据塞入*/
                $('.order_no strong').text(json.res.order_no);
                $('.create_time strong').text(json.res.create_time);
                $('.roominfo li.tel').find('strong').text(json.res.target_telephone);
                $('.roominfo li.address').find('.inp').val(json.res.target_address);
                //$('.roominfo li.deliver').find()
                $('.roominfo li.guest').find('strong').text(json.res.target_guest_name);
                $('.roominfo .address2').find('strong').text(json.res.target_address);
                for(var i=0;i<json.resinfo.length;i++){
                    var menuID = json.resinfo[i].menu_id;
                    var menuName = json.resinfo[i].menu_name;
                    var count = json.resinfo[i].count;
                    var unitPrice = json.resinfo[i].unit_price;
                    var remark = json.resinfo[i].remark;
                    var price = unitPrice * count;
                    $('ul.uldishes').append('<li><div><span class=\'num\'>'+menuID+'</span><span class=\'dishname\'>'+menuName+'</span><span class=\'per\'>'+unitPrice+'</span><span class=\'amount\'><a class=\'minus\' href="#">-</a><input class=\'inp inp_short\' value=\'1\' type=\'text\'><a class=\'add\' href=\'#\'>+</a></span><span class=\'price\'>'+price+'</span><span><a class=\'del\' href=\'#\'>删除</a></span></div><div class=\'msg\'><input class=\'inp inp_long inp_msg\' value=\'1\' type=\'text\'></div></li>');            
                }
                stripesTable();
                /*合计费用*/
                $('.mealfee strong').text(json.res.money);
                $('.supplement strong').text(json.res.money);
                $('.service strong').text(json.res.money);
                $('.total strong').text(json.res.money);
                /*遮罩层*/
                $(self.parent.frames['header'].document.body).append("<div class='masklayer'></div>");
                $(self.parent.frames['subnav'].document.body).append("<div class='masklayer'></div>");
                  
            }
    })


}

/*隔行换色*/
function stripesTable(){
    $('ul.uldishes li').removeClass('c1 c2');
    $('ul.uldishes li:odd').addClass('c2');
    $('ul.uldishes li:even').addClass('c1');
    //$('ul.uldishes li:last').css('border','none');
}


/*遮罩层消失*/
function maskLayer(){
    $(self.parent.frames['header'].document.body).find('.masklayer').fadeOut('slow');
    $(self.parent.frames['subnav'].document.body).find('.masklayer').fadeOut('slow');
}

/*倒计时*/
var flag = 0;
function countDown(startTime){
    console.log(startTime);
    var endTime = startTime;
    endTime.setMinutes(endTime.getMinutes()+5);  //设置时间为5分钟后
    console.log(endTime);
    
    /*判断有无键盘鼠标操作*/
    $('#order_details').keydown(function(){
        /*重置计时*/
        if( flag == 0){ console.log('keydown');timerReset(); }
    });
    $('#order_details').mouseover(function(){
        /*重置计时*/
        if( flag == 0){ console.log('mouseover');timerReset(); }
    })
    $('#order_details').click(function(){
        /*重置计时*/
        if( flag == 0){ console.log('click'); timerReset(); }
    })

    /*在没有操作的情况下*/
    oddTime(endTime);
    /*每隔30秒发送重置计时请求*/
    resetRequest();
    
    
    
}

/*重置计时*/
function timerReset(){
    flag = 1;
    setTimeout(function(){
        flag = 0;
    },31000);
}

/*重置标志*/
function resetRequest(){
    setTimeout(function(){
        console.log('flag请求标志'+flag);
            $.ajax({
                type:'GET',
                url:'http://huangchen.redcms.com/order/edit.html',
                data:'act=check&oid='+flag+'',
                success:function(){
                    console.log('30秒发送一个请求标志');
                },
                error:function(){
                    console.log('不向服务器发送重置请求标志')
                }
            });        
        if( flag == 1){
            clearTimeout(movement);
            $('#minutes').text('4分');
            //$('#seconds').hide();
            var startTime = new Date();
            var endTime = startTime;
            endTime.setMinutes(endTime.getMinutes()+5);  //设置时间为5分钟后        
            oddTime(endTime);
        }
        resetRequest();
    },30000);
}



/*剩余时间*/
var movement;
function oddTime(endTime){
    if( movement){ clearTimeout(movement);}
    var current = new Date();
    if( new Date(endTime) <= current){ 
        clearTimeout(movement);
        $.fancybox.close(); 
        /*遮罩层消失*/
        maskLayer();
        setTimeout(function(){
            $.fancybox({
                'href':'#order_tips02',
                'onClosed' : function() { maskLayer();}
            });
            
        },1000);
        return false;
    }
    var oddTime = new Date(endTime) - current;
    var oddMinutes = parseInt(oddTime/1000/60);
    $('#minutes').text(oddMinutes + '分');
    //if( new Date(endTime) - current <= 60000){
        $('i#seconds').show();
        var oddSeconds = parseInt(oddTime/1000%60);
        $('#seconds').text(oddSeconds +'秒');
    //}
    movement = setTimeout("oddTime('"+endTime+"')" ,1000);

}


$(window).resize(function(){
    serviceHeight();
});

 

posted @ 2014-07-08 17:29  Western Journey  阅读(409)  评论(0编辑  收藏  举报