<?php
	require_once("config/connect.php");
	require_once("config/pageConfig.php");
	require_once("global_functions.php");
	check_login('', '', 'ENABLERKEY_BENUTZER_VERSCHIEBEN');
	
	if(!isset($_SESSION)) session_start();
		$currentUserId=$_SESSION['userId'];
		$currentUserName=$_SESSION['userName'];
		$tableUserResult="";
		
			
		$userTablesQl=mysql_query("SELECT offener_bon.ID AS openBonId, bon_id, tische.tischName, SUM(((PRICESELL*stueck)*(1+tax_rate))) AS sumBrutto
									FROM offener_bon 
									LEFT JOIN tische ON offener_bon.bon_id = tische.id
									WHERE benutzer_id = ".$currentUserId." GROUP BY bon_id ORDER BY tische.tischName");
		if(mysql_num_rows($userTablesQl) > 0)$tableUserResult.="<p  class='moveTableRowHeader' style='background:#ccc; margin:.5% 0 5px 0;'>
								<span style='display:inline-block;' class='userTableToMove'>&nbsp;</span>
								<span class='tischNameSpan'>Tisch Name</span><span class='moveTableListBetrag'>Betrag</span></p>";
		while($userTables=mysql_fetch_array($userTablesQl)){
			$tableUserResult.="<p class='moveTableRow'>
								<label class='cbLarge2'>
								<input type='checkbox' class='userTableToMove cbLarge2' id=\"tisch".$userTables['bon_id']."\" 	/>
								<span>&nbsp;</span>
								</label>
								<span class='tischNameSpan'>".$userTables['tischName']."</span>
									<span class='moveTableListBetrag'>".number_format($userTables['sumBrutto'], 2, ",", ".")."</span></p>";
		
		}
		
		$listU=mysql_query("SELECT id, name FROM benutzer");
		$userOptionList="<select id='moveUserTableSelect' class='adminReportSelect orange'><option id='0' class='' >Benutzer auswählen</option>";
		while($listUser=mysql_fetch_array($listU)){
			$userOptionList.="<option id=\"userTarget".$listUser['id']."\" class='' >".$listUser['name']."</option>";
		}
		
		$userOptionList.="</select>";
	
	
?>
<head>
<meta charset="utf-8">
	<title>SITPOS Benutzer übergeben</title>
	<?php echo PAGE_CACHE_OFF; ?>
	<script src="jquery/jquery-2.1.1.min.js"></script>
	<script src="jquery/jquery-ui1.11.4.js"></script>
	<script src="include/global_js_functions.js"></script>
	
	<style type="text/css">
	<?php include 'include/main.css'; ?>
	<?php include 'include/menue.css'; ?>
		.moveTablesFunctionHeader{ position:relative; width:100%; height:auto; max-height:10%; margin:0; padding:.8% 0;			}
		#moveUserTableSelect{ margin-top:0; margin-right:3%;width:27%; padding:0.05em; font-weight:normal; font-size:1em; }
		.moveTableRow{ width:75%; line-height:1.5em; margin:0; padding:0;				}
		.moveTableRowHeader{ width:75%; line-height:1.5em; margin:0; padding:0; background:#bbc;				}
		#moveTableContainer{ position:relative; width:100%; height:85%; overflow:auto;		}
		.moveTablesBut{ position:relative; width:25%; margin:0 1%;			}
		#moveUserTableMsgBox{ position:relative; width:90%; height:22px; margin:0 auto;			}
		.tischNameSpan{	width:59%; margin-left:2%; display:inline-block;			}
		.userTableToMove{ width:10%; margin:0; padding:0;	}
		.moveTableListBetrag{ display:inline-block; width:27%; text-align:right;		}
	</style>
	
	<script>
		function moveUserTable(){
			var moveList = new Array();
			$(".userTableToMove").each(function(){
				if($(this).prop('checked') === true){
					selectedId=$(this).prop('id');
					if(selectedId != '')moveList.push(selectedId.replace("tisch", ""));
				}
			});
				// wenn alle ausgewählt werden steht als erstes element ein leeres 
				// wird in ajax_functions_gastro dann ausgefiltert
			var moveTargetId = false;
			moveTargetStrId=$("#moveUserTableSelect option:selected" ).prop('id');
			moveTargetName=$("#moveUserTableSelect option:selected" ).val();
			
			moveTargetId=moveTargetStrId.replace("userTarget", "")*1;
			if(moveTargetId < 1 || moveList.length < 1){
				$("#moveUserTableMsgBox").html("Fehlender Ziel Benutzer oder Tische. Funktion abgebrochen.");
				return false;
			}
			else $("#moveUserTableMsgBox").html(moveList.length + " Tische Für Benutzer "+ moveTargetName +" ausgewählt");
			
			 var moveListAsString = JSON.stringify(moveList);
			 
			$.ajax({
					type: "POST",
					url: "include/ajax_functions_gastro.php",
					dataType : "html",
					data: {functionKey: "moveUserTable", moveList:moveListAsString, moveTargetId:moveTargetId, moveTargetName:moveTargetName },
					async: true,
					success:function(ajaxResponse){
						if(checkJson(ajaxResponse) == true){ 
							respArray=JSON.parse(ajaxResponse);
							$("#moveTableContainer").html(respArray[0]);
							$("#moveUserTableMsgBox").html(respArray[2]);
						}
						else console.log("json nicht erkannt ! "+ajaxResponse);
					}
			});
		
		
		}
		
		function selectTables(){
			var tSel=false;
			$(".userTableToMove").each(function(){
				if($(this).prop('checked') == true){
					tSel=true;
				}
			});
			if(tSel==true){
				$(".userTableToMove").prop('checked', false);
				$("#moveTablesSelBut").text("Tische auswählen");
			}
			else{
				$(".userTableToMove").prop('checked', true);
				$("#moveTablesSelBut").text("Tische abwählen");
			}
			
		
		}
	
	
	</script>
	
</head>

<body>

<?php

	echo "<div id='wrapper_admin' class='wrapper_admin'>";

	menue_anzeigen();
	
	echo  "<div id='adminContent' class='adminContent'>";
	
	echo "<p class='moveTablesFunctionHeader'><button id='moveTablesSelBut' class='button_none_float orange moveTablesBut floatL' onclick='selectTables()'>Tische auswählen</button>
			".$userOptionList."
			<button id='moveTablesMoveBut' class='button_none_float orange moveTablesBut floatR'  onclick='moveUserTable()'>Tische übergeben</button>
			</p><div class='cclear'></div>";
	
	echo "<p id='moveUserTableMsgBox'></p>";
	
	echo "<div id='moveTableContainer'>".$tableUserResult."</div>";
	
	echo "";
	
		


	echo "</div></div>";
		
	
		
	
?>




</body>
</html>