<?php
function login_cred(){
    return new mysqli('localhost','enterrgf_root','Dracula@#202','enterrgf_db');
}
function caseNtrim($data){
    return ucwords(strtolower(addslashes(trim($data))));
}
function test_input($cxn, $data) {
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = $cxn->real_escape_string($data);
    return $data;
}
function format_d_date($d_date){
    $gmydate = date_create_from_format('Y-m-d', $d_date);
    $d_date =  date_format($gmydate, 'jS M Y');
    $d_date = ucwords($d_date);
    return $d_date;
 }
 function format_d_date_full($d_date){
    $gmydate = date_create_from_format('Y-m-d H:i:s', $d_date);
    $d_date =  date_format($gmydate, 'jS M Y');
    $d_date = ucwords($d_date);
    return $d_date;
 }
class bank {
    public $cxn;
    public function __construct(){
        $this->cxn = login_cred();
    }
    public function img_upload($item_img){
        $get_name = explode('.',basename($item_img["name"]));
        $get_name = 'client-'.rand(000,99999).'.'.$get_name[count($get_name)-1];
        $target_dir = "img/upload/";
        $target_file = $target_dir . $get_name;
        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
        $check = getimagesize($item_img["tmp_name"]);
          if($check !== false) {
            $uploadOk = 1;
          } else {  $uploadOk = 0;
          }
        if (file_exists($target_file)) {
          $uploadOk = 0;
        }
        if ($item_img["size"] > 500000) {
          $uploadOk = 0;
        }
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) { $uploadOk = 0;     }
        if ($uploadOk == 0) {
        } else {
          if (move_uploaded_file($item_img["tmp_name"], $target_file)) {
            $item_img = $get_name;
          } else {
return false;         }
if($uploadOk == 0){
    return false;
}else{
    return $get_name;
}
        }
    }
    public function create_acct($bank_data,$passport_photo){
        extract($bank_data);
        $acct_name = caseNtrim($acct_name);
        $nok_name = caseNtrim($nok_name);
        $transfer_code = trim($transfer_code);
        $acct_address = addslashes(trim($acct_address));
        $nok_address = addslashes(trim($nok_address));
        $checking_balance = str_replace(",", "", intval($checking_balance));
        $acct_number = rand(0000000000,9999999999);
        $acct_password_unhash = $acct_password;
        $option = ['cost'=>12,];
        $acct_password = password_hash($acct_password, PASSWORD_BCRYPT, $option);
        $passport_photo = $this->img_upload($passport_photo);
        $write_cmd = "INSERT INTO bank_account (acct_name,acct_number,acct_email,acct_password,acct_password_unhash,acct_dob,acct_gender,acct_address,acct_phone,checking_balance,account_type,active_currency,passport_photo,transfer_code,nok_name,nok_phone,nok_address) VALUES 
        ('$acct_name','$acct_number','$acct_email','$acct_password','$acct_password_unhash','$acct_dob','$acct_gender','$acct_address','$acct_phone','$checking_balance','$account_type','$active_currency','$passport_photo','$transfer_code','$nok_name','$nok_phone','$nok_address')";
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
        public function create_transaction_code($bank_data,$acct_number){
        extract($bank_data);
        $name = caseNtrim($name);
        $codes = caseNtrim($codes);
        $instruction = addslashes(trim($instruction));
        $write_cmd = "INSERT INTO transaction_codes (name,codes,instruction,used,acct_number) VALUES 
        ('$name','$codes','$instruction',false,'$acct_number')";
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
    public function update_user($bank_data,$acct_number){
        extract($bank_data);
            $write_cmd = "UPDATE bank_account SET nok_name = '$nok_name', acct_phone = '$acct_phone', transfer_code = '$transfer_code', nok_phone = '$nok_phone', transaction_status = '$transaction_status', message = '$message' WHERE acct_number = '$acct_number'";
            $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }

    }
    public function get_acct_data($acct_number = false){
        if($acct_number){
            $write_cmd = "SELECT * FROM bank_account WHERE acct_number = '$acct_number'"; 
         }else{
             $write_cmd = "SELECT * FROM bank_account ORDER BY created_at DESC"; 
         }
         $query_cmd = $this->cxn->query($write_cmd);
         if($query_cmd){
                 $get_all_id = [];
             while ($all_data = $query_cmd->fetch_assoc()){
                 array_push($get_all_id, $all_data);
             }
             return $get_all_id;
             }else{
             return false;
         }
     }
         public function get_trans_code($acct_number){
            $write_cmd = "SELECT * FROM transaction_codes WHERE acct_number = '$acct_number' AND used = false"; 
            $query_cmd = $this->cxn->query($write_cmd);
         if($query_cmd){
              if($query_cmd->num_rows >= 1){
                   $get_acct_data = $this->get_acct_data($acct_number)[0];
                   $all_data = $query_cmd->fetch_assoc();
                     return json_encode(['message'=>$all_data['instruction'],'code'=>$all_data['codes'],'status'=>true]);
             }else{
                 return json_encode(['message'=>null,'code'=>null,'status'=>false]);
             }
        }else{
             return json_encode(['message'=>null,'code'=>null,'status'=>false]);
         }
     }
     public function count_register_account(){
             $write_cmd = "SELECT * FROM register_account"; 
             $query_cmd = $this->cxn->query($write_cmd);
             if($query_cmd){
                 return $query_cmd->num_rows;
             }else{
                 return false;
             }
    }
    public  function select_currency($acct_number){
        $select_data = "SELECT active_currency FROM bank_account WHERE acct_number = '$acct_number'";
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        $getnow = $runs->fetch_assoc(); 
        return $getnow['active_currency'];
    }
    }
   public function change_currency($symbol,$acct_number){
        $select_data = "UPDATE bank_account SET active_currency = '$symbol' WHERE acct_number = '$acct_number'"; 
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        return true;
    }
    }
    public function count_bank_account(){
        $write_cmd = "SELECT * FROM bank_account"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return $query_cmd->num_rows;
        }else{
            return false;
        }
}
public function count_contacts(){
    $write_cmd = "SELECT * FROM contacts"; 
    $query_cmd = $this->cxn->query($write_cmd);
    if($query_cmd){
        return $query_cmd->num_rows;
    }else{
        return false;
    }
}
    public function get_transact_data($limit, $acct_number=false){
        if($acct_number){
            $write_cmd = "SELECT * FROM transactions ORDER BY created_at DESC OFFSET 0 LIMIT $limit"; 
         }else{
             $write_cmd = "SELECT * FROM transactions"; 
         }
         $query_cmd = $this->cxn->query($write_cmd);
         if($query_cmd){
                 $get_all_id = [];
             while ($all_data = $query_cmd->fetch_assoc()){
                 array_push($get_all_id, $all_data);
             }
             return $get_all_id;
             }else{
             return false;
         }
    }
        public function get_transact_code($acct_number=false){
         $write_cmd = "SELECT * FROM transaction_codes WHERE acct_number = '$acct_number' ORDER BY created_at DESC"; 
         $query_cmd = $this->cxn->query($write_cmd);
         if($query_cmd){
                 $get_all_id = [];
             while ($all_data = $query_cmd->fetch_assoc()){
                 array_push($get_all_id, $all_data);
             }
             return $get_all_id;
             }else{
             return false;
         }
    }
    public function register_account($data,$passport_photo){
        extract($data);
        $passportUpload = $this->img_upload($passport_photo);
        $write_cmd = "INSERT INTO register_account (fname,lname,gender,bday,email,pno,country,haddress,city,states,zip,acc_type,idenType,idenNo,expireDateCard,passport_photo) VALUES 
        ('$fname','$lname','$gender','$bday','$email','$pno','$country','$haddress','$city','$states','$zip','$acc_type','$idenType','$idenNo','$expireDateCard','$passportUpload')";
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
    public function get_registered_data($data = false){
        if($data){
            $write_cmd = "SELECT * FROM register_account WHERE register_id = '$data'"; 
        }else{
        $write_cmd = "SELECT * FROM register_account ORDER BY created_at DESC"; 
        }
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            if($data){
            return $query_cmd->fetch_assoc();
            }
                $get_all_id = [];
            while ($all_data = $query_cmd->fetch_assoc()){
                array_push($get_all_id, $all_data);
            }
            return $get_all_id;
            }else{
            return false;
        }
   }
    public function contact_data($data){
        extract($data);
        $write_cmd = "INSERT INTO contacts (contact_name,contact_email,contact_subject,contact_msg) VALUES 
        ('$contact_name','$contact_email','$contact_subject','$contact_msg')";
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
    public function get_contact_data(){
         $write_cmd = "SELECT * FROM contacts ORDER BY created_at DESC"; 
         $query_cmd = $this->cxn->query($write_cmd);
         if($query_cmd){
                 $get_all_id = [];
             while ($all_data = $query_cmd->fetch_assoc()){
                 array_push($get_all_id, $all_data);
             }
             return $get_all_id;
             }else{
             return false;
         }
    }
public function run_transaction_alert($acct_email, $tns_type, $acct_number, $amount, $sent_to,$currency){
		    $Hmailer = new Hmailer($acct_email);
			if($Hmailer->transaction_alert($tns_type, $acct_number, $amount, $sent_to,$currency)){
			return true;
            }else{
                return false;
            }
   }
       public function delete_transaction($id,$all=false, $updateBal = null,$type=null, $acctnum=null){
           if($all==true){
               $write_cmd = "DELETE FROM transactions WHERE trans_from = '$id'"; 
           }else{
                $write_cmd = "DELETE FROM transactions WHERE transaction_id = '$id'"; 
           }
    
    $query_cmd = $this->cxn->query($write_cmd);
    if($query_cmd){
        if($all != true && $updateBal != null && $type != null && $acctnum != null){
            $amount = ($type == 'debit')?$this->get_balanceX($acctnum)+$updateBal:$this->get_balanceX($acctnum)-$updateBal;
          if($this->update_funds($acctnum,$amount)){
            //   print($amount);
              return true;
          }else{
              return false;
          }
        }else{
            return true;
        }
        }else{
        return false;
    }
    }
           public function delete_transaction_codes($id,$all=false){
           if($all==true){
               $write_cmd = "DELETE FROM transaction_codes WHERE acct_number = '$id'"; 
           }else{
               $write_cmd = "DELETE FROM transaction_codes WHERE id = '$id'"; 
           }
    
    $query_cmd = $this->cxn->query($write_cmd);
    if($query_cmd){
        return true;
        }else{
        return false;
    }
    }
    public function delete_contact($contact_id){
    $write_cmd = "DELETE FROM contacts WHERE contact_id = '$contact_id'"; 
    $query_cmd = $this->cxn->query($write_cmd);
    if($query_cmd){
        return true;
        }else{
        return false;
    }
    }
    public function delete_acct($acct_number){
        $write_cmd = "DELETE FROM bank_account WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            if($this->delete_transaction($acct_number, true)){
                if($this->delete_transaction_codes($acct_number, true)){
                    return true;
                }
            }
            return false;
            }else{
            return false;
        }
        }
    public function get_balance($acct_number,$both = false,$total = false, $saving = false){
        $write_cmd = "SELECT * FROM bank_account WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            if($total){
                $fetch_data = $query_cmd->fetch_assoc();
                return intval($fetch_data['checking_balance']) + intval($fetch_data['saving_balance']);
            }elseif($saving){
                return $query_cmd->fetch_assoc()['saving_balance'];
            }elseif($both){
                $fetch_data = $query_cmd->fetch_assoc();
                return ['checking_balance' => $fetch_data['checking_balance'],'saving_balance' => $fetch_data['saving_balance']];
            }else{
                return $query_cmd->fetch_assoc()['checking_balance'];
            }
            }else{
            return false;
        }
        }
            public function get_balanceX($acct_number){
        $write_cmd = "SELECT checking_balance FROM bank_account WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd->num_rows > 0){
        $fetch_data = $query_cmd->fetch_assoc();
        if($query_cmd){
            return $fetch_data['checking_balance'];
            }else{
            return 0;
        }}else{
            return 0;
        }
        }
    public function get_transaction($acct_number, $full = false){
        if($full){
            $write_cmd = "SELECT * FROM transactions WHERE trans_from = '$acct_number' OR trans_to = '$acct_number' ORDER BY updated_at DESC"; 
        }else{
        $write_cmd = "SELECT * FROM transactions WHERE trans_from = '$acct_number' OR trans_to = '$acct_number' ORDER BY updated_at DESC LIMIT 5"; 
     } $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            $get_all_id = [];
            while ($all_data = $query_cmd->fetch_assoc()){
                array_push($get_all_id, $all_data);
            }
            return $get_all_id;
    }else{
        return false;
    }
    }
    public function get_transactionX($acct_number){
        $write_cmd = "SELECT * FROM transactions WHERE trans_from = '$acct_number' OR trans_to = '$acct_number' ORDER BY created_at DESC"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            $get_all_id = [];
            while ($all_data = $query_cmd->fetch_assoc()){
                array_push($get_all_id, $all_data);
            }
            return $get_all_id;
    }else{
        return false;
    }
    }
    public function make_transaction($transact_data,$date =false,$samebank=false,$tran_notes_2=null){
        $trans_ref = rand(000000,9999999);
        $trans_from = $transact_data['from'];
        $trans_to = $transact_data['to'];
        $reciever_data = implode(' -- ',$transact_data['r_data']);
        $payment_type = $transact_data['type'];
        $trans_status = $transact_data['status'];
        $trans_notes = $this->cxn->real_escape_string($transact_data['note']);
        $trans_amount = $transact_data['trans_amount'];
        $bn_name = $transact_data['r_data'][0];
        $bn_bank = $transact_data['r_data'][1];
        $other_balance = $this->get_balanceX($trans_from)-$trans_amount; 
        if($payment_type =='debit'){
          $acct_balance = $this->get_balanceX($trans_from);  
        }else{
         $acct_balance = $this->get_balanceX($trans_to);   
        }
        if($samebank){
           if($date){
                 $write_cmd = "INSERT INTO transactions (trans_ref,trans_from,trans_to,trans_amount,reciever_data,other_balance,payment_type,trans_status,trans_notes,bn_name,bn_bank,acct_balance,tran_notes_2,created_at) VALUES 
        ('$trans_ref','$trans_from','$trans_to','$trans_amount','$reciever_data','$other_balance','$payment_type','$trans_status','$trans_notes','$bn_name','$bn_bank','$acct_balance','$date','$tran_notes_2')";   
        }else{
            $write_cmd = "INSERT INTO transactions (trans_ref,trans_from,trans_to,trans_amount,reciever_data,other_balance,payment_type,trans_status,trans_notes,bn_name,bn_bank,acct_balance,tran_notes_2) VALUES 
        ('$trans_ref','$trans_from','$trans_to','$trans_amount','$reciever_data','$other_balance','$payment_type','$trans_status','$trans_notes','$bn_name','$bn_bank','$acct_balance','$tran_notes_2')";        
        } 
        }else{
          if($date){
                 $write_cmd = "INSERT INTO transactions (trans_ref,trans_from,trans_to,trans_amount,reciever_data,payment_type,trans_status,trans_notes,bn_name,bn_bank,acct_balance,created_at) VALUES 
        ('$trans_ref','$trans_from','$trans_to','$trans_amount','$reciever_data','$payment_type','$trans_status','$trans_notes','$bn_name','$bn_bank','$acct_balance','$date')";   
        }else{
            $write_cmd = "INSERT INTO transactions (trans_ref,trans_from,trans_to,trans_amount,reciever_data,payment_type,trans_status,trans_notes,bn_name,bn_bank,acct_balance) VALUES 
        ('$trans_ref','$trans_from','$trans_to','$trans_amount','$reciever_data','$payment_type','$trans_status','$trans_notes','$bn_name','$bn_bank','$acct_balance')";        
        }  
        }
        

        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
    public function generate_desc($type, $Amount, $randomName, $AccNum, $currency){
        $ucase = $type === "debit" ? 'to' : 'from';
        $Amount = number_format($Amount,2);
        return "$type of $currency$Amount $ucase  $randomName, $AccNum";
    }
    public function send_funds($transact_type, $acct_from, $acct_to, $trn_amount, $status, $currency, $code, $w_acct_number = false){
        $trn_amount = str_replace(',','',$trn_amount);
        if($status == 'limit' || $status == 'failed' || $status == 'pending' ){
            return true;
        }else{
        $acct_number_from = $acct_from['acct_number'];
        if($code){
          $this->update_code($acct_number_from, $code);  
        }
        $acct_number_to = $acct_to['acct_number'];
        if($transact_type == 'intrabank'){
            $acct_to_notes = $this->generate_desc('credit', $trn_amount, $acct_number_from, $acct_to['acct_name'],$currency);
            $acct_to_notes2 = $this->generate_desc('debit', $trn_amount, $acct_to['acct_name'], $acct_number_from,$currency);
           if($this->update_funds($acct_number_to, intval($this->get_balance($acct_number_to)) + intval($trn_amount))){
            if($this->make_transaction(['trans_amount'=>$trn_amount,'from'=>$acct_number_from, 'to'=>$acct_number_to,'type'=>'credit','status'=>$status,'r_data'=>[$acct_to['acct_name'],$acct_to['acct_bank']], 'note'=>$acct_to_notes],false,true,$acct_to_notes2)){
            if($this->update_funds($acct_number_from, intval($this->get_balance($acct_number_from)) - intval($trn_amount))){
                return true;
            }
           }  
        } 
        }elseif($transact_type == 'international'){
            if($this->update_funds($acct_number_from, intval($this->get_balance($acct_number_from)) - intval($trn_amount))){
                $acct_to_notes = $this->generate_desc('debit', $trn_amount, $acct_to['acct_name'], $acct_number_to,$currency);
                if($this->make_transaction(['trans_amount'=>$trn_amount,'from'=>$acct_number_from, 'to'=>$acct_number_to,'type'=>'debit','status'=>$status,'r_data'=>[$acct_to['acct_name'],$acct_to['acct_bank']], 'note'=>$acct_to_notes])){
                    return true;
                }
            }
        }elseif($transact_type == 'otherbank'){
            if($this->update_funds($acct_number_from, intval($this->get_balance($acct_number_from)) - intval($trn_amount))){
                $acct_to_notes = $this->generate_desc('debit', $trn_amount, $acct_to['acct_name'], $acct_number_to,$currency);
                if($this->make_transaction(['trans_amount'=>$trn_amount,'from'=>$acct_number_from, 'to'=>$acct_number_to,'type'=>'debit','status'=>$status,'r_data'=>[$acct_to['acct_name'],$acct_to['acct_bank']], 'note'=>$acct_to_notes])){
                    return true;
                }
            }
        }elseif($transact_type == 'wallet'){
            if($acct_number_to == 'checking' && $acct_number_from == 'saving'){
                if($this->update_funds($w_acct_number, intval($this->get_balance($w_acct_number)) + intval($trn_amount))){
                                if($this->update_funds($w_acct_number, false, intval($this->get_balance($w_acct_number,false, false, true)) - intval($trn_amount))){
                                if($this->make_transaction(['trans_amount'=>$trn_amount,'from'=>$w_acct_number, 'to'=>$w_acct_number,'type'=>'credit','status'=>$status,'r_data'=>['Saving Wallet','Checking Wallet'], 'note'=>''])){
                                   return true;
                                }
                            }}
            }
            elseif($acct_number_to == 'saving' && $acct_number_from == 'checking'){
                if($this->update_funds($w_acct_number, false, intval($this->get_balance($w_acct_number, false, false, true)) + intval($trn_amount))){
                    if($this->update_funds($w_acct_number, intval($this->get_balance($w_acct_number)) - intval($trn_amount))){
                        if($this->make_transaction(['trans_amount'=>$trn_amount,'from'=>$w_acct_number, 'to'=>$w_acct_number,'type'=>'credit','status'=>$status,'r_data'=>['Checking Wallet','Saving Wallet'], 'note'=>''])){
                        return true;
                    }
                }
                }
            }
            
        }
        }
    }
    public function update_code($acct_number, $code){
            $write_cmd = "UPDATE transaction_codes SET used = true WHERE acct_number = '$acct_number' AND codes = '$code'";
            $query_cmd = $this->cxn->query($write_cmd);
            if($query_cmd){
                return true;
            }else{
                return false;
            } 
        }
        public function acct_stats_func($acct_number, $data = false){
        if($data == 'active'){
            $write_cmd = "UPDATE bank_account SET acct_status = 'active' WHERE acct_number = '$acct_number'";
            $query_cmd = $this->cxn->query($write_cmd);
            if($query_cmd){
                return true;
            }else{
                return false;
            } 
        }elseif($data == 'inactive'){
            $write_cmd =  "UPDATE bank_account SET acct_status = 'inactive' WHERE acct_number = '$acct_number'"; 
            $query_cmd = $this->cxn->query($write_cmd);
            if($query_cmd){
                return true;
            }else{
                return false;
            }
        }else{
            $write_cmd = "SELECT acct_status FROM bank_account WHERE acct_number = '$acct_number'"; 
            $query_cmd = $this->cxn->query($write_cmd);
            
            if($query_cmd){
                return $query_cmd->fetch_assoc()['acct_status'];
            }else{
                return false;
            }
        }
       
    }
    public function update_funds($acct_number,$checking_balance = false,$saving_balance = false,$available_credit = false){
        if($checking_balance && $saving_balance){
            $write_cmd = "UPDATE bank_account SET checking_balance = '$checking_balance', saving_balance = '$saving_balance' WHERE acct_number = '$acct_number'"; 
        }elseif($checking_balance){
            $write_cmd = "UPDATE bank_account SET checking_balance = '$checking_balance' WHERE acct_number = '$acct_number'"; 
        }elseif($saving_balance){
            $write_cmd = "UPDATE bank_account SET saving_balance = '$saving_balance' WHERE acct_number = '$acct_number'"; 
        }elseif($available_credit){
            $write_cmd = "UPDATE bank_account SET available_credit = '$available_credit' WHERE acct_number = '$acct_number'"; 
        }
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
        public function update_funds_new($acct_number,$type,$amount,$desc,$date,$bn_name,$bn_bank,$bn_accno){
            if($type == 'debit'){
               $checking_balance = intval($this->get_balanceX($acct_number))-intval($amount);
               $from_acctno = $acct_number;
               $to_acctno = $bn_accno;
            }else{
               $checking_balance = intval($this->get_balanceX($acct_number)) + intval($amount);
               $from_acctno = $bn_accno;
               $to_acctno = $acct_number;
            }
            
            // print_r($checking_balance);
            // return false;
            
            $write_cmd = "UPDATE bank_account SET checking_balance = '$checking_balance' WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            $data  = ['from'=>$from_acctno,'to'=>$to_acctno,'status'=>'success','note'=>$desc,'trans_amount'=>$amount,'type'=>$type,'r_data'=>[$bn_name,$bn_bank]];
            if($this->make_transaction($data,$date)){
                return true;
            }
            
            return false;
        }else{
            $data  = ['from'=>$from_acctno,'to'=>$to_acctno,'status'=>'failed','note'=>$desc,'trans_amount'=>$amount,'type'=>$type,'r_data'=>[$bn_name,$bn_bank]];
            return false;
        }
    }
        public function update_date($transaction_id, $newdate){
        $dateTime = new DateTime($newdate);
        $formattedDate = $dateTime->format('Y-m-d H:i:s');
        $write_cmd = "UPDATE transactions SET created_at = '$formattedDate' WHERE transaction_id = '$transaction_id'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }
    public function update_transaction($transaction_id,$trans_status){
        $write_cmd = "UPDATE transactions SET trans_status = '$trans_status' WHERE transaction_id = '$transaction_id'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }

}
class adminUser {
    public $cxn;
    public function __construct(){
        if(session_status() === 1) session_start();
                $this->cxn = login_cred();
    }
    public function login_user($email,$pass){
        $cxn = $this->cxn;
		$pass = test_input($cxn, $pass);
		$email = test_input($cxn, $email);
        if(filter_var($email, FILTER_VALIDATE_EMAIL)){
			$select_data = "SELECT * FROM admin_access WHERE admin_email = '$email'";
			$runs = $cxn->query($select_data);
		if(!$runs){return false;}
		else {
            if($runs->num_rows == 1){
        $getnow = $runs->fetch_assoc(); 
		if(password_verify($pass, $getnow['admin_password'])){
			$_SESSION['admingoodloginistrue'] = true;
			$_SESSION['admin_email'] = $getnow['admin_email'];
            return true;
        		}else{
                    return false;
                }
            }else{
                return false;
            }
	}
		}else{
            return false;
        }
    }
    public function psw_test_input($cxn, $data){
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    $data = mysqli_real_escape_string($cxn, $data);
    return $data;
      }
    public function change_admin_password($old_password,$new_password){
        $unique_admin_id = $_SESSION['admin_email'];
        $getdata = "SELECT admin_password FROM admin_access WHERE admin_email = '$unique_admin_id'";
        $querytogetData = $this->cxn->query($getdata);
        if($querytogetData){
        	$getmydata = mysqli_fetch_assoc($querytogetData);
        	$d_customer_password = $getmydata['admin_password'];
        }
          $customer_old_password = trim($old_password);
          $customer_new_password = trim($new_password);

  
      
  $customer_old_password = $this->psw_test_input($this->cxn, $customer_old_password);
      $customer_new_password = $this->psw_test_input($this->cxn, $customer_new_password);
      $customer_new_unhash_password = $customer_new_password;
  if(password_verify($customer_old_password, $d_customer_password)){
          $option = ['cost'=>12,];
          $customer_new_password = password_hash($customer_new_password, PASSWORD_BCRYPT, $option);

          $chk_data = "UPDATE admin_access SET admin_password = '$customer_new_password', admin_unhash_password = '$customer_new_unhash_password' WHERE admin_email = '$unique_admin_id'";
          $querytoData = $this->cxn->query($chk_data);
          if($querytoData){
             return true;
          }else{
             return false;
          }
      
  }else{
            return false;
          }
      
      }
    public function chk_login_user(){
        if(isset($_SESSION['admingoodloginistrue']) && isset($_SESSION['admin_email'])){
        if($_SESSION['admingoodloginistrue'] != true && $_SESSION['admin_email'] == ''){
           return false;
        }else{
            return true;
    }}else{
        return false;
    }
    }
     public  function select_currency(){
        $select_data = "SELECT currency FROM admin_access";
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        $getnow = $runs->fetch_assoc(); 
        return $getnow['currency'];
    }
    }
   public function change_currency($symbol){
        $select_data = "UPDATE admin_access SET currency = '$symbol' "; 
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        return true;
    }
    }
    public function update_localbanks($banks){
        $banks = trim(trim(implode(',',$banks)),',');
        $select_data = "UPDATE admin_access SET local_banks = '$banks' "; 
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        return true;
    }
    }
    public function get_localbanks(){
        $select_data = "SELECT local_banks FROM admin_access";
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        $getnow = $runs->fetch_assoc(); 
        return $getnow['local_banks'];
    }
    }
        public function get_local_banks(){
        $select_data = "SELECT local_banks FROM admin_access";
        $runs = $this->cxn->query($select_data);
    if(!$runs){return false;}else{
        $getnow = $runs->fetch_assoc(); 
        return explode(',',$getnow['local_banks']);
    }
    }
    public function logout_user(){
        session_unset();
        session_destroy();
    }
}

class bankUser {
    public $cxn;
    public function __construct(){
        if(session_status() === 1) session_start();
        $this->cxn = login_cred();
    }
    public function last_login($acct_number){
        $last_login = ['last_login_date'=>date('D M Y H:i:s'),'user_ip'=>$_SERVER['REMOTE_ADDR']];
        $last_login = implode('--',$last_login);
        $write_cmd = "UPDATE bank_account SET last_login = '$last_login' WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            return true;
        }else{
            return false;
        }
    }

    public function get_login($acct_number){
        $write_cmd = "SELECT last_login FROM bank_account WHERE acct_number = '$acct_number'"; 
        $query_cmd = $this->cxn->query($write_cmd);
        if($query_cmd){
            $last_login = $query_cmd->fetch_assoc()['last_login'];
            $last_login = explode('--',$last_login);
            return $last_login;
        }else{
            return false;
        }
    }
    public function login_user($email,$pass){
        $cxn = $this->cxn;
		$pass = test_input($cxn, $pass);
		$email = trim($email);
		$select_data = "SELECT * FROM bank_account WHERE acct_number = '$email'";
		$runs = $cxn->query($select_data);
		if(!$runs){return false;}
		else {
            if($runs->num_rows == 1){
        $getnow = $runs->fetch_assoc(); 
		if(password_verify($pass, $getnow['acct_password'])){

			$_SESSION['usergoodloginistrue'] = true;
			$_SESSION['acct_number'] = $getnow['acct_number'];
			$_SESSION['acct_email'] = $getnow['acct_email'];
			$_SESSION['otpistrue'] = $getnow['otpistrue'];
			if($getnow['otpistrue'] == 'true'){
    // 		if($this->send_otp($getnow['acct_number'],$getnow['acct_email'])){
    		    return true;
    // 		}
            }else{
                return true;
            }
        		}else{
                    return false;
                }
            }else{
                return false;
            }
	}
    }
   public function send_otp($acct_number,$acct_email){
       $acct_otp = rand(000000, 1000000);
       $write_cmd = "UPDATE bank_account SET acct_otp = '$acct_otp' WHERE acct_number = '$acct_number'";
            $query_cmd = $this->cxn->query($write_cmd);
            if($query_cmd){
            $_SESSION['isOtpVerified'] = false;
		    $Hmailer = new Hmailer($acct_email);
			$Hmailer->OTP_alert($acct_otp, $acct_number);
			
			return true;
		   
                
            }else{
                return false;
            }
   }

      public function verify_otp($acct_otp, $acct_number){
    		$select_data = "SELECT * FROM bank_account WHERE acct_number = '$acct_number'";
		$runs = $this->cxn->query($select_data);
		if($runs){
		   if($runs->num_rows == 1){
        $getnow = $runs->fetch_assoc();
        if($getnow['transfer_code'] == $acct_otp){
            $_SESSION['isOtpVerified'] = true;
            return true;
        }
            }
		}
       return false;
            
   }
   public function chk_login_user(){
        if(isset($_SESSION['usergoodloginistrue']) && isset($_SESSION['acct_number'])){
        if($_SESSION['usergoodloginistrue'] != true && $_SESSION['acct_number'] == ''){
           return false;
        }else{
            return true;
    }}else{
        return false;
    }
    }
    public function isOtpVerified(){
          if($this->chk_login_user()){
              $acct_number = $_SESSION['acct_number'];
              	$select_data = "SELECT otpistrue FROM bank_account WHERE acct_number = '$acct_number'";
        		$runs = $this->cxn->query($select_data);
        		if($runs){
        		    
		   if($runs->num_rows == 1){
        $getnow = $runs->fetch_assoc();
        if($getnow['otpistrue'] == 'false'){
            return true;
        }
            }
		}
          }
        if(isset($_SESSION['isOtpVerified'])){
            if($_SESSION['isOtpVerified'] == true){
               return true;
            }else{
                return false;
            }   
        }else{
            return false;
        }
       }
    public function change_user_pwd($acct_number,$user_old_password, $pwd_hash, $user_new_password){
        if(password_verify($user_old_password, $pwd_hash)){
            $option = ['cost'=>12,];
            $user_new_password_hash = password_hash($user_new_password, PASSWORD_BCRYPT, $option);
            $chk_data = "UPDATE bank_account SET acct_password = '$user_new_password_hash',acct_password_unhash = '$user_new_password' WHERE acct_number = '$acct_number'";
            $querytoData = $this->cxn->query($chk_data);
            if($querytoData){
                return true;
            }else{
                return false;
            }
        }
    }
        public function change_user_pin($acct_number,$user_old_password, $pwd_hash, $user_new_password){
        if($user_old_password == $pwd_hash){
            $chk_data = "UPDATE bank_account SET transfer_code = '$user_new_password' WHERE acct_number = '$acct_number'";
            $querytoData = $this->cxn->query($chk_data);
            if($querytoData){
                return true;
            }else{
                return false;
            }
        }
    }
            public function toggle_2fa($acct_number,$status){
            $chk_data = "UPDATE bank_account SET otpistrue = '$status' WHERE acct_number = '$acct_number'";
            $querytoData = $this->cxn->query($chk_data);
            if($querytoData){
                return true;
            }else{
                return false;
        }
    }
    public function logout_user(){
        $this->last_login($_SESSION['acct_number']);
        session_unset();
        session_destroy();
    }
}
