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

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

$page = "Transfer to other Bank";
$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['addBen']) && $_POST['addBen'] == 'addBen')
{
if ($_POST['accNo'] == "") { store_msg("Beneficiary's Account Number is required", "danger"); redirect(OTHERTRANSFER);}
elseif ($_POST['benName'] == "") { store_msg("Beneficiary's Account Name is required", "danger"); redirect(OTHERTRANSFER);}
elseif ($_POST['benBnk'] == "") { store_msg("Beneficiary's Bank is required", "danger"); redirect(OTHERTRANSFER);}
elseif ($_POST['benCntry'] == "") { store_msg("Beneficiary's Bank Country is required", "danger"); redirect(OTHERTRANSFER);}
elseif ($_POST['benSeca'] == "") { store_msg("Enter Answer to Security Question", "danger"); redirect(OTHERTRANSFER);}
elseif (!security_answer($_SESSION['user_login'], $_POST['benSeca'])){ store_msg("Incorrect answer to security question", "danger"); redirect(OTHERTRANSFER);}
else{
$benacc = clean_data($_POST['accNo']);
$benname = clean_data($_POST['benName']);
$benbank = clean_data($_POST['benBnk']);
$bencountry = clean_data($_POST['benCntry']);
$usernumber = $_SESSION['user_number'];

$stmt = $pdo->prepare('INSERT INTO '.BEN_TABLE_NAME.' (user_number, ben_bnk, ben_country, ben_name, ben_acc, ben_added) VALUES (:usernumber, :benbnk, :country, :benname, :benacc, NOW())');

$stmt->execute(array(':usernumber' => $usernumber, ':benbnk' => $benbank, ':country' => $bencountry, ':benname' => $benname, ':benacc' => $benacc));
if ($pdo->lastInsertId()) {
    log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $benname, 'Successfully Added a Transfer Beneficiary');
    store_msg("Beneficiary Added Successfully", "success"); redirect(OTHERTRANSFER);
}else{
    store_msg("Add Beneficiary Error", "danger"); redirect(OTHERTRANSFER);
}
}
}

if (isset($_POST['delBen']) && $_POST['delBen'])
{
$benid = clean_data($_POST['delBen']);
$benname = clean_data($_POST['benName']);
$usernumber = $_SESSION['user_number'];

$stmt = $pdo->prepare('DELETE FROM '.BEN_TABLE_NAME.' WHERE ben_id = :benid AND user_number = :usernumber LIMIT 1');
if ($stmt->execute(array(':benid' => $benid, ':usernumber' => $usernumber))) {
    log_info($_SERVER['REMOTE_ADDR'], $_SESSION['user_login'], $benname, 'Successfully Deleted a Transfer Beneficiary');
    store_msg("Beneficiary Deleted Successfully", "success"); redirect(OTHERTRANSFER);
}else{
    store_msg("Delete Beneficiary Error", "danger"); redirect(OTHERTRANSFER);
}
}

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>Transfer to other Banks</h2>
              <small>Transfer to another account in another bank.</small>
            </div>
            <div class="box-divider m-a-0"></div>
            <div class="box-body p-v-md">
            <form id="Stmnt" method="post" action="<?php echo OTHERCONFIRM ?>" autocomplete="off" enctype="multipart/form-data" ui-jp="parsley">
              <div class="row row-sm">
                <div class="col-sm-12">
                    <div class="md-form-group">
                      <select name="fmAcc" class="md-input" data-parsley-error-message="Select From Account" required>
                        <option value="">Select Account to transfer from </option>
                        <?php
            				$accounts = get_accs($_SESSION['user_number']);
                            if (empty($accounts)) {
                            echo '<option value="">No account exist</option>';
                            }else{
            				foreach($accounts as $acc)
            				{
            				echo '
                            <option value="'.$acc['acc_no'].'">'.$acc['acc_no'].' (Balance: '.$acc['currency'].''.number_format(get_balance($acc['acc_no']),2).')</option>
            				';
            				}
                            }
            			?>
        			  </select>
                      <label>Select From Account</label>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-7">
                    <div class="md-form-group">
                      <select name="toAcc" class="md-input" data-parsley-error-message="Select Trasfer Beneficiary" required>
                        <option value="">Select beneficiary Account</option>
                        <?php
            				$beneficiary = get_beneficiary($_SESSION['user_number']);
                            if (empty($beneficiary)) {
                            echo '<option value="">No beneficiary added yet</option>';
                            }else{
            				foreach($beneficiary as $ben)
            				{
            				echo '
                            <option value="'.$ben['ben_id'].'">'.$ben['ben_name'].' ('.$ben['ben_acc'].') - '.$ben['ben_bnk'].' '.$ben['ben_country'].'</option>
            				';
            				}
                            }
            			?>
        			  </select>
                      <label>Select Beneficiary Account</label>
                    </div>
                </div>
                <div class="col-sm-5">
                    <div class="md-form-group">
                      Beneficiary: <a href="#" class="nav-link label success" data-toggle="modal" data-target="#addben">Add</a>
                      <a href="#" class="nav-link label danger" data-toggle="modal" data-target="#manageben">Manage</a>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="text" name="uSwft" class="md-input" data-parsley-error-message="Enter <?php echo SWFT_CODE ?>" required>
                      <label><?php echo SWFT_CODE ?></label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="text" name="uTmem" class="md-input" data-parsley-error-message="Enter Transfer Memo or Purpose" required>
                      <label>Transfer Memo(Purpose)</label>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="text" id="Amnt" name="tfAmt" class="md-input" data-parsley-error-message="Enter Transfer Amount" required>
                      <label>Amount</label>
                    </div>
                </div>
                <div class="col-sm-6">
                    <div class="md-form-group float-label">
                      <input type="password" name="uSeca" class="md-input" data-parsley-error-message="Enter Answer to Security Question" required>
                      <label>Answer to Security Question</label>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-12">
                  <button type="submit" name="trasfer" value="trasfer" class="btn rounded btn-outline main mainbd p-x-md">Transfer</button>
                </div>
              </div>
            </form>
            </div>
          </div>
        </div>
        </div>
      </div>
    </div>


    <!-- Add Beneficiary modal -->
    <div id="addben" class="modal" data-backdrop="true">
      <div class="modal-dialog">
      <form id="addBen" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
        <div class="modal-content">
          <div class="modal-header">
          	<h5 class="modal-title">Add New Beneficiary</h5>
          </div>
          <div class="modal-body p-lg">
            <div class="row row-sm">
              <div class="col-sm-6">
                <div class="md-form-group float-label">
                  <input type="text" name="accNo" class="md-input" data-parsley-error-message="Beneficiary's Account Number is required" required>
                  <label>Beneficiary's Account Number</label>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="md-form-group float-label">
                  <input type="text" name="benName" class="md-input" data-parsley-error-message="Beneficiary's Account Name is required" required>
                  <label>Beneficiary's Account Name</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
              <div class="col-sm-12">
                <div class="md-form-group float-label">
                  <input type="text" name="benBnk" class="md-input" data-parsley-error-message="Beneficiary's Bank is required" required>
                  <label>Beneficiary's Bank</label>
                </div>
              </div>
            </div>
            <div class="row row-sm">
              <div class="col-sm-6">
                <div class="md-form-group float-label">
                  <input type="tel" id="country" name="benCntry" class="md-input" placeholder="Country" data-parsley-error-message="Beneficiary's Bank Country is required" required>
                </div>
              </div>
              <div class="col-sm-6">
                <div class="md-form-group float-label">
                  <input type="password" name="benSeca" class="md-input" data-parsley-error-message="Enter Answer to Security Question" required>
                  <label>Answer to Security Question</label>
                </div>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" name="addBen" value="addBen" class="btn rounded btn-outline b-success text-success p-x-md">Add Beneficiary</button>
            <button type="button" class="btn rounded btn-outline b-danger text-danger p-x-md" data-dismiss="modal">Cancel</button>
          </div>
        </div><!-- /.modal-content -->
      </form>
      </div>
    </div>
    <!-- / .modal -->


    <!-- Manage Beneficiary modal -->
    <div id="manageben" class="modal" data-backdrop="true">
      <div class="modal-dialog">
      <form id="Dlben" method="post" action="" onsubmit="return confirm('Are you sure you want to delete this beneficiary?');" ui-jp="parsley">
        <div class="modal-content">
          <div class="modal-header">
          	<h5 class="modal-title">Manage Beneficiary</h5>
          </div>
          <div class="modal-body p-lg">
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th>Account Number</th>
                      <th>Name</th>
                      <th>Bank</th>
                      <th>Country</th>
                      <th>Delete</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
        				$beneficiary = get_beneficiary($_SESSION['user_number']);
                        if (empty($beneficiary)) {
                        echo '<tr><td colspan="5">No Beneficiary Added Yet</td></tr>';
                        }else{
        				foreach($beneficiary as $ben)
        				{
        				echo '
                        <tr>
                          <td>'.$ben['ben_acc'].'</td>
                          <td>'.$ben['ben_name'].'</td>
                          <td>'.$ben['ben_bnk'].'</td>
                          <td>'.$ben['ben_country'].'</td>
                          <td><input type="hidden" name="benName" value="'.$ben['ben_name'].'" /><button type="submit" name="delBen" value="'.$ben['ben_id'].'" class="nav-link label danger">Delete</button></td>
                        </tr>
        				';
        				}
                        }
        			?>
                  </tbody>
                </table>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn rounded btn-outline b-danger text-danger p-x-md" data-dismiss="modal">Cancel</button>
          </div>
        </div><!-- /.modal-content -->
      </form>
      </div>
    </div>
    <!-- / .modal -->


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



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

  </div>
<!-- build:js scripts/app.html.js -->
<!-- jQuery -->
  <script src="libs/jquery/jquery/dist/jquery.js"></script>
  <script src="scripts/jquery.maskMoney.js"></script>
  <script>
    $(function() {
        $("#Amnt").maskMoney();
    });
  </script>
  <script src="scripts/countrySelect.js"></script>
  <script>
    $("#country").countrySelect({
    //defaultCountry: "jp",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    //preferredCountries: ['ca', 'gb', 'us']
    });
  </script>

<!-- Bootstrap -->
  <script src="libs/jquery/tether/dist/js/tether.min.js"></script>
  <script src="libs/jquery/bootstrap/dist/js/bootstrap.js"></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>
