<?php 
session_start();
        
if(!isset($_SESSION['customer_login'])) 
    header('location:index.php');   
?>
<?php
     $t_amount=$_REQUEST['t_val'];
     $sender_id=$_SESSION["login_id"];
     $reciever_bank=$_REQUEST['bank'];
     $reciever_accnum=$_REQUEST['accnum'];
     $reciever_name=$_REQUEST['transname'];
     // //select last transaction id in reciever's passbook.
     include '_inc/dbconn.php';
     // $sql="SELECT MAX(transactionid) from passbook".$reciever_id;
     // $result=mysqli_query($con, $sql) or die(mysqli_error($con));
     // $rws=  mysqli_fetch_array($result);
     // $r_last_tid=$rws[0];
    
    // //select the details in the last row of reciever's passbook.
    // $sql="SELECT * from passbook".$reciever_id." WHERE transactionid='$r_last_tid'";
    // $result=mysqli_query($con, $sql) or die(mysqli_error($con));
    // while($rws= mysqli_fetch_array($result)){
    // $r_amount=$rws[7];
    // $r_name=$rws[2];
    // $r_branch=$rws[3];
    // $r_ifsc=$rws[4];
    // }
    
   //select the last transaction id in the sender's passbook
    $sql="SELECT MAX(transactionid) from passbook".$sender_id;
    $result= mysqli_query($con, $sql) or die(mysqli_error($con));
    $rws=  mysqli_fetch_array($result);
    $s_last_tid=$rws[0];
    
    //select the details in the last row of sender's passbook.
    $sql="SELECT * from passbook".$sender_id." WHERE transactionid='$s_last_tid'";
    $result=mysqli_query($con, $sql) or die(mysqli_error($con));
    while($rws= mysqli_fetch_array($result)) {
    $s_amount=$rws[7];
    $s_name=$rws[2];
    $s_branch=$rws[3];
    $s_ifsc=$rws[4];
    }
    
    $date=date( 'Y-m-d');
    
    $s_total=$s_amount-$t_amount; //sender's final balance.
    
    if($s_amount<=10)
    {
        echo '<script>alert("Your account balance is less than $10\n\nYou must maintain a minimum balance of $10 in order to proceed with the transfer.");';
        echo 'window.location= "customer_transfer.php";</script>';
    }
    elseif($t_amount<1){
         echo '<script>alert("You cannot transfer less than $1");';
        echo 'window.location= "ccustomer_account_summary.php";</script>';
    }
    elseif($s_total<10)
    {
        echo '<script>alert("You do not have enough balance in your account to proceed this transfer.\n\nYou must maintain a minimum of $10 in your account.");';
        echo 'window.location= "customer_account_summary.php";</script>';
    }
    
    else{ 
        //insert statement into reciever passbook.
        // $r_total=$r_amount+$t_amount;
        // $sql1="insert into passbook".$reciever_id." values('','$date','$r_name','$r_branch','$r_ifsc','$t_amount','0','$r_total','BY $s_name')";
        // mysqli_query($con,$sql1) or die(mysqli_error($con));
        
        //insert statement into sender passbook.
       $new_rn = strtoupper($reciever_name);
        $s_total=$s_amount-$t_amount;
        $s_ifsc="<a href=\'./transfer-confirmation.php\'><span class=\'mb-2 mr-2 badge badge-warning\'>Pending</span></a>.";
        $sql2="insert into passbook".$sender_id." values('0','$date','$s_name','$reciever_bank','$s_ifsc','0','$t_amount','$s_total','TRANSFER TO $new_rn -$reciever_accnum ')";
         mysqli_query($con,$sql2) or die(mysqli_error($con));
        
    //   echo 'alert("Transfer Pending.");';
        echo '<script>window.location= "transfer-confirmation.php";</script>';
    }
?>



