<?php session_start();
		if (isset($_SESSION['logged']) && $_SESSION['logged'] == 'yes') {
			include ('../scripts/classes.php'); if ($_SESSION['lang']=="EN"){include ('../lang/en.php');}elseif ($_SESSION['lang']=="GR"){include ('../lang/gr.php');}else {include ('../lang/en.php');}
			$mode = $lbl_redeem_credits;
			$page_title =$lbl_cls_redeem_credits; //Assign Page Title
			$array = getGlobalSettings();
			foreach ($array as $settings){extract($settings);}
	if ($_SERVER['REQUEST_METHOD'] != 'GET') exit;
	$barcode = mysqli_real_escape_string($mysqli, $_GET['barcode']);				
	//Customer Match Query
	$match = "SELECT a.id as cust, a.barcode_id, a.last_name, a.first_name, a.birth_date, a.is_active, b.name as name, a.credits, a.score, a.turnover, a.last_transaction_date, b.redeem_limit, b.multiplier FROM cls_customers a
				INNER JOIN cls_customer_groups b ON a.group_id = b.id
			WHERE barcode_id='$barcode'";
	$result = mysqli_query($mysqli, $match) or die("Query failed : " . mysqli_error($mysqli));
	if (mysqli_num_rows($result) == 0){
	echo $lbl_system_error;
	$str = implode("\n", $errors);
	die("Search Error:\n\n" . $str);
	//exit();
		}
		else {
		while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
		$barcode_id = $row['barcode_id']; 
		$customer_id = $row['cust']; 
		$last_name = $row['last_name'];  
		$first_name = $row['first_name']; 
		$group_id = $row['name'];
		$birth_date = $row['birth_date'];
		$active = $row['is_active'];
		$credits = $row['credits'];
		$score = $row['score'];
		$turnover = $row['turnover'];
		$limit = $row['redeem_limit'];
		$multiplier = $row['multiplier'];
		$last_transaction_date = $row['last_transaction_date'];
		}
}
		//redeem eligible
		if ($credits >= $limit) {
			$redeem_eligible= $lbl_yes;
			$reduction= floor($credits * $multiplier);
			$input_color = '#00CC00';
			$visibility = 'visible';
		}else{
			$redeem_eligible= $lbl_no;
			$input_color = '#FF0000';
			$reduction=0;
			$visibility = 'hidden';
		}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $page_title ?></title>
<link rel="shortcut icon" href="../img/favicon.png" />
<link href="../cms1.css" rel="stylesheet" type="text/css"  />
<script type="text/javascript" src="../scripts/autoSum.js"></script>
<script type="text/javascript" src="../scripts/menu.js" ></script>
<script>
function checkSubmit(e)
{
   if(e && e.keyCode == 13)
   {
     if (checkVal()) {document.forms['redeemFrm'].submit()};
   } else if(e && e.keyCode == 112)
   {
   	window.location = "idle.php";
}
}
</script>
<script>
function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
	  var regex = /[0-9 \s \b]|\ /;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}
</script>
<script>
function checkVal() {
var redeem = document.getElementById('inputRedeem').value;
if (redeem > 0 && redeem <= <?php echo $reduction ?>) { 
	return confirm("<?php echo $lbl_discount_grant ?>" + Math.abs(redeem) + "<?php echo $currency ?>;");
	}else if (redeem > 0 && redeem > <?php echo $reduction ?>){
	alert ("<?php echo $lbl_credits_not_enough_for_amount ?>");
	document.getElementById('inputRedeem').focus();
	document.getElementById('inputRedeem').select();
	}else if (redeem == 0){
	alert ("<?php echo $lbl_please_enter_amount ?>");
	document.getElementById('inputRedeem').focus();
	document.getElementById('inputRedeem').select();
	}
}
</script>
<script>
function startGo(){
  interval = setInterval("Go()",1);
}
function Go(){
  var credits_redeem = (document.getElementById('inputRedeem').value) / (<?php echo $multiplier ?>);
  document.redeemFrm.sumbox.value = Math.abs(credits_redeem);
}
function stopGo(){
  clearInterval(interval);
}
</script>
</head>
<body onload="document.getElementById('inputRedeem').focus(); document.getElementById('inputRedeem').select()">
<script>
top.document.title=self.document.title;
</script>
		<div onkeypress="return checkSubmit(event)">
		<fieldset style="min-height:200px">
		<legend><?php echo  $mode ?></legend>
		<br />
		<div  class="success">
		<ul>
			<li>
				<label class="bar_label_ready"><?php echo $lbl_found_match ?></label>
				<label style="font-size:20px;color:#999999;"> <?php echo $barcode_id; ?></label>
				<label style="font-size:20px;color:#43759A"> <?php echo "(".$last_name; ?> </label>
				<label style="font-size:20px;color:#43759A;"> <?php echo $first_name; ?> </label>
				<label style="font-size:20px;color:#43759A"> <?php echo date("d.m.Y",strtotime( $birth_date));?> </label>
				<label style="font-size:20px;color:#43759A;"> <?php echo $group_id.")"; ?> </label>
			</li>
		</ul>
		</div>
                <?php 
			if ($active==0){
				echo "<div class='critical'><ul><li><label style='font-size:20px;color:#FFF;'>$lbl_customer_is_inactive ($lbl_last_transaction_date:".date("d.m.Y",strtotime($last_transaction_date)).")</label></li></ul></div>";
				echo "<div style=\"float:right; padding:5px\" align=\"right\"><input type=\"button\" id=\"go_idle\" value=\"$lbl_idle\" class=\"big_idle\" onclick=\"window.location.href='idle.php'\"/></div>";
			exit();
			}
		?>
		<div style="float:right; padding:5px" class="align-right">
			<input type="button" id="go_idle" value="Αναμονή" class="big_idle" onclick="window.location.href='idle.php'" />
			<br />
            <input type="button" id="go_reset" value="Reset" class="big_reset" style="visibility:<?php echo $visibility ?>" onclick="document.getElementById('inputRedeem').value='<?php echo $reduction ?>'; document.getElementById('inputRedeem').focus();" /><br />
	      <input type="button" id="go_submit" value="OK" class="big_ok"  style="visibility:<?php echo $visibility ?>" onclick="if (checkVal()) {document.forms['redeemFrm'].submit()};"/>
		</div>
		<div style="width:80%; padding:15px;">
        <form name="redeemFrm" id="redeemFrm" action="create_transaction_redeem.php" method="post">
         <table>
        	<tr>
            	<td><label class="cls_big_labels"><?php echo $lbl_credits_balance; ?></label></td>
        		<td><input type="text" style="color:#FF0000; font-size:40px;" class="cls_big_input" readonly="readonly" value="<?php echo $credits ?>" /></td>
            </tr><tr>
            	<td><label class="cls_small_labels"><?php echo $lbl_score; ?></label></td>
                <td><input type="text" class="cls_small_input" readonly="readonly" value="<?php echo $score?>" /></td>
			</tr><tr>
        		<td><label class="cls_small_labels"><?php echo $lbl_turnover; ?></label></td>
                <td><input type="text" class="cls_small_input" readonly="readonly" value="<?php echo $currency."".$turnover ?>" /></td>
			</tr><tr>
        		<td><label class="cls_small_labels"><?php echo $lbl_low_limit; ?></label></td>
                <td><input type="text" class="cls_small_input" readonly="readonly" value="<?php echo $limit ?>" /></td>
			</tr><tr>
        		<td><label class="cls_small_labels"><?php echo $lbl_multiplier; ?></label></td>
                <td><input type="text" name="multiplier" id="multiplier" class="cls_small_input" readonly="readonly" value="<?php echo $multiplier ?>" /></td>
			</tr><tr>
            	<td><label class="cls_big_labels"><?php echo $lbl_redeem_eligible; ?></label></td>
                <td><input type="text" class="cls_big_input" readonly="readonly" style="background-color:<?php echo $input_color ?>; color:#FFF" value="<?php echo $redeem_eligible ?>"  /></td> 
            </tr>
            <?php if ($redeem_eligible==$lbl_yes){
			?><tr>
        		<td><label class="cls_big_labels"><?php echo $lbl_discount_amount; ?></label></td>
                <td><input type="text" id="inputRedeem" name="inputRedeem" tabindex="1" class="cls_big_input"  style="background-color:#FFF; color:#F00" value="<?php echo $reduction ?>" autocomplete="off" onkeypress="validate(event);" onFocus="startGo();" onBlur="stopGo();"  /></td>
                <td><input type="text" id="sumbox" name="sumbox" class="cls_big_input"/>
			</tr>
            <?php }
			?>
            <input type="hidden" id="barcode_id" name="barcode_id" value="<?php echo $barcode_id ?>" />
            <input type="hidden" id="customer_id" name="customer_id" value="<?php echo $customer_id ?>" />
        </table>
        </form>  
		</div>	
		</fieldset>			
        </div>
</body>
</html>
<?php   
    mysqli_close($mysqli); 
} else {
    header('Location: login.php');
}
?>


