<?php
session_start();
require_once('config/config.php');
require_once('class/functions.php');
dbconnect();

if (!is_user()) {
    redirect(LOGIN);
}

$page = "Pending Transfer Confirmation";
$geo = ip2location($_SERVER['REMOTE_ADDR']);
$country = $geo->country;
$city = $geo->city;
visit_log($_SERVER['REMOTE_ADDR'], $country, $city, $_SERVER['HTTP_USER_AGENT'], getBrowser($_SERVER['HTTP_USER_AGENT']), getOS($_SERVER['HTTP_USER_AGENT']), $page, $_SESSION['user_login']);



//if (isset($_POST['transfer']) && $_POST['trasfer'] == 'transfer')
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['transfer']))
{
if (!acc_active($_POST['fmAcc'], 'active')){ store_msg("Transfer account is inactive", "danger"); redirect(PENDINGTRANSFER);}
elseif (!sufficient_balance($_POST['fmAcc'], $_POST['tfAmt'])){ store_msg("Insufficient Balance", "danger"); redirect(PENDINGTRANSFER);}
else{
$transfer = transf_details($_POST['trnsNo'], $_SESSION['user_number']);
foreach($transfer as $trans) {
$amount = $trans['atmpt_amt'];
$fromacc = $trans['acc_no'];
$swift = $trans['rcv_code'];
$memo = $trans['purpose'];
$benacc = $trans['rcv_acc'];
$benname = $trans['rcv_name'];
$benbnk = $trans['rcv_bk'];
$bencntry = $trans['rcv_country'];
}
}
}

if (isset($_POST['confirm']) && $_POST['confirm'] == 'confirm')
{

$fromacc = clean_data($_POST['fmAcc']);
$swift = clean_data($_POST['uSwft']);
$memo = clean_data($_POST['uTmem']);

$benacc = clean_data($_POST['toAcc']);
$benname = clean_data($_POST['benName']);
$benbnk =  clean_data($_POST['benBnk']);
$bencntry = clean_data($_POST['benCnty']);

$amount = clean_data($_POST['tfAmt']);
$usernumber = $_SESSION['user_number'];
$usernames = user_names($_SESSION['user_login']);
$transnumber = trans_code();
$transdate = date("l jS \of F Y h:i A");

$stmt = $pdo->prepare('INSERT INTO '.TRANF_TABLE_NAME.' (trans_number, user_number, acc_no, acc_name, atmpt_date, rcv_bk, rcv_country, rcv_name, rcv_acc, rcv_code, purpose, atmpt_amt) VALUES (:transnumber, :usernumber, :accno, :accname, NOW(), :rcvbk, :rcvcntry, :rname, :racc, :code, :purpose, :amount)');

$stmt->execute(array(':transnumber' => $transnumber, ':usernumber' => $usernumber, ':accno' => $fromacc, ':accname' => $usernames, ':rcvbk' => $benbnk, ':rcvcntry' => $bencntry, ':rname' => $benname, ':racc' => $benacc, ':code' => $swift, ':purpose' => $memo, ':amount' => $amount));
if ($pdo->lastInsertId()){

    //send Email
    $message = "
    <html>
    <head>
    <title>".WEB_NAME."</title>
    </head>
    <body>
    <img src=".LOGO_URL." alt=".WEB_NAME."/><br><br>
    <p>ONLINE FUNDS TRANSFER HAS BEEN ATTEMPTED WITH THESE DETAILS:<br><br>
    DATE: ".$transdate." <br>
    SOURCE ACCOUNT: ".$fromacc." <br>
    BENEFICIARY ACCOUNT NUMBER: ".$benacc." <br>
    BENEFICIARY ACCOUNT NAME: ".$benname." <br>
    BENEFICIARY BANK: ".$benbnk." <br>
    BENEFICIARY BANK COUNTRY: ".$bencntry." <br>
    ".SWFT_CODE.": ".$swift." <br>
    TRANSFER MEMO (PURPOSE): ".$memo." <br>
    AMOUNT ".get_currency($fromacc)."".$amount."
    </p><br><br><br>
    ===========
    <p>".WEB_NAME."</p>
    </body>
    </html>
    ";
    $subject = "ONLINE FUNDS TRANSFER ATTEMPT";
    //send_email($fromname, $fromemail, $toemail, $subject, $message)
    send_email(WEB_NAME, WEB_EMAIL, WEB_EMAIL, $subject, $message);
    //Set Transnumber as session
    $_SESSION['trans_code'] = $transnumber;
    $_SESSION['trans_acc'] = $fromacc;
    log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $benname, 'Successfully Added Transfer Attempt');
    if (CODE1_TRUE){redirect(CODE1);}else{redirect(OTHEROTP);}
}
}

require_once('head.php');
?>

<body>
  <div class="app" id="app">

<!-- ############ LAYOUT START-->
  <!-- aside -->
  <?php require_once('aside.php'); ?>
  <!-- / aside -->



  <!-- content -->
  <div id="content" class="app-content box-shadow-z3" role="main">

    <?php require_once('header.php'); ?>

    <?php require_once('footer.php'); ?>

    <div ui-view class="app-body" id="view">
    <!-- ############ PAGE START-->

    <?php require_once('topbar.php'); ?>


    <?php
    $notify = get_alerts($_SESSION['user_login'], 'all');
    foreach($notify as $ntfy){
    echo "<div class=\"alert alert-warning\" role=\"alert\">
    <strong>".date('l jS F Y', strtotime($ntfy['alrt_date']))."</strong><br>".$ntfy['alrt_cnt']."
    </div>";
    }
    ?>

    <?php if (!empty($_SESSION['msg_text'])) {
    $msg = create_alert($_SESSION['msg_text'], $_SESSION['msg_type']);
    echo $msg;
    clear_msg(); }
    ?>


    <div class="padding">
      <div class="p-x-lg">
        <div class="row">
        <div class="col-lg-8 offset-lg-2">
          <div class="box">
            <div class="box-header">
              <h2>Confirm Transfer</h2>
              <small>Review the details of your transfer.</small>
            </div>
            <div class="box-divider m-a-0"></div>
            <div class="box-body p-v-md">
            <form id="ConPen" method="post" action="" ui-jp="parsley">
              <div class="p-a">
                <div class="streamline b-l m-b">
                    <div class="sl-item b-muted">
                        <div class="sl-content">
                            <p>YOU HAVE INITIATED A TRANSFER TO OTHER BANK:</p>
                        </div>
                    </div>
                    <div class="sl-item b-info">
                        <div class="sl-icon">
                            <i class="material-icons text-info">&#xe0b2;</i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">SOURCE ACCOUNT NUMBER:</div>
                            <p><?php echo $fromacc; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-success">
                        <div class="sl-icon">
                            <i class="material-icons text-info">&#xe0b5;</i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY ACCOUNT NUMBER:</div>
                            <p><?php echo $benacc; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-success">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY ACCOUNT NAME:</div>
                            <p><?php echo $benname; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY BANK:</div>
                            <p><?php echo $benbnk; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">BENEFICIARY BANK COUNTRY:</div>
                            <p><?php echo $bencntry; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-info">
                        <div class="sl-content">
                            <div class="sl-date text-muted"><?php echo strtoupper(SWFT_CODE); ?>:</div>
                            <p><?php echo $swift; ?></p>
                        </div>
                    </div>
                    <div class="sl-item">
                        <div class="sl-content">
                            <div class="sl-date text-muted">TRANSFER MEMO (PURPOSE):</div>
                            <p><?php echo $memo; ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-success">
                        <div class="sl-icon">
                            <i class="material-icons text-info">&#xe877;</i>
                        </div>
                        <div class="sl-content">
                            <div class="sl-date text-muted">AMOUNT:</div>
                            <p class="text-success"><?php echo get_currency($fromacc); ?><strong><?php echo number_format($amount,2); ?></p>
                        </div>
                    </div>
                    <div class="sl-item b-muted">
                        <div class="sl-content">
                            <p class="text-muted">Click cancel below to cancel transfer or transfer to complete transfer</p>
                        </div>
                    </div>
                </div>
              </div>

              <div class="row row-sm">
                <div class="col-sm-12">
                    <input name="fmAcc" type="hidden" value="<?php echo $fromacc; ?>">
                    <input name="toAcc" type="hidden" value="<?php echo $benacc; ?>">
                    <input name="benName" type="hidden" value="<?php echo $benname; ?>">
                    <input name="benBnk" type="hidden" value="<?php echo $benbnk; ?>">
                    <input name="benCnty" type="hidden" value="<?php echo $bencntry; ?>">
                    <input name="uSwft" type="hidden" value="<?php echo $swift; ?>">
                    <input name="uTmem" type="hidden" value="<?php echo $memo; ?>">
                    <input name="tfAmt" type="hidden" value="<?php echo $amount; ?>">
                    <button type="submit" name="confirm" value="confirm" class="btn rounded btn-outline main mainbd p-x-md">Transfer</button>
                    <a href="<?php echo PENDINGTRANSFER ?>" class="btn rounded btn-outline b-danger text-danger p-x-md">Cancel</a>
                </div>
              </div>
            </form>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>


    <!-- ############ PAGE END-->
    </div>



    </div>
<!-- ############ LAYOUT END-->

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="libs/jquery/jquery/dist/jquery.js"></script>

<!-- Bootstrap -->
  <script src="libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></script>
  <script src="scripts/jquery.maskMoney.js"></script>
  <script>
    $(function() {
        $("#tfAmt").maskMoney();
    });
  </script>
<!-- core -->
  <script src="libs/jquery/underscore/underscore-min.js"></script>
  <script src="libs/jquery/jQuery-Storage-API/jquery.storageapi.min.js"></script>
  <script src="libs/jquery/PACE/pace.min.js"></script>

  <script src="scripts/config.lazyload.js"></script>

  <script src="scripts/palette.js"></script>
  <script src="scripts/ui-load.js"></script>
  <script src="scripts/ui-jp.js"></script>
  <script src="scripts/ui-include.js"></script>
  <script src="scripts/ui-device.js"></script>
  <script src="scripts/ui-form.js"></script>
  <script src="scripts/ui-nav.js"></script>
  <script src="scripts/ui-screenfull.js"></script>
  <script src="scripts/ui-scroll-to.js"></script>
  <script src="scripts/ui-toggle-class.js"></script>

  <script src="scripts/app.js"></script>

  <!-- ajax -->
  <script src="libs/jquery/jquery-pjax/jquery.pjax.js"></script>
  <script src="scripts/ajax.js"></script>
<!-- endbuild -->
</body>
</html>
