var orders0=new Object();
orders0['orders_create']=new Object();
orders0['orders_complite']=new Object();
var order_tmp=$('#order_tmp').html();
var orderline_tmp=$('#orderline_tmp tbody').html();
var orderline_modifiers_tmp=$('#orderline_modifiers_tmp tbody').html();

$('body').off('click', '.order');
$('body').on('click', '.order', function(){
	$('.new_line', $(this)).removeClass('new_line');
});

function beep() {
	document.getElementById('bell').play();
}


function reload_display(start=false) {
	if(json===0) {
		$('#display').load(location.href+' #display .reload');
		return;
	}
	$.ajax({
        url: orders_display_url,
        dataType: "html",
	timeout: 2800,
        success: function(response) {
        	var res=JSON.parse(response);
        	var new_order_create=false;
        	var new_order_create_line=false;
        	var new_order_create_modifier=false;
        	var new_order_complite=false;
        	var new_order_complite_line=false;
        	var new_order_complite_modifier=false;
        	if(('orders_create' in orders0)) {
	        	$.each(res['orders_create'], function(order_number, turn){
	        		$.each(turn, function(turn_number, order){
	        			new_order_create=false;
	        			ip='';
	        			if(!(order_number in orders0['orders_create']) || ((order_number in orders0['orders_create']) && !(turn_number in orders0['orders_create'][order_number]))) {
	        				new_order_create=true;
	        				var html=order_tmp;
	        				html=html.replace(/@action_type/g, 'order_complite');
	        				html=html.replace(/@div_id/g, 'create_'+order_number+'_'+turn_number);
	        				html=html.replace(/@order_number/g, order_number);
	        				html=html.replace(/@turn_number/g, turn_number);
	        				html=html.replace(/@order_id/g, res['orders_create'][order_number][turn_number]['id']);
	        				html=html.replace(/@room_title/g, res['orders_create'][order_number][turn_number]['room_title']);
	        				html=html.replace(/@table_title/g, res['orders_create'][order_number][turn_number]['table_title']);
	        				html=html.replace(/@create_order_time/g, res['orders_create'][order_number][turn_number]['create_order_time']);
	        				if(!res['orders_create'][order_number][turn_number]['order_description']) {
	        					res['orders_create'][order_number][turn_number]['order_description']='';
	        				}
	        				html=html.replace(/@order_description/g, res['orders_create'][order_number][turn_number]['order_description']);
	        				html=html.replace(/@button/g, 'Готово');
	        				if('ip' in res['orders_create'][order_number][turn_number]) {
	        					html=html.replace(/@cashbox_ip/g, res['orders_create'][order_number][turn_number]['ip']);
	        					ip=res['orders_create'][order_number][turn_number]['ip'];
	        				}
	        				else {
	        					html=html.replace(/@cashbox_ip/g, '');
	        				}
	        				$('#orders_create').prepend(html);
	        			}
	        			else {
	        				$('.order_description_'+order_number).html(res['orders_create'][order_number][turn_number]['order_description']);
	        			}

	        			$.each(order['lines'], function(line_id, line){
	        				new_order_create_line=false;
	        				if(new_order_create===true || !(line_id in orders0['orders_create'][order_number][turn_number]['lines'])){

	        					new_order_create_line=true;
	        					var html=orderline_tmp;
	        					html=html.replace(/@action_type_line/g, 'order_line_complite');
	        					html=html.replace(/@button/g, 'Готово');
	        					html=html.replace(/@cashbox_ip/g, ip);
	        					html=html.replace(/@line_id/g, line_id);
	        					html=html.replace(/@parent_id/g, line['parent_id']);
	        					html=html.replace(/@line_title/g, line['product_title']);
	        					html=html.replace(/@line_count/g, line['count']);
	        					$('#create_'+order_number+'_'+turn_number+' table').append(html);
	        				}
	        				if(('modifiers' in line)) {
	        					$.each(line['modifiers'], function(modifier_id, modifier){
	        						new_order_create_modifier=false;
	        						if(new_order_create===true
	        							|| !(line_id in orders0['orders_create'][order_number][turn_number]['lines'])
	        							|| !('modifiers' in orders0['orders_create'][order_number][turn_number]['lines'][line_id])
	        							|| (('modifiers' in orders0['orders_create'][order_number][turn_number]['lines'][line_id])
	        								&& !(modifier_id in orders0['orders_create'][order_number][turn_number]['lines'][line_id]['modifiers']))) {
			        					new_order_create_modifier=true;
			        					var html=orderline_modifiers_tmp;
			        					html=html.replace(/@line_id/g, modifier_id);
			        					html=html.replace(/@parent_id/g, modifier['parent_id']);
			        					html=html.replace(/@line_title/g, modifier['product_title']);
			        					html=html.replace(/@line_count/g, modifier['count']);
			        					$('#create_'+order_number+'_'+turn_number+' table .modifiers_add_'+line_id).before(html);
			        				}
	        					});
	        				}
	        			});
	        		});
	        	});

	        	$.each(orders0['orders_create'], function(order_number, turn){
	        		$.each(turn, function(turn_number, order){
	        			if(!(order_number in res['orders_create']) || ((order_number in res['orders_create']) && !(turn_number in res['orders_create'][order_number]))) {
	        				$('#create_'+order_number+'_'+turn_number).remove();
	        			}
	        			else {
	        				$.each(order['lines'], function(line_id, line){
	        					if(!(line_id in res['orders_create'][order_number][turn_number]['lines'])) {
	        						$('#create_'+order_number+'_'+turn_number+' .line_'+line_id).removeClass('new_line');
	        						$('#create_'+order_number+'_'+turn_number+' .line_'+line_id).addClass('remove_line');
	        						$('.order_line_ok_tr_'+line_id).remove();
	        						$('.modifiers_'+line_id).addClass('remove_line');
	        						if((line_id in res['orders_complite'][order_number][turn_number]['lines'])) {
	        							$('#create_'+order_number+'_'+turn_number+' .line_'+line_id).remove();
	        							$('.modifiers_'+line_id).remove();
	        							$('.modifiers_add_'+line_id).remove();
	        						}
	        					}
	        					else {
		        					if(('modifiers' in line)) {
			        					$.each(line['modifiers'], function(modifier_id, modifier){
			        						if(!('modifiers' in res['orders_create'][order_number][turn_number]['lines'][line_id])
			        							|| (('modifiers' in res['orders_create'][order_number][turn_number]['lines'][line_id]) && !(modifier_id in res['orders_create'][order_number][turn_number]['lines'][line_id]['modifiers']))) {
			        							$('#create_'+order_number+'_'+turn_number+' .line_'+modifier_id).removeClass('new_line');
			        							$('#create_'+order_number+'_'+turn_number+' .line_'+modifier_id).addClass('remove_line');
			        						}
			        					});
			        				}
			        				else {
			        					$('.modifiers_'+line_id).addClass('new_line');
			        				}
			        			}
	        				});
	        			}
	        		});
	        	});
	        }

	        if(('orders_complite' in orders0)) {
	        	$.each(res['orders_complite'], function(order_number, turn){
	        		$.each(turn, function(turn_number, order){
	        			new_order_complite=false;
	        			var ip='';
	        			if(!(order_number in orders0['orders_complite']) || ((order_number in orders0['orders_complite']) && !(turn_number in orders0['orders_complite'][order_number]))) {
	        				new_order_complite=true;
	        				var html=order_tmp;
	        				html=html.replace(/@action_type/g, 'issued_order');
	        				html=html.replace(/@div_id/g, 'complite_'+order_number+'_'+turn_number);
	        				html=html.replace(/@order_number/g, order_number);
	        				html=html.replace(/@turn_number/g, turn_number);
	        				html=html.replace(/@order_id/g, res['orders_complite'][order_number][turn_number]['id']);

	        				html=html.replace(/@room_title/g, res['orders_complite'][order_number][turn_number]['room_title']);
	        				html=html.replace(/@table_title/g, res['orders_complite'][order_number][turn_number]['table_title']);
	        				
	        				if(!res['orders_complite'][order_number][turn_number]['order_description']) {
	        					res['orders_complite'][order_number][turn_number]['order_description']='';
	        				}
	        				html=html.replace(/@order_description/g, res['orders_complite'][order_number][turn_number]['order_description']);
	        				html=html.replace(/@create_order_time/g, '');
	        				html=html.replace(/@button/g, 'Выдано');
	        				if('ip' in res['orders_complite'][order_number][turn_number]) {
	        					html=html.replace(/@cashbox_ip/g, res['orders_complite'][order_number][turn_number]['ip']);
	        					ip=res['orders_complite'][order_number][turn_number]['ip'];
	        				}
	        				else {
	        					html=html.replace(/@cashbox_ip/g, '');
	        				}
	        				$('#orders_complite').prepend(html);
	        			}
	        			else {
	        				$('.order_description_'+order_number).html(res['orders_complite'][order_number][turn_number]['order_description']);
	        			}


	        			$.each(order['lines'], function(line_id, line){
	        				new_order_complite_line=false;
	        				if(new_order_complite===true || !(line_id in orders0['orders_complite'][order_number][turn_number]['lines'])) {
	        					new_order_complite_line=true;
	        					var html=orderline_tmp;
	        					html=html.replace(/@action_type_line/g, 'issued_order_line');
	        					html=html.replace(/@button/g, 'Выдано');
	        					html=html.replace(/@cashbox_ip/g, ip);
	        					html=html.replace(/@line_id/g, line_id);
	        					html=html.replace(/@parent_id/g, line['parent_id']);
	        					html=html.replace(/@line_title/g, line['product_title']);
	        					html=html.replace(/@line_count/g, line['count']);
	        					$('#complite_'+order_number+'_'+turn_number+' table').append(html);
	        				}
	        				if(('modifiers' in line)) {
	        					$.each(line['modifiers'], function(modifier_id, modifier){
	        						new_order_complite_modifier=false;
	        						if(new_order_complite===true
	        							|| !(line_id in orders0['orders_complite'][order_number][turn_number]['lines'])
	        							|| !('modifiers' in orders0['orders_complite'][order_number][turn_number]['lines'][line_id])
	        							|| (('modifiers' in orders0['orders_complite'][order_number][turn_number]['lines'][line_id])
	        								&& !(modifier_id in orders0['orders_complite'][order_number][turn_number]['lines'][line_id]['modifiers']))) {
			        					new_order_complite_modifier=true;
			        					var html=orderline_modifiers_tmp;
			        					html=html.replace(/@line_id/g, modifier_id);
			        					html=html.replace(/@parent_id/g, modifier['parent_id']);
			        					html=html.replace(/@line_title/g, modifier['product_title']);
			        					html=html.replace(/@line_count/g, modifier['count']);
			        					if(html!=null) {
			        						$('#complite_'+order_number+'_'+turn_number+' table .modifiers_add_'+line_id).before(html);
			        					}
			        				}
	        					});
	        				}
	        			});
	        		});
	        	});

	        	$.each(orders0['orders_complite'], function(order_number, turn){
	        		$.each(turn, function(turn_number, order){
	        			if(!(order_number in res['orders_complite']) || ((order_number in res['orders_complite']) && !(turn_number in res['orders_complite'][order_number]))) {
	        				$('#complite_'+order_number+'_'+turn_number).remove();
	        			}
	        			else {
	        				$.each(order['lines'], function(line_id, line){
	        					if(!(line_id in res['orders_complite'][order_number][turn_number]['lines'])) {
	        						$('#complite_'+order_number+'_'+turn_number+' .line_'+line_id).removeClass('new_line');
	        						$('#complite_'+order_number+'_'+turn_number+' .line_'+line_id).addClass('remove_line');
	        						$('.order_line_ok_tr_'+line_id).remove();
	        						$('.modifiers_'+line_id).addClass('remove_line');
	        					}
	        					else {
		        					if(('modifiers' in line)) {
			        					$.each(line['modifiers'], function(modifier_id, modifier){
			        						if(!('modifiers' in res['orders_complite'][order_number][turn_number]['lines'][line_id])
			        							|| (('modifiers' in res['orders_complite'][order_number][turn_number]['lines'][line_id]) && !(modifier_id in res['orders_complite'][order_number][turn_number]['lines'][line_id]['modifiers']))) {
			        							$('#complite_'+order_number+'_'+turn_number+' .line_'+modifier_id).removeClass('new_line');
			        							$('#complite_'+order_number+'_'+turn_number+' .line_'+modifier_id).addClass('remove_line');
			        						}
			        					});
			        				}
			        				else {
			        					$('.modifiers_'+line_id).addClass('new_line');
			        				}
			        			}
	        				});
	        			}
	        		});
	        	});
	        }

        	orders0=res;
        	if(new_order_create===true
        		|| new_order_create_line===true
        		|| new_order_create_modifier===true
        		|| new_order_complite===true
        		|| new_order_complite_line===true
        		|| new_order_complite_modifier===true) {
        		beep();
        	}
        	if(start===true) {
        		$('.new_line').removeClass('new_line');
        	}
    	}
 	});
}
reload_display(true);
setInterval('reload_display()', 3000);

$('#executor_display_settings input[type="checkbox"]').change(function(){
	if($(this).prop('checked')) {
		var value=1;
	}
	else {
		var value=0;
	}
	var name=$(this).attr('id');
	document.cookie = name+"="+value;
	location.replace('/receipts/executor_display');
});

$('#executor_display_settings input[type="number"]').change(function(){
	var name=$(this).attr('id');
	document.cookie = name+"="+$(this).val();
	location.replace('/receipts/executor_display');
});

function order_line_issued(line_id) {
	$('.line_'+line_id).remove();
	$('.modifiers_add_'+line_id).remove();
	$('.modifiers_'+line_id).remove();
	$('.order_line_ok_tr_'+line_id).remove();
}