<?php
session_start();

if(!isset($_SESSION['customer_login'])) 
    header('location:index.php');   
?>


<?php

$cust_id=$_SESSION['cust_id'];
include '_inc/dbconn.php';
$success = "0";
$error_message = "";


if(!empty($_POST["submit_email"])) {
    $result = mysqli_query($con,"SELECT * FROM customer WHERE email='" . $_POST["email"] . "'");
$count  = mysqli_num_rows($result);
if($count>0) {
		// generate OTP
		$otp = rand(100000,999999);
		// Send OTP
		require_once("mail_function.php");
		$mail_status = sendOTP($_POST["email"],$otp);
		
		if($mail_status == 1) {
			$result = mysqli_query($con,"INSERT INTO otp_expiry(otp,is_expired,create_at) VALUES ('" . $otp . "', 0, '" . date("Y-m-d H:i:s"). "')");
			$current_id = mysqli_insert_id($con);
			if(!empty($current_id)) {
                $success=1;
			}
		}
	} else {
		$error_message = "Email does not exist!";
    }
}
if(!empty($_POST["submit_otp"])) {
	$result = mysqli_query($con,"SELECT * FROM otp_expiry WHERE otp='" . $_POST["otp"] . "' AND is_expired!=1 AND NOW() <= DATE_ADD(create_at, INTERVAL 24 HOUR)");
	$count  = mysqli_num_rows($result);
	if(!empty($count)) {
		$result = mysqli_query($con,"UPDATE otp_expiry SET is_expired = 1 WHERE otp = '" . $_POST["otp"] . "'");
		$success = 2;	
	} else {
		$success =1;
		$error_message = "Invalid OTP!";
	}	
}
?>



<?php include 'header.php' ?>

<?php include 'customer_navbar.php'?>
<style>
body{
font-family: calibri;
}
.form_container {
    border: 1px solid rgba(0, 0, 0, .05);
    box-shadow: 15px 26px 30px rgba(0, 0, 0, .09);
    border-left: 1px solid rgba(0, 0, 0, .06);
border-radius: 4px;
max-width: 500px;
padding: 20px 30px 30px;
text-align: center;
margin:0 auto;
}
.sanhead { font-size: 20px; }
.san { padding:20px; }
.error_message {
color: #b12d2d;
text-align: center;
background-color: #ffd9d9;
}
.message {
width: 100%;
max-width: 300px;
padding: 10px 30px;
border-radius: 4px;
margin: 0 auto;
margin-bottom: 5px;    
text-align: center;
}
.inputclass{
border: #CCC 1px solid;
padding: 10px 20px;
border-radius:4px;
}
.submit_button {
padding: 10px 20px;
background: #a9961e;
border: #736615 1px solid;
color: #FFF;
border-radius:4px;
}
</style>

<div class="content-wrapper">
          <div class="content">  



          <?php
if(!empty($error_message)) {
?>
<div class="message error_message"><?php echo $error_message; ?></div>
<?php
}
?>

<form name="frmUser" method="post" action="">
<div class="form-group">
<div class="row justify-content-center mt-50">

        <div class="col-lg-6 ">
       
											


                                                <?php 
			if(!empty($success == 1)) { ?>

<!-- Small Buttons -->
<div class="card card-default">
												<div class="card-header card-header-border-bottom">


													<h4 class="text-mute">Fund Transfer</h4>
												</div>
												<div class="card-body">
													<p class="mb-5">Please check your email for One-Time Password.</p>
                                                    <div class="text-right">

                                                        <div class="form-group">
                                                    <input type="text" class="form-control mb-5"  name="otp" placeholder="One Time Password" required>
                                                    </div>
													<input type="submit" name="submit_otp" class="btn btn-outline-success" value="Submit OTP">
                                                    <a href="./customer_account_summary.php" class="btn btn-outline-danger">Go Home</a>




                                                    

<?php 
} else if ($success == 2) {
    $sql="SELECT MAX(transactionid) from passbook".$cust_id;
    $result= mysqli_query($con, $sql) or die(mysqli_error($con));
    $rws=  mysqli_fetch_array($result);
    $s_last_tid=$rws[0];


  $sql="SELECT * from passbook".$cust_id." WHERE transactionid='$s_last_tid'";
    $result=mysqli_query($con, $sql) or die(mysqli_error($con));
    while($rws= mysqli_fetch_array($result)) {
    $s_ifsc=$rws[4];
    }

$result = mysqli_query($con,"SELECT * FROM otp_expiry WHERE otp='" . $_POST["otp"] . "'");

$count=mysqli_num_rows($result);
$row=mysqli_fetch_array($result);

if($count > 0){

    $ifsc_new = "<span class=\'mb-2 mr-2 badge badge-success\'>Successful</span>";
    $sql2="UPDATE passbook".$cust_id." SET ifsc='$ifsc_new'";
     mysqli_query($con,$sql2) or die(mysqli_error($con));
    
    echo '<script>alert("Transaction Successful");';
    echo 'window.location= "customer_mini_statement.php";
    </script>';
}
?>

<?php
}
else {
?>
<!-- Small Buttons -->
<div class="card card-default">
												<div class="card-header card-header-border-bottom">
 	<h4 class="text-mute">Fund Transfer</h4>
												</div>
												<div class="card-body">
													<p class="mb-5">Please continue, to get a One-Time Password for verification.</p>
                                                    <div class="text-right">

                                                    <?php
                                                    
                                                    $sql="SELECT email FROM customer WHERE id='$cust_id'";
                                                    $result4=  mysqli_query($con, $sql) or die(mysqli_error($con));
                                                    $rws=  mysqli_fetch_array($result4);
                                                    $email3 = $rws[0];
                                                    
                                                    
                                                    
                                                    
                                                    ?>

                                                    


                                                    <input type="hidden" name="email" value="<?php echo $email3;?>">
													<input type="submit" name="submit_email" class="btn btn-outline-success" value="Continue">
                                                    <a href="./customer_account_summary.php" class="btn btn-outline-danger">Go Home</a>
                                                    <?php 
}
?>
 </div>
 
 </form>
 
                                                    </div>
												</div>
											</div>

										</div>
</div>
</div>
</div>
</div>