<?php
function logInAccount(){
  global $conn;
  global $siteshortname;

  $account_id = mysqli_real_escape_string($conn, $_POST["user_id"]);
  $password = mysqli_real_escape_string($conn, $_POST["password"]);

  $query = mysqli_query($conn, "SELECT * FROM users WHERE account_id = '$account_id'");
  $numrow = mysqli_num_rows($query);
  if($numrow >= 1){
      
     
    $row = mysqli_fetch_assoc($query);
    $u_dbid = $row["id"];
    $dbpass = $row["b_password"];
    $b_email = $row["b_email"];
    $account_status = $row["account_status"];
    

    if($password == $dbpass){
        
        //Check account status of user if suspended.
    
    if($account_status == "Suspended"){
        echo "<script>$('#hide-fa').hide();</script>";
        echo '
              <div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                <i class="fa fa-warning"><i> Your account has been <b>SUSPENDED</b>, contact support@'.$siteshortname.'.online to resolve issue.
              </div>
    ';
    }else{
         // SESSION USER IDS

    $_SESSION["u_dbid"] = $u_dbid;

    echo "<script>window.location.assign('dashboard.php');</script>";
    }
    
    
   
    }else{
      echo "<script>$('#hide-fa').hide();</script>";
        echo '
              <div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                The password you entered is incorrect.
              </div>
    ';
    }
  }else{
    echo "<script>$('#hide-fa').hide();</script>";
        echo '
              <div class="alert alert-danger">
                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                The User ID you entered is invalid.
              </div>
    ';
  }
}

function sameBankTransfer(){
  global $conn;
  global $sitename;

  $account_numb = $_POST["account_numb"];

  $query = mysqli_query($conn, "SELECT * FROM users WHERE b_account_number = '$account_numb'");
  $numrow = mysqli_num_rows($query);
  if($numrow >= 1){
    $row = mysqli_fetch_assoc($query);

    $firstname = $row["b_firstname"];
    $middlename = $row["b_middlename"];
    $lastname = $row["b_lastname"];
    $the_user_id = $row["id"];
    
    echo '<script>
         $("#account_name").val("'.ucwords($firstname).'  '.ucwords($middlename).'  '.ucwords($lastname).'");
         $("#account_numbb").val("'.$account_numb.'");
         $("#hide-fa").hide();

    </script>';

    echo '<script>
         $("#panel-one").hide();
         $("#panel-two").show();
    </script>';
      
  }else{
     echo '<div class="alert alert-danger">
               <p style="font-size: 12px;"><i class="fa fa-warning" style="font-size: 18px;"></i>&nbsp;Hmm. The account number you entered does not match any '.$sitename.' bank account.</p>
             </div>';
             echo '<script>
                  $("#hide-fa").hide();
                  $("#panel-one").show();
                  $("#panel-two").hide();
               
             </script>';
  }
}

function sameBankTransfer2(){
  global $conn;
  global $u_dbid;
  global $db_account_pin;
  global $sitename;

  $transfer_amount = $_POST["amount"];
  $account_numbb = $_POST["account_numbb"];
  $account_name = $_POST["account_name"];
  $confirm_pin = $_POST["confirm_pin"];

  //START PHP CODE

  //FETCH USER FROM DETAILS[DEBIT]
  $query = mysqli_query($conn, "SELECT * FROM users WHERE id = '$u_dbid'");
  $row = mysqli_fetch_assoc($query);
   $D_firstname = $row["b_firstname"];
   $D_lastname = $row["b_lastname"];
   $D_middlename = $row["b_middlename"];
   $D_amount = $row["b_amount"];
   $D_id = $row["id"];
  // FETCH USER TO DETAILS[CREDIT]
   $query = mysqli_query($conn, "SELECT * FROM users WHERE b_account_number = '$account_numbb'");
   $row = mysqli_fetch_assoc($query);
    $C_firstname = $row["b_firstname"];
    $C_lastname = $row["b_lastname"];
    $C_middlename = $row["b_middlename"];
    $C_amount = $row["b_amount"];
    $C_id = $row["id"];
    $C_account_number = $row["b_account_number"];
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //DEBIT THE USER THAT SEND

    //Check if confirm pin is correct....
    if($db_account_pin == $confirm_pin){

      //first check if the user have enough funds
      if(($D_amount *1) > ($transfer_amount * 1)){

        $new_D_amount = ($D_amount*1) - ($transfer_amount*1);
        $query = mysqli_query($conn, "UPDATE users SET b_amount = '$new_D_amount' WHERE id = '$D_id'");

        // CREDIT THE USER SENT TO
        //first check how much the receiver  have and add the credited amount to it
        $new_C_amount = ($C_amount*1) + ($transfer_amount);
        $query = mysqli_query($conn, "UPDATE users SET b_amount = '$new_C_amount' WHERE id = '$C_id'");


        //DEBITED USER TRANSACTION....
        $D_user_id = $D_id;
        $D_date = date("m/d/y");
        $D_desc = "ACCOUNT DEBITED >> ".ucwords($D_firstname)." ".ucwords($D_middlename)." ".ucwords($D_lastname)." /ONLINE BANK TRANSFER/ ".ucwords($C_firstname)." ".ucwords($C_middlename)." ".ucwords($C_lastname)." ";

        $D_amount = $transfer_amount;

        $D_status = "DEBIT";
        $D_trans_type = "Same Bank Transfer";
        $D_trans_status = "APPROVED";
        $D_cot_trans_status = "1";
        $D_aml_trans_status = "1";
        $D_nrt_trans_status = "1";

        $D_r_name = "".ucwords($C_firstname)." ".ucwords($C_middlename)." ".ucwords($C_lastname)."";
        $D_r_account_number = $C_account_number;
        $D_r_bank_name = $sitename;
        



        $query = mysqli_query($conn, "INSERT INTO transfer VALUES('', '$D_user_id', '$D_date', '$D_desc', '$D_amount', '$D_status', '$D_trans_type', '$D_trans_status', '$D_cot_trans_status', '$D_aml_trans_status', '$D_nrt_trans_status', '$D_r_name', '$D_r_account_number', '$D_r_bank_name')");

        //CREDITED USER TRANSACTION....
        $C_user_id = $C_id;
        $C_date = date("m/d/y");
        $C_desc = "ACCOUNT CREDITED >> ".ucwords($C_firstname)." ".ucwords($C_middlename)." ".ucwords($C_lastname)." /ONLINE BANK TRANSFER/ ".ucwords($D_firstname)." ".ucwords($D_middlename)." ".ucwords($D_lastname)." ";
        $C_amount = $transfer_amount;
        $C_status = "CREDIT";
        
        $C_trans_type = "Same Bank Transfer";
        $C_trans_status = "APPROVED";
        $C_cot_trans_status = "1";
        $C_aml_trans_status = "1";
        $C_nrt_trans_status = "1";

        $C_r_name = "NULL";
        $C_r_account_number = "NULL";
        $C_r_bank_name = $sitename;

        $query = mysqli_query($conn, "INSERT INTO transfer VALUES('', '$C_user_id', '$C_date', '$C_desc', '$C_amount', '$C_status', '$C_trans_type', '$C_trans_status', '$C_cot_trans_status', '$C_aml_trans_status', '$C_nrt_trans_status', '$C_r_name', '$C_r_account_number', '$C_r_bank_name')");



        echo "<script>window.location.assign('same-digital-transfer-success.php?D_date=$D_date&&D_amount=$D_amount&&D_status=$D_status&&D_trans_status=$D_trans_status');</script>";

    }else{
      echo '<div class="alert alert-danger" style="border-radius:0px;">
                <p style="font-size: 12px;"><i class="fa fa-warning" style="font-size: 18px;"></i>&nbsp;Insufficient funds.</p>
              </div>';
      echo '<script>
                   $("#hide-faa").hide();
                
      </script>';
    }
   

    }else{
      echo '<div class="alert alert-danger" style="border-radius:0px;">
                <p style="font-size: 12px;"><i class="fa fa-warning" style="font-size: 18px;"></i>&nbsp;Incorrect Transaction Pin.</p>
              </div>';
      echo '<script>
                   $("#hide-faa").hide();
                
      </script>';
    }

 
}

function uploadPhotos(){
  global $conn;

  $user_id = $_POST["user_id"];
  $profile_pics = $_FILES["profile_pics"]["name"];

  if(!empty($profile_pics)){
        $target = "userphotos/".basename($_FILES["profile_pics"]["name"]);

        move_uploaded_file($_FILES["profile_pics"]["tmp_name"], $target);

        $query = mysqli_query($conn, "UPDATE users SET b_profile_pics = '$profile_pics'  WHERE id = '$user_id'");

        echo "<script>window.location.assign('myaccount.php');</script>";
  }else{
        echo "<script>alert('Select Images');</script>";
  }

}

function changePassword(){
  global $conn;

  $current_password = $_POST["current_password"];
  $new_password = $_POST["new_password"];
  $new_password2 = $_POST["new_password2"];
  $u_dbid = $_POST["u_dbid"];

  // Queri 1
  $query = mysqli_query($conn, "SELECT * FROM users WHERE id = '$u_dbid'");
  $row = mysqli_fetch_assoc($query);

  $dbcurrentpass = $row["b_password"];

  if($dbcurrentpass == $current_password){

    if($new_password == $new_password2){
            $query = mysqli_query($conn, "UPDATE users SET b_password = '$new_password' WHERE id = '$u_dbid'");
              echo '
                  <div class="alert alert-success">
                    <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                     <i class="fa fa-check"></i> Successfilly Changed.
                  </div>
              ';
              echo "<script>$('#hide-faa').hide();</script>";
              echo "<script>
                      $('#current_password').hide();
                      $('#new_password').hide();
                      $('#new_password2').hide();
                      $('#change_passwordbtn').hide();

                   </script>";
    }else{
        echo '
            <div class="alert alert-danger">
              <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
               <i class="fa fa-times"></i> The new password you entered does not match.
            </div>
        ';
        echo "<script>$('#hide-faa').hide();</script>";
    }

  }else{
      echo '
          <div class="alert alert-danger">
            <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
             <i class="fa fa-times"></i> The current password you entered is incorrect.
          </div>
      ';
      echo "<script>$('#hide-faa').hide();</script>";
     
  }
}

function emailForgetPassword(){
    global $conn;
    $email = $_POST["email"];

    $query = mysqli_query($conn, "SELECT * FROM users WHERE b_email = '$email'");
    $numrow = mysqli_num_rows($query);
    if($numrow >= 1){

        $row = mysqli_fetch_assoc($query);
        $u_dbid = $row["id"];


        $query = mysqli_query($conn, "UPDATE users SET b_password = '224122' WHERE id = $u_dbid");
        echo '<script>
             $("#panel-one").hide();
             $("#panel-two").hide();
             $("#panel-three").hide();
             $("#panel-last").show();
        </script>';

        ///SEND MAILSSSS
        //SEND MAIL CONTAINING USER ID.......

                 $email_to = $email;
                 $subject = "forget password";
                 

                 //BIG MESSAGE
                 $message = '
                 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                     <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                      <title>Account created.</title>
                      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
                    </head>
                    <body>
                      <div style="background-color:#f8f8f8;">
                        <img src="" class="img-responsive" width="160">
                      </div>
                 
                      Hi '.$b_firstname.'<br><br>
                      <br>
                      
                      <br>
                      <h3>ACCOUNT ID - '.$account_id.'</h3>
                      <br>
                      
                      <p style="font-size:16px;">
                       Your password has been successfully changed and your new password is <b>224122</b>
                      </p>
                      
     
                      <br><br>
                      <div style="background-color:#e10514; color:#fff;">&nbsp; &copy; '.date("Y").' copyright, zeenetcapital Bank INC</div>
                    </body>
                    </html>
                 ';

                    $headers[] = 'MIME-Version: 1.0';
                    $headers[] = 'Content-type: text/html; charset=iso-8859-1';

                    // Additional headers
                     $headers[] = 'From: zeenetcapital <info@zeenetcapital.com>';

                    //SEND MAIL
                     mail($email_to, $subject, $message,  implode("\r\n", $headers));


    }else{
        echo '<div class="alert alert-danger">
                  <p style="font-size: 12px;"><i class="fa fa-warning" style="font-size: 18px;"></i>&nbsp;Hmm. That doesn’t match our records. Please try again.</p>
                </div>';
                echo '<script>
                     $("#hide-fa").hide();
                     $("#panel-one").hide();
                     $("#panel-two").show();
                     $("#panel-three").hide();
                     $("#panel-last").hide();
                  
                </script>';
    }
}

function accountNumbForgetPassword(){
    global $conn;
    $account_number = $_POST["account_number"];

    $query = mysqli_query($conn, "SELECT * FROM users WHERE b_account_number = '$account_number'");
    $numrow = mysqli_num_rows($query);
    if($numrow >= 1){

        $row = mysqli_fetch_assoc($query);
        $u_dbid = $row["id"];


        $query = mysqli_query($conn, "UPDATE users SET b_password = '224122' WHERE id = $u_dbid");

        echo '<script>
             $("#panel-one").hide();
             $("#panel-two").hide();
             $("#panel-three").hide();
             $("#panel-last").show();
        </script>';

        ///SEND MAILSSSS
         ///SEND MAILSSSS
        //SEND MAIL CONTAINING USER ID.......

                 $email_to = $email;
                 $subject = "Forget Password";
                 

                 //BIG MESSAGE
                 $message = '
                 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    <html xmlns="http://www.w3.org/1999/xhtml">
                     <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                      <title>Account created.</title>
                      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
                    </head>
                    <body>
                      <div style="background-color:#f8f8f8;">
                        <img src="https://en.zeenetcapital.com/images/zeelogo.png" class="img-responsive" width="160">
                      </div>
                 
                      Hi '.$b_firstname.'<br><br>
                      <br>
                      
                      <br>
                      <h3>ACCOUNT ID - '.$account_id.'</h3>
                      <br>
                      
                      <p style="font-size:16px;">
                      Your password has been successfully changed and your new password is <b>224122</b>
                      </p>
                      
     
                      <br><br>
                      <div style="background-color:#e10514; color:#fff;">&nbsp; &copy; '.date("Y").' copyright, zeratinternational Bank INC</div>
                    </body>
                    </html>
                 ';

                    $headers[] = 'MIME-Version: 1.0';
                    $headers[] = 'Content-type: text/html; charset=iso-8859-1';

                    // Additional headers
                     $headers[] = 'From: zeratinternational Bank <info@zeratinternational.com>';

                    //SEND MAIL
                     mail($email_to, $subject, $message,  implode("\r\n", $headers));

    }else{
        echo '<div class="alert alert-danger">
                  <p style="font-size: 12px;"><i class="fa fa-warning" style="font-size: 18px;"></i>&nbsp;Hmm. That doesn’t match our records. Please try again.</p>
                </div>';
                echo '<script>
                     $("#hide-faa").hide();
                     $("#panel-one").hide();
                     $("#panel-two").hide();
                     $("#panel-three").show();
                     $("#panel-last").hide();
                  
                </script>';
    }
}
?>