<?php
session_start();
include_once ('../include/session.php');
require_once '../include/class.user.php';

if (!isset($_SESSION['acc_no'])) {

    header("Location: login");
    exit();
}




$reg_user = new USER();
$stmt = $reg_user->runQuery("SELECT * FROM account WHERE acc_no=:acc_no");
$stmt->execute(array(":acc_no" => $_SESSION['acc_no']));
$row = $stmt->fetch(PDO::FETCH_ASSOC);

$email = $row['email'];

$temp = $reg_user->runQuery("SELECT * FROM temp_transfer WHERE email = '$email' ORDER BY id DESC LIMIT 1");
$temp->execute();
$rows = $temp->fetch(PDO::FETCH_ASSOC);
$tempa = $reg_user->runQuery("SELECT * FROM transfer WHERE email = '$email' ORDER BY id DESC LIMIT 1");
$tempa->execute();
$rowc = $tempa->fetch(PDO::FETCH_ASSOC);

$ego = $reg_user->runQuery("SELECT t_bal FROM account WHERE acc_no=:acc_no");
$stmt1 = $reg_user->runQuery("SELECT a_bal FROM account WHERE acc_no=:acc_no");
$ego->execute(array(":acc_no" => $_SESSION['acc_no']));
$stmt1->execute(array(":acc_no" => $_SESSION['acc_no']));
$owo = $ego->fetch(PDO::FETCH_ASSOC);
$row1 = $stmt1->fetch(PDO::FETCH_ASSOC);

$bal = $row['t_bal'];
$baa = $row['a_bal'];
$amount = $rows['amount'];

if (isset($_POST['code'])) {
    $email = $row['email'];
    $amount = $_POST['amount'];
    $acc_no = $_POST['acc_no'];
    $acc_name = $_POST['acc_name'];
    $bank_name = $_POST['bank_name'];
    $swift = $_POST['swift'];
    $routing = $_POST['routing'];
    $type = $_POST['type'];
    $remarks = $_POST['remarks'];
    $bal = $row['t_bal'];
    $baa = $row['a_bal'];
$cout = $_POST['cout'];
$transtype = $_POST['transtype'];


    $tmp_otp = $row['tmp_otp'];
    $sub = $_POST['tmp_otp'];


    if ($sub !== $tmp_otp) {
        header("Location: dom?errorotp");
        exit();
    } elseif ($bal < $amount && $baa < $amount) {
        $bal = $row['t_bal'];
        $baa = $row['a_bal'];
        $amount = $rows['amount'];
        header("Location: dom?insufficient");
        exit();
    } else {
        if ($reg_user->transfer($email, $amount, $acc_no, $acc_name, $bank_name, $swift, $routing, $type, $remarks, $cout, $transtype)) {
            $email = $row['email'];
            $fname = $row['fname'];
            $mname = $row['mname'];
            $lname = $row['lname'];
            $currency = $row['currency'];
            $amount = $rows['amount'];
            $acc_no = $row['acc_no'];
            $phone = $row['phone'];
            $acc_name = $_POST['acc_name'];
            $bank_name = $_POST['bank_name'];
            $swift = $_POST['swift'];
            $routing = $_POST['routing'];
            $type = $_POST['type'];
            $date = $rowc['date'];
            $remarks = $_POST['remarks'];
            $cout = $_POST['cout'];
            $transtype = $_POST['transtype'];


            $bal = $row['t_bal'];
            $baa = $row['a_bal'];
            $total = $bal - $amount;
            $avail = $baa - $amount;

            $updatebal = $reg_user->runQuery("UPDATE account SET t_bal = '$total', a_bal = '$avail' WHERE email = '$email'");
            $updatebal->execute();




            $messag = " <div class=''>
  <div class='aHl'></div>
  <div id=':190' tabindex='-1'></div>
  <div id=':18p' class='ii gt' jslog='20277; u014N:xr6bB; 4:W251bGwsbnVsbCxbXV0.'>
    <div id=':18o' class='a3s aiL msg3853875273355017072'>
      <u></u>
      <div style='width: 100% !important; margin: 0; padding: 0; background-color: #e8f8ff;'>
        <span style='color: transparent; display: none; height: 0; max-height: 0; max-width: 0; opacity: 0; overflow: hidden; width: 0;'> </span>
        <table border='0' cellpadding='0' cellspacing='0' role='presentation' style='table-layout: fixed;' width='100%'>
          <tbody>
            <tr>
              <td align='center' valign='top'>
                <table align='center' border='0' cellpadding='0' cellspacing='0' class='m_3853875273355017072pc-email-container' role='presentation' style='margin: 0 auto; max-width: 620px;' width='100%'>
                  <tbody>
                    <tr>
                      <td align='left' style='padding: 0 10px;' valign='top'>
                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                          <tbody>
                            <tr>
                              <td height='20' style='font-size: 1px; line-height: 1px;'>&nbsp;</td>
                            </tr>
                          </tbody>
                        </table>
                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                          <tbody>
                            <tr>
                              <td valign='top'>
                                <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                  <tbody>
                                    <tr>
                                      <td bgcolor='#E8F8FF' class='m_3853875273355017072pc-sm-p-30 m_3853875273355017072pc-xs-p-20' style='padding: 29px 35px; background-color: #ffffff; text-align: right; font-size: 0;' valign='top'>
                                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                          <tbody>
                                            <tr>
                                              <td align='center' valign='top'>
                                                  <img alt='' height='32' src='https://skyutahcapitals.com/sitelogo.png' style='max-width: 100%; height: auto; border: 0; line-height: 100%; outline: 0; font-size: 14px; color: #1b1b1b;' width='130' class='CToWUd' data-bit='iit'>
                                              </td>
                                            </tr>
                                          </tbody>
                                        </table>
                                      </td>
                                    </tr>
                                  </tbody>
                                </table>
                                <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                  <tbody>
                                    <tr>
                                      <td bgcolor='#E8F8FF' class='m_3853875273355017072pc-xs-p-15-10-20 m_3853875273355017072pc-sm-p-20-20-25' style='padding: 20px 30px 35px; background-color: #ffffff;' valign='top'>
                                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                          <tbody>
                                            <tr>
                                              <td height='0' style='font-size: 1px; line-height: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              <td style='padding: 0 10px;' valign='top'>
                                                  <img alt='' height='' src='https://ba.rclays.co/dam/images/index/Index_Hero-Banner.png' style='border: 0; line-height: 100%; outline: 0; border-radius: 6px; max-width: 100%; color: #1b1b1b; display: block; height: auto;' width='520' class='CToWUd' data-bit='iit'>
                                              </td>
                                            </tr>
                                            <tr>
                                              <td height='30' style='line-height: 1px; font-size: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              <td height='12' style='line-height: 1px; font-size: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              
                                            </tr>
                                            <tr>
                                              <td height='25' style='line-height: 1px; font-size: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              <td style='padding: 0 10px; font-family: 'Averta', 'Fira Sans', Helvetica, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 28px; letter-spacing: -0.2px; color: #00425f;' valign='top'>
                                                <p>Hello $fname,</p>
                                                <p>Your transfer was successful! Below is the transaction summary.</p>

                                                <h3>Transaction Details</h3>
                                     <table style='border:1px solid black;padding:2px;' width='400'>
                                        <tr>
                                            <th style='text-align:left;'>Credit/Debit</th>
                                            <td>Debit</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Account Number</th>
                                            <td>$acc_no</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Account Name</th>
                                            <td>$acc_name</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Date/Time</th>
                                            <td>$date</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Description</th>
                                            <td>$remarks</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Amount</th>
                                            <td>$currency $amount</td>
                                        </tr>
                                        <tr>
                                            <th style='text-align:left;'>Balance</th>
                                            <td>$currency $bal</td>
                                        </tr>
                                        
                                        <tr style='background-color:#00aeef;'>
                                            <th style='text-align:left; color:#fff;'>Available Balance</th>
                                            <td style='color:#fff;'>$currency $avail</td>
                                        </tr>
                                     </table>
                                                
                                                
                                                
                                              </td>
                                            </tr>
                                            <tr>
                                              <td height='15' style='line-height: 1px; font-size: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              <td height='20' style='line-height: 1px; font-size: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                        </table>
                                        
                                        
                                      </td>
                                    </tr>
                                  </tbody>
                                </table>
                                <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                  <tbody>
                                    <tr>
                                      <td bgcolor='#00425F' style='background-color: #00425f;' valign='top'>
                                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                          <tbody>
                                            <tr>
                                              
                                            </tr>
                                          </tbody>
                                        </table>
                                      </td>
                                    </tr>
                                  </tbody>
                                </table>
                                <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                  <tbody>
                                    <tr>
                                      <td bgcolor='#FFFFFF' class='m_3853875273355017072pc-sm-p-38-30-40 m_3853875273355017072pc-xs-p-25-20' style='padding: 38px 40px 40px 40px; background-color: #ffffff;' valign='top'>
                                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                                          <tbody>
                                            <tr>
                                              <td class='m_3853875273355017072pc-xs-fs-14' style='text-align: center; font-family: Arial, sans-serif; font-size: 24px;' valign='top'>
                                                  <img alt='' height='21' src='http://img.nc.quickteller.com/images/quicktellersmart/quicktellersmart_568/5_53b0d85e-c406-4bcd-92cf-046e6ae77be7.png' style='border: 0; line-height: 100%; outline: 0; font-size: 14px; color: #151515;' width='21' class='CToWUd' data-bit='iit'>
                                                <span>&nbsp;&nbsp;&nbsp;</span>
                                                  <img alt='' height='21' src='http://img.nc.quickteller.com/images/quicktellersmart/quicktellersmart_568/6_1c4aee3c-ad24-4a2e-b4f2-b5aad2b734e6.png' style='border: 0; line-height: 100%; outline: 0; font-size: 14px; color: #151515;' width='21' class='CToWUd' data-bit='iit'>
                                                <span>&nbsp;&nbsp;&nbsp;</span>
                                                  <img alt='' height='21' src='http://img.nc.quickteller.com/images/quicktellersmart/quicktellersmart_568/7_5463fd6e-a546-4c41-8f1f-5110c75f53db.png' style='border: 0; line-height: 100%; outline: 0; font-size: 14px; color: #151515;' width='21' class='CToWUd' data-bit='iit'>
                                              </td>
                                            </tr>
                                            <tr>
                                              <td height='15' style='font-size: 1px; line-height: 1px;' valign='top'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          <tbody>
                                            <tr>
                                              <td style='text-align: center; line-height: 20px; letter-spacing: -0.2px; font-family: 'Averta', 'Fira Sans', Helvetica, Arial, sans-serif; font-size: 14px; color: #9b9b9b;' valign='top'>
                                                Philadelphia, PA 19176
                                                <p style='#F00; font-size:13px;line-height: 15px;'>This message is sent to this email to $fname <br> <br> </p><center><b>How do I know this is not a fake email?</b></center> <br>

An email really coming from us will address you by your registered first and last name or your business name. It will not ask you for sensitive information like your password, bank account or credit card details.<br><br>
                                      <p></p>
                                      <p style='#F00; font-size:13px;line-height: 15px;'>Remember not to click any links in suspicious looking emails. </p>
                                              </td>
                                            </tr>
                                            <tr>
                                              <td height='13' style='font-size: 1px; line-height: 1px;'>&nbsp;</td>
                                            </tr>
                                          </tbody>
                                          
                                        </table>
                                      </td>
                                    </tr>
                                  </tbody>
                                </table>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                        <table border='0' cellpadding='0' cellspacing='0' role='presentation' width='100%'>
                          <tbody>
                            <tr>
                              <td height='20' style='font-size: 1px; line-height: 1px;'>&nbsp;</td>
                            </tr>
                          </tbody>
                        </table>
                      </td>
                    </tr>
                  </tbody>
                </table>
              </td>
            </tr>
          </tbody>
        </table>
        <br>
        <div></div>
      </div>
    </div>
  </div>
</div> ";


            $subject = "[Debit Alert] - Skyutah Capitals";

            $reg_user->send_mail($email, $messag, $subject);

            $phonee = preg_replace('/[^0-9]/', '', $row['phone']);

            $acc_last_four = substr($row['acc_no'], -4);
            $amoun_sms = $currency . " " . $amount;
            $balance_sms = $currency . " " . $bal;
            $remark_sms = substr($remarks, 0, 15);

            $mobile_msg = "Transfer completed, Acct: **".$acc_last_four.",  Amt: " .$amoun_sms. ", Desc:".$remark_sms. ", Date:".$date.", Avail Bal:".$balance_sms;
            

            header("Location: fundsuccess");
            exit();
        }
    }
}
include_once ('counter.php');
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Transfer Processing - Skyutah Capitals</title>
            
            <?php include 'header.php';?>

             <main>
                    <div class="vd jd tto vv oq ar ri">
                        <!-- Page header -->
                        <div class="ry">
                            <!-- Title -->
                            <h1 class="gg zj text-slate-800 font-bold">Authenticating Transfer</h1>
                        </div>

                        <div class="bg-white by rounded-sm ry">
                            <div class="flex fh zt qo">
                                <!-- Panel -->
                                <div class="ad">
                                    <!-- Panel body -->
                                    <div class="vs fz">

                                        <!-- Rate -->
                                        <section>
                                            <h4 class="gy yt text-slate-800 font-bold rb">Inter-Bank OTP Authentication</h4>
                                            <div class="oq af">
                                                <div class="td">
                                                    <div class="tp tb tx iz oq om hw" aria-hidden="true"></div>
                                                    <ul class="td flex fg oq">
                                                        <li class="flex">
                                                            <button class="w-3 h-3 rounded-full bg-white cy hc">
                                                                <span class="tc">1</span>
                                                            </button>
                                                        </li>
                                                        <li class="flex">
                                                            <button class="w-3 h-3 rounded-full hb cy hf">
                                                                <span class="tc">2</span>
                                                            </button>
                                                        </li>
                                                        <li class="flex">
                                                            <button class="w-3 h-3 rounded-full bg-white cy hc">
                                                                <span class="tc">3</span>
                                                            </button>
                                                        </li>
                                                    </ul>
                                                </div>
                                                <div class="oq flex fg text-sm text-slate-500 gz im">
                                                    <div>...</div>
                                                    <div>Enter OTP</div>
                                                    <div>Done</div>
                                                </div>
                                            </div>
                                        </section>

                                        <div>
                                            <div class="text-sm">Hello, <?php echo $row['fname']; ?> Please Enter SMS/EMAIL OTP Code sent to you!   OTP CODE is required to complete The Transfer.</div>
                                        </div>

                                        <!-- Tell us in words -->
                                        <section>
                                            <div>
                                            <div class="flex items-center fg">
                                                <label class="block text-sm gk rx" for="tooltip">Enter One Time Password</label>
                                                <div class="td r_" x-data="{ open: false }" @mouseenter="open = true" @mouseleave="open = false">
                                                    <button class="block" aria-haspopup="true" :aria-expanded="open" @focus="open = true" @focusout="open = false" @click.prevent="" aria-expanded="false">
                                                        <svg class="ue on db yu" viewBox="0 0 16 16">
                                                            <path d="M8 0C3.6 0 0 3.6 0 8s3.6 8 8 8 8-3.6 8-8-3.6-8-8-8zm0 12c-.6 0-1-.4-1-1s.4-1 1-1 1 .4 1 1-.4 1-1 1zm1-3H7V4h2v5z"></path>
                                                        </svg>
                                                    </button>
                                                    <div class="nv tp tz ne fe ag">
                                                        <div class="uz pa ya v_ mt rounded lq in" x-show="open" x-transition:enter="wr wh wf fe" x-transition:enter-start="opacity-0 ak" x-transition:enter-end="bv ax" x-transition:leave="wr wh wf" x-transition:leave-start="bv" x-transition:leave-end="opacity-0" style="display: none;">
                                                            <div class="text-sm">OTP is Required!.</div>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>
                                            <form autocomplete="off" method="post" >
                                            <input id="tooltip" class="tn oq" type="text" name="tmp_otp">
                                            <input type="hidden" name="email" value="<?php echo $row['email']; ?>">
                                            <input type="hidden" name="amount" value="<?php echo $rows['amount']; ?>">
                                            <input type="hidden" name="acc_no" value="<?php echo $rows['acc_no']; ?>">
                                            <input type="hidden" name="acc_name" value="<?php echo $rows['acc_name']; ?>">
                                            <input type="hidden" name="bank_name" value="<?php echo $rows['bank_name']; ?>">
                                            <input type="hidden" name="swift" value="<?php echo $rows['swift']; ?>">
                                            <input type="hidden" name="cout" value="<?php echo $rows['cout']; ?>">
                                            <input type="hidden" name="transtype" value="<?php echo $rows['transtype']; ?>">
                                            <input type="hidden" name="routing" value="<?php echo $rows['routing']; ?>">
                                            <input type="hidden" name="type" value="<?php echo $rows['type']; ?>">
                                            <input type="hidden" name="remarks" value="<?php echo $rows['remarks']; ?>">
                                        </div>
                                        </section>
                                    </div>

                                    <!-- Panel footer -->
                                    <footer>
                                        <div class="flex fh vw vx ck border-slate-200">
                                            <div class="flex lk">
                                                <button class="btn hb xs yo ml-3" name="code">Confirm Transfer</button>
                                            </div>
                                        </div>
                                    </footer>
                                </div>
                            </div>
                        </div>
                    </div>
                </main>

            <?php include 'footer.php';?>