<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/funcs.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QIA | Renewal Notice </title>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/allcss.css">
<style>
html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%; overflow:scroll;z-index: 100;}  
#insform {
text-align: left;
width:96%;
background: #AAFFD4;
margin: 5px; padding-left:5px; padding-right:5px;
font-family:"Tahoma", Arial, Helvetica, sans-serif;
font-size:14px;
text-wrap: none;
}
</style>

</head>
<body>



<p align="center" style="font-size:24px;font-weight:bold;padding:10px; background: #DEDEB4; box-shadow: 0px 0px 5px rgba(0,0,0,0.5);"> RENEWAL NOTICE &nbsp;&nbsp; Booking No. 
    <?php 
         $db=new MySQLDatabase();
	    $db->open_connection(); 
	    if(isset($_POST['btnaddins'])) { $insno = mysql_real_escape_string($db->connection, $_REQUEST['insno']); } 
	    else { $insno = $_GET['insno']; } 
	    echo $insno; 
	?> 
</p>

<?php 

if( isset($_POST['btnaddins'])) {
	$insno = $_POST['insno'];	$renewtxt = $_POST['renewtxt'];
	$inst1 = $_POST['inst1'];	$inst2 = $_POST['inst2'];	$inst3 = $_POST['inst3'];
	$rdate = substr($_REQUEST['rdate'],6,4)."-".substr($_REQUEST['rdate'],3,2)."-".substr($_REQUEST['rdate'] ,0,2);
	$ndate = substr($_REQUEST['ndate'],6,4)."-".substr($_REQUEST['ndate'],3,2)."-".substr($_REQUEST['ndate'] ,0,2);
	
	$endosav = 0;
	$user = $_SESSION['reusername'];	$ts = date("Y-m-d H:i:s");
	$sql = mysqli_query($db->connection, "SELECT * FROM renewals WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ $endosav = 1;  }
	if($endosav){ $qry = "UPDATE renewals SET ndate='$ndate', rdate='$rdate', renewtxt='$renewtxt', inst1='$inst1', inst2='$inst2', inst3='$inst3', us='$user', ts = '$ts' WHERE insno='$insno';";  }
	else { $qry = "INSERT INTO renewals (insno, ndate, rdate, renewtxt, inst1, inst2, inst3, us, ts) VALUES ('$insno','$ndate','$rdate', '$renewtxt','$inst1','$inst2','$inst3','$user', '$ts');"; }
	$sql = mysqli_query($db->connection, $qry) or die (mysqli_error($db->connection) );
	echo '<script type="text/javascript"> window.close();</script>';
}

elseif( isset($_GET['insno'])) {
	$user = $_SESSION['reusername'];
	$db=new MySQLDatabase();
	$db->open_connection();
	$sql = mysqli_query($db->connection, "SELECT * FROM insrecs WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ 
		if($row['inst1']!=='0') { $inst1 = '1st Installment Kshs '.number_format($row['inst1'],0); }
		else { $inst1 = ''; }
		if($row['inst2']!=='0') { $inst2 = '2nd Installment Kshs '.number_format($row['inst2'],0); }
		else { $inst2 = ''; }
		if($row['inst3']!=='0') { $inst3 = '3rd Installment Kshs '.number_format($row['inst3'],0); }
		else { $inst3 = ''; }
		$regno = $row['regno'];
		$rdate= substr($row['expdate'],8,2)."-".substr($row['expdate'],5,2)."-".substr($row['expdate'] ,0,4);
		}
	$sql = mysqli_query($db->connection, "SELECT * FROM renewals WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ if($row['ndate'] !== NULL) { $ndate = substr($row['ndate'],8,2)."-".substr($row['ndate'],5,2)."-".substr($row['ndate'] ,0,4); 	
																	 $rdate = substr($row['rdate'],8,2)."-".substr($row['rdate'],5,2)."-".substr($row['rdate'] ,0,4); 
																	 $renewtxt = $row['renewtxt'];
																	 $inst1 = $row['inst1'];	$inst2 = $row['inst2'];		$inst3 = $row['inst3'];
																	 }					 
										}
	else {	$ndate = ''; 		
			$renewtxt = '<b><u>REF: Reg. No.'.$regno.'</u></b> <br> To our esteemed client, we hereby kindly remind you that your insurance cover is about to <br> 
						expire and would request you to make the financial arrangements concerning <br> 
						premium payment well in advance so that your cover can be extended or renewed on the due
						 date. We really thank you for doing business with us.'; 
		   }
																	 
    echo '<form id="insform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="insrenewsav.php" >';
	echo '<table id="insf" style="width:640px;">';
	echo '<tr style="font-weight:bold;"><td>Notice Date</td> <td><input type="text" id="ndate" name="ndate" value="'.$ndate.'"/></td></tr>';
	echo '<tr style="font-weight:bold;"><td>Renewal Date</td> <td><input type="text" id="rdate" name="rdate" value="'.$rdate.'"/> <input type="hidden" id="insno" name="insno" value="'.$insno.'"/></td></tr>';
	echo '<tr style="font-weight:bold;"><td colspan="2">Subject & Text</td>';
	echo '<tr><td colspan="2"><textarea id="renewtxt" col="80" row="4" name="renewtxt">'.$renewtxt.'</textarea> </td></tr>';
	echo '<tr style="font-weight:bold;"><td>Installment 1 Text</td> <td><input type="text" id="inst1" name="inst1" value="'.$inst1.'" size="40"/></td></tr>';
	echo '<tr style="font-weight:bold;"><td>Installment 2 Text</td> <td><input type="text" id="inst2" name="inst2" value="'.$inst2.'" size="40"/></td></tr>';
	echo '<tr style="font-weight:bold;"><td>Installment 3 Text</td> <td><input type="text" id="inst3" name="inst3" value="'.$inst3.'" size="40"/></td></tr>';
	echo '</table>';

	echo '<p align="center"><input type="submit" class="text ui-widget-content ui-corner-all" style="width:180px;font-weight:bold;"  id="btnaddins" name="btnaddins" value="Save Renewal"/></p>';
}
?> 

</form>

</body></html>

<script type="text/javascript">       
$('#rdate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
$('#ndate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
CKEDITOR.replace('renewtxt');
</script>
