<?php 
	
	include('header.php');
	
    // require '../userDash/PHPMailer/class.phpmailer.php';
    // require '../userDash/PHPMailer/PHPMailerAutoload.php';
	
	$date_ = date("m-d-Y");
	
	if(isset($_GET['uid'])){
	    $user = $_GET['uid'];
	    $acc_id = $_GET['acc'];
					
		$g_us = "select * from users where uID='$user'"; 
		$rs_result =  $conn->query($g_us); //run the query
		while($row = $rs_result->fetch_assoc()) { 
			$uid=$row['uID'];
			$fname=$row['fn'];
			$lname=$row['ln'];
			$add=$row['address'];
			$ct=$row['townCity'];
			$st=$row['state'];
			$count=$row['country'];
			$pz=$row['zip'];
			$ph=$row['pNumber'];
			$em=$row['email'];
			$dob=$row['dob'];
			$gen=$row['gender'];
			$nat=$row['nation'];
			$occ=$row['occu'];
			$acc=$row['memID'];
			$pw=$row['pass'];
			$cd=$row['cDate'];
			$ll=$row['LLogin'];
			$pic=$row['picture'];
			$tok=$row['token'];
		}
	    
	    $g_a = "select * from account where id='".$acc_id."'"; 
		$rs_t =  $conn->query($g_a); //run the query
		while($row = $rs_t->fetch_assoc()) { 
			$acid=$row['id'];
			$mID=$row['memID'];
			$at=$row['aType'];
			$an=$row['aName'];
			$cur=$row['currency'];
			$sym=$row['symbol'];
			$ab=$row['aBal'];
			$do=$row['dateOpened'];
			$cot=$row['cot_token'];
			$aNm=$row['aNum'];
			$cl=$row['cLimit'];
		}
	}
	
	if(isset($_GET['tid'])){
	    $st = "select * from trans where tid='".$_GET['tid']."'"; 
		$r_t =  $conn->query($st); //run the query
		while($row = $r_t->fetch_assoc()) { 
			$tid=$row['tid'];
			$tmid=$row['memID'];
			$tdate=$row['tDate'];
			$pac=$row['pAccno'];
			$narr=$row['narration'];
			$ammt=$row['amount'];
			$tSt="<option value='".$row['status']."'>".$row['status']."</option>";
			$ttype ="<option value='".$row['type']."'>".$row['type']."</option>";
		}
		
		$day = date("Y-m-d",strtotime("".$tdate.""));
		$time = date("h:i",strtotime("".$tdate.""));
		$button = "up";
		$bname="Update";
		$cancel_btn = "<a href='client_transactions.php?id=".$user."' class='btn btn-danger'>Cancel</a>";
	}else{
		$day="";
		$time="";
		$ttype="<option selected disabled value=''>Select Option</option>";
		$tSt="<option selected disabled value=''>Select Option</option>";
		$narr="";
		$ammt="";
		$button = "mp";
		$bname="Add";
		$cancel_btn = "";
	}
	
	if(isset($_GET['transaction'])){
	    $amount_ = $_GET['amount'];
	    $date__ = $_GET['date'];
        $name = $fname." ".$lname;
		$email = $em;
	    
	    if($_GET['transaction'] == "DEBIT"){
//     		$mail = new PHPMailer;
    											
//     		$mail->isSMTP();
//     		$mail->SMTPDebug = 0; 
//     		$mail->Host = "mail.fcb-capital.com";
//     		$mail->SMTPAuth = true;
//     		$mail->SMTPSecure = true;
//     		//$mail->Port = 25; 
//     		$mail->Port = 465; 
    		
//     		$mail->Username = 'no-reply@fcb-capital.com';
//     		$mail->Password = 'fe-XB;AYE-$Y';
    		
    		
//     		$mail->From = 'no-reply@fcb-capital.com';
//     		$mail->FromName = 'Guardian Trust - United';
//     		$mail->addAddress($email,$name);
    		
//     		$mail->IsHTML(true);
//     		$mail->WordWrap = 50;
//     			$body="<div style=\"padding:10px;background-color:#f5f5f5;border:1px solid #ccc;min-height:300px;\">";
//     			$body.="<h3 style=\"border-bottom:1px solid #ccc;padding:10px;\">Dear ".$name.", </h3><p>You have a transaction notification on your account.</p>
//     				<p>
//     					<strong>Date/Time: </strong> ".date("jS M, Y h:ia",strtotime("".$date__.""))."<br>
//     					<strong>Transaction Amount: </strong> ".number_format($amount_, 2)." DR<br>
//     					<strong>Account Balance: </strong> ".number_format($ab, 2)." CR
//     				</p>
//     				--
//     				<br>
//     				<img src='https://www.fcb-capital.com/wp-content/themes/spring/images/springbank-prime-1.png' style='width: 200px;' alt='Guardian Trust - United'><br>
//     				
//     				Email: customercare@fcb-capital.com <br>
//     				Website: <a href='https://fcb-capital.com/'>www.fcb-capital.com</a> <br>
//     				Address: 2093 Ryan Road,Sioux Falls South Dakota United States.<hr>
//     				<p style='text-align: center;'>THIS IS AN AUTOMATED RESPONSE. <b>**DO NOT RESPOND TO THIS EMAIL**</b></p>
//     			</div>
//     		";
    			
    				
    				
    			
//     		$mail->Subject = 'Transaction Notification';
//     		$mail->Body    = $body;
    		
    		
    		
    		 
// 			$mail->isHTML(true);
    		
    		
//     		if(!$mail->send()){
//     			//bad
//     			$error1 = "Something went wrong! Try again later.<hr>";	
//     		}else{	
                //good
                echo "<script>alert('Statement added successfully!');</script>";
	            echo "<script>window.location.href = 'client_transactions.php?id=".$uid."';</script>')";
    // 		}
    // 		$name=$email=NULL;
	    }elseif($_GET['transaction'] == "CREDIT"){
//     		$mail = new PHPMailer;
    											
//     		$mail->isSMTP();
//     		$mail->SMTPDebug = 0; 
//     		$mail->Host = "mail.fcb-capital.com";
//     		$mail->SMTPAuth = true;
//     		$mail->SMTPSecure = true;
//     		//$mail->Port = 25; 
//     		$mail->Port = 465; 
    		
//     		$mail->Username = 'no-reply@fcb-capital.com';
//     		$mail->Password = 'fe-XB;AYE-$Y';
    		
    		
//     		$mail->From = 'no-reply@fcb-capital.com';
//     		$mail->FromName = 'Guardian Trust - United';
//     		$mail->addAddress($email,$name);
    		
//     		$mail->IsHTML(true);
//     		$mail->WordWrap = 50;
//     			$body="<div style=\"padding:10px;background-color:#f5f5f5;border:1px solid #ccc;min-height:300px;\">";
//     			$body.="<h3 style=\"border-bottom:1px solid #ccc;padding:10px;\">Dear ".$name.", </h3><p>You have a transaction notification on your account.</p>
//     				<p>
//     					<strong>Date/Time: </strong> ".date("jS M, Y h:ia",strtotime("".$date__.""))."<br>
//     					<strong>Transaction Amount: </strong> ".number_format($amount_, 2)." CR<br>
//     					<strong>Account Balance: </strong> ".number_format($ab, 2)." CR
//     				</p>
//     				--
//     				<br>
//     				<img src='https://www.fcb-capital.com/wp-content/themes/spring/images/springbank-prime-1.png' style='width: 200px;' alt='Guardian Trust - United'><br>
//     				
//     				Email: customercare@fcb-capital.com <br>
//     				Website: <a href='https://fcb-capital.com/'>www.fcb-capital.com</a> <br>
//     				Address: 2093 Ryan Road,Sioux Falls South Dakota United States.<hr>
//     				<p style='text-align: center;'>THIS IS AN AUTOMATED RESPONSE. <b>**DO NOT RESPOND TO THIS EMAIL**</b></p>
//     			</div>
//     		";
    			
    				
    				
    			
//     		$mail->Subject = 'Transaction Notification';
//     		$mail->Body    = $body;
    		
    		
    		
    		 
// 			$mail->isHTML(true);
    		
    		
//     		if(!$mail->send()){
//     			//bad
//     			$error1 = "Something went wrong! Try again later.<hr>";	
//     		}else{	
                //good
                echo "<script>alert('Statement added successfully!');</script>";
	            echo "<script>window.location.href = 'client_transactions.php?id=".$uid."';</script>')";
    // 		}
    // 		$name=$email=NULL;
	    }
	}

	$error1="";
									 
	if(isset($_POST['mp'])){
		$ttype = $_POST['trantype'];
		$tdate = $_POST['tdate'];	
		$ttime = $_POST['ttime'];	
		$rem = $_POST['rem'];
		$status_ = $_POST['status'];

		$rece = $anu;
		$ammt = $_POST['amount'];
		$aball__ = $_POST['a_ball'];
			
		$datt = $tdate." ".$ttime;	

		if($ttype == "debit"){
			$newbalance = $aball__ - $ammt;
			$trrr__ = "DEBIT";
		}elseif($ttype == "credit"){
			$newbalance = $aball__ + $ammt;
			$trrr__ = "CREDIT";
		}
		
		$sqtr="INSERT INTO trans(memID, tDate, pAccno, amount, type, narration, status)values('$acc', '$datt', '$aNm', '$ammt', '$ttype', '$rem', '$status_')";												
		$resultr=mysqli_query($conn,$sqtr);

		if($resultr){
			if($status_ == "pending"){
				echo "<script>window.location.href = 'client_transactions.php?id=".$uid."';</script>')";
			}else{
				if($newbalance < 500){
					$error1 = "Error! Insufficient funds to make this transaction.<hr>";	
				}else{
					$ubal="update account set aBal='".$newbalance."' where id='".$acc_id."'";
					$uSet=mysqli_query($conn,$ubal);
					
					if($uSet){
						if($status_ == "complete"){
							echo "<script>window.location.href = 'add_statement.php?uid=$user&acc=$acc_id&transaction=".$trrr__."&amount=$ammt&date=$datt';</script>')";
						}else{
							echo "<script>window.location.href = 'client_transactions.php?id=".$uid."';</script>')";
						}
					}else{
						$error1 = "Something went wrong! Try again later.<hr>";	
					}
				}
			}
		}else{
			//echo mysqli_error('$conn');
			$error1 = "Something went wrong! Try again later.<hr>";	
		}
	}elseif(isset($_POST['up'])){
		$ttype = $_POST['trantype'];
		$tdate = $_POST['tdate'];	
		$ttime = $_POST['ttime'];	
		$rem = $_POST['rem'];
		$status_ = $_POST['status'];
		$ammt = $_POST['amount'];

		$rece = $anu;
		$datt = $tdate." ".$ttime;	
			
		$sqtr="update trans set tDate='".$datt."', amount='".$ammt."', type='".$ttype."', narration='".$rem."', status='".$status_."' where tid='".$tid."'";												
		$resultr=mysqli_query($conn,$sqtr);

		if($resultr){
			echo "<script>window.location.href = 'client_transactions.php?id=".$uid."';</script>')";
		}
	}
?>
    
    <!-- MENU SECTION END-->
    <div class="content-wrapper">
        <div class="container" >
			<div class="row">
				<div class="col-md-12 col-sm-12 col-xs-12" >
					<div class="panel panel-default">
						<div class="panel-heading" style="background-color:#1D2D40;color:white;font-size:20px;font-family:Agency FB">
							- Add Statement (Account No: <?php echo $aNm; ?> | User: <?php echo $fname." ".$lname; ?>)
						</div>
						<div class="panel-body">
							
							<form method="post">
								<div class="form-group">
									<div id="am_error" style="color: indianred; font-style: italic;"><?php echo $error1; ?></div>
									
									<label class="control-label">Transaction Date (yyyy-mm-dd)</label>
									<input id="flatpickr" value="<?php echo $day; ?>" class="form-control" type="date" placeholder="YYY-MM-DD" required title="Enter a date in this format yyyy-mm-dd" name="tdate" style="width: 100%;"><br>
									<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css">
									<script src="https://cdn.jsdelivr.net/npm/flatpickr"></script>
									<script>
										flatpickr('#flatpickr',{
										dateFormat: 'Y-m-d',
										disableMobile: "true"
										});
									</script>
									
									<label class="control-label">Transaction Time (Hour:Min AM/PM)</label><br>
									<input type="time" value="<?php echo $time; ?>" name="ttime"><br><br>
									
									<label class="control-label">Transaction Type</label> 
									<select class="form-control  c-square c-theme" name='trantype' required id="account">
										<?php echo $ttype; ?>
										<option value='debit'>Debit</option>
										<option value='credit'>Credit</option>
									</select><br>
									
									<label class="control-label">Transaction Status</label> 
									<select class="form-control  c-square c-theme" name='status' required id="account">
										<?php echo $tSt; ?>
										<option value='cot'>COT Code Pending</option>
										<option value='tax'>Tax Code Pending</option>
										<option value='authorization'>Authorization Code Pending</option>
										<option value='complete'>Transaction Completed</option>
									</select><br>
									
									<label class="control-label">Narration</label>
									<textarea class="form-control  c-square c-theme input-lg" name="rem" placeholder="Business, Bill Pay, Insurance ..."><?php echo $narr; ?></textarea><br>
									
								</div>
								<hr>
								<div class="form-group has-error">  
									<label class="control-label">Transaction Amount</label>
									<input type="number" class="form-control  c-square c-theme" id="amount" placeholder="0.00" name='amount' value="<?php echo $ammt; ?>" min="0" step="0.01" required>
									<input type="hidden" value="<?php echo $ab; ?>" name="a_ball">
								</div><br>
							
								<input type="submit" class="btn btn-primary" value="<?php echo $bname; ?> Statement" name="<?php echo $button; ?>">
								<?php echo $cancel_btn; ?>
							</form>
						</div>
					</div>
				</div>
			</div>
        </div>
    </div>
    <!-- CONTENT-WRAPPER SECTION END-->
    <footer style="background-color:#071A30">
        <div class="container">
            <div class="row">
                <div class="col-md-12">
                    Copyright &copy; <?php echo date("Y"); ?> | ALL RIGHTS RESERVED. 
                </div>

            </div>
        </div>
    </footer>
    <!-- FOOTER SECTION END-->
    <!-- JAVASCRIPT AT THE BOTTOM TO REDUCE THE LOADING TIME  -->
    <!-- CORE JQUERY SCRIPTS -->
    <script src="assets/js/jquery-1.11.1.js"></script>
    <!-- BOOTSTRAP SCRIPTS  -->
    <script src="assets/js/bootstrap.js"></script>

</body>
</html>
