<?php 
include 'check.php';
include '../db.inc.php';
include 'check_msg.php';
if(isset($_POST['submit'])) {
	$accid=$_POST['accid'];
	$date=$_POST['date'];
	$type=$_POST['type'];
	$amount=$_POST['amount'];
	$notes=addslashes($_POST['notes']);
	$status=$_POST['status'];
	$tid=rand(100000,999999);
	$sql="INSERT INTO `history` (`tid` ,`bname`,`bcountry`,`accnum`,`accname`,`cot`,`stat`,`routnum`,`sort`,`ownadd`,`number` ,`date` ,`amount` ,`type` ,`details` ,`status`)VALUES ( '$tid', '','','','','','','','','','$accid', '$date' , '$amount', '$type', '$notes', '$status')";
	if(!mysqli_query($connectionString,$sql)) {
    echo("Error description: " . mysqli_error($connectionString));exit;
  }
	
	if($status == "Successful") {
		$user=mysqli_fetch_array(mysqli_query($connectionString,"SELECT * FROM users WHERE number='$accid' LIMIT 1"),MYSQLI_ASSOC);
		if($type=="Account Credit" || $type=="Direct Payin") {
			$bal=$user['balance'] + $amount;
			$bal2=$user['balance2'] + $amount;
		}
		else {
			$bal=$user['balance'] - $amount;
			$bal2=$user['balance2'] - $amount;
		}
	
		mysqli_query($connectionString,"UPDATE users SET balance='$bal', balance2='$bal2' WHERE number='$accid' LIMIT 1");



$user=mysqli_fetch_array(mysqli_query($connectionString,"SELECT * FROM users WHERE $accid=number LIMIT 1"),MYSQLI_ASSOC);

             $recipient=$user['email'];
		$mob_tel=$user['phone'];
		$first_name=$user['firstname'];
		$last_name=$user['lastname'];
                $senderName = "AnB Credit Union";

$fromEmailAddress = "no-reply-accountdept@anbcreditunion.com";

$emailSubject = "Transaction Alert";

$comments = "\n Hello $first_name $last_name \n \n This is to inform you that a transaction has occurred in your account. \n\n TRANSACTION DETAILS \n\n TRANSACTION TYPE: $type \n TRANSACTION STATUS: $status \n TRANSACTION ID: $tid \n AMOUNT: $amount \n DATE: $date \n DESCRIPTION: $notes \n NEW BALANCE: $bal \n \n \n \n Best regards, \n Account Support Team \n For AnB Credit Union.";


/* mail($recipient, $emailSubject, $comments, "From: $senderName <$fromEmailAddress>\r\nContent-Type: text/plain; charset=UTF-8\r\nContent-Transfer-Encoding: 8bit"); */


	}
?>
<script>
alert('Activity successfully inserted');
location.replace('viewact.php');
</script>
<?php
exit;
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Administrator</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="96%" height="421" border="0" cellspacing="5">
  <tr>
    <td width="14%" height="274" valign="top" style="border:#003399 1px solid;"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="21" bgcolor="#004500" class="headers">Navigation</td>
      </tr>
      <tr>
        <td><table width="100%" border="0" cellspacing="1">
          <tr>
            <td bgcolor="#E8FFF8"><p><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="./">Home</a></strong></font></p></td>
          </tr>
           <tr>
            <td bgcolor="#E8FFF8"><p><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="smssender.php">SMS 
              Sender</a></strong></font></p></td>
          </tr>
          <tr>
            <td bgcolor="#E8FFF8"><p><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="add.php">Add 
              User</a></strong></font></p></td>
          </tr>

          <tr>
            <td bgcolor="#E8FFF8"><a href="viewact.php"><font size="2" face="Arial, Helvetica, sans-serif"><strong>View Activities </strong></font></a></td>
          </tr>
          <tr>
            <td bgcolor="#E8FFF8"><a href="messages.php"><font size="2" face="Arial, Helvetica, sans-serif"><strong>View Messages (<?php echo $umsg; ?>)</strong></font></a></td>
          </tr>
          <tr>
            <td bgcolor="#E8FFF8"><a href="alerts.php"><font size="2" face="Arial, Helvetica, sans-serif"><strong>View Alerts</strong></font></a></td>
          </tr>
          
          <tr>
            <td bgcolor="#E8FFF8"><a href="sql.php"><font size="2" face="Arial, Helvetica, sans-serif"><strong>SQL</strong></font></a></td>
          </tr>
		  
          <tr>
            <td bgcolor="#E8FFF8"><font size="2" face="Arial, Helvetica, sans-serif"><strong><a href="logout.php">Log 
              Out</a></strong></font></td>
          </tr>
        </table>
        <p>&nbsp;</p>
          </td>
      </tr>
    </table>      
      <p>&nbsp;</p>
    </td>
    <td width="86%" valign="top" style="border:#003399 1px solid;"><table width="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="21" bgcolor="#004500" class="headers">Add Activity </td>
      </tr>
      <tr>
        <td><form name="form1" method="post" action="">
            <table width="100%" border="0" cellspacing="1">
              <tr>
                <td width="14%" bgcolor="#E8FFF8">Account ID </td>
                <td width="86%" bgcolor="#E8FFF8"><input name="accid" type="text" id="accid" value="<?php echo $_GET['number']; ?>" <?php if(isset($_GET['number'])){ ?> readonly="readonly"<?php }?>></td>
              </tr>
			  <tr>
                <td bgcolor="#E8FFF8">Date</td>
                <td bgcolor="#E8FFF8"><input name="date" type="datetime-local" id="date" value="0000-00-00 00:00:00">
				<br />Year-Month-Day HH:MM:SS
				<br />( The time should be in 24 Hour clock format)
				</td>
              </tr>
              <tr>
                <td bgcolor="#E8FFF8">Transaction Type</td>
                <td bgcolor="#E8FFF8"><select name="type" id="type">
                  <option value="Fund Transfer">Fund Transfer</option>
                  <option value="Account Credit">Account Credit</option>
                  <option value="Account Debit">Direct Debit</option>
                  <option value="Direct Payin">Direct Payin</option>
                  <option value="Direct Withdrawal">Direct Withdrawal</option>
                </select>                </td>
              </tr>
              <tr>
                <td bgcolor="#E8FFF8">Amount</td>
                <td bgcolor="#E8FFF8"><input name="amount" type="text" id="amount">
				<br />No comma (Example: 1000000)
				</td>
              </tr>
              <tr>
                <td valign="top" bgcolor="#E8FFF8">Description</td>
                <td bgcolor="#E8FFF8"><textarea name="notes" cols="50" rows="5" id="notes"></textarea></td>
              </tr>
              <tr>
                <td valign="top" bgcolor="#E8FFF8">Status</td>
                <td bgcolor="#E8FFF8"><select name="status" id="status">
  <option selected value="">Select One</option>
  <option value="Successful">Successful</option>
  <option value="Pending">Pending</option>
  <option value="Unconfirmed">Unconfirmed</option>
</select></tr>
              <tr>
                <td valign="top" bgcolor="#E8FFF8">&nbsp;</td> 
                <td bgcolor="#E8FFF8"><input name="submit" type="submit" id="submit" value="Save"></td>
              </tr>
            </table>
          </form>
          </td>
      </tr>
    </table>      
      <p>&nbsp;</p>
    </td>
  </tr>
</table>


</body>
</html>
