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

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

$page = "Dashboard";
$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']);

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-->
    <div class="item">
        <div class="item-bg">
          <img src="users/<?php echo user_photo($_SESSION['user_login']); ?>" class="blur opacity-3">
        </div>
        <div class="p-a-md">
          <div class="row m-t">
            <div class="col-sm-10">
              <a href class="pull-left m-r-md">
                <span class="avatar w-96">
                  <img src="users/<?php echo user_photo($_SESSION['user_login']); ?>">
                  <i class="on b-white"></i>
                </span>
              </a>
              <div class="clear m-b">
                <p class="text-md profile-status">Welcome</p>
                <h3 class="m-a-0 m-b-xs"><?php echo user_names($_SESSION['user_login']); ?></h3>
                <p class="text-muted">
                    <?php
                    $lastlogin = last_login($_SESSION['user_login']);
                    foreach($lastlogin as $last){
                        if ($last['last_ts'] == '0000-00-00 00:00:00') {
                            echo '';
                        }else{
                            $lastdate = date('l jS F Y', strtotime($last['last_ts']));
                            $lastip = $last['last_ip'];
                            echo'<span class="m-r">Last login: <strong>'.$lastdate.'</strong></span>';
                            echo' From: <small><strong><i class="fa fa-map-marker m-r-xs"></i>'.$lastip.'</strong> </small><br>';
                            echo' <a href="http://api.hostip.info/get_html.php?ip='.$lastip.'" target="_blank"><img src="http://api.hostip.info/flag.php?ip='.$lastip.'" alt="Country" width="39" height="24"></a> ';
                        }
                    } ?>
                </p>
              </div>
            </div>
          </div>
        </div>
    </div>


    <?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="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header">
              <h3>Account Balance</h3>
            </div>
            <div class="table-responsive">
                <table class="table">
                  <thead>
                    <tr>
                      <th>Account Number</th>
                      <th>Status</th>
                      <th>Account Type</th>
                      <th>Currency</th>
                      <th>Book Balance</th>
                      <th>Available Balance</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
        				$accounts = get_accs($_SESSION['user_number']);
                        if (empty($accounts)) {
                        echo '<tr><td colspan="6">No account exist</td></tr>';
                        }else{
        				foreach($accounts as $acc)
        				{
        				echo '
                        <tr>
                          <td>'.$acc['acc_no'].'</td>
                          <td>'.(($acc['acc_status'] == 'active')?'<span class="label green">'.$acc['acc_status'].'</span>':'<span class="label red">'.$acc['acc_status'].'</span>').'</td>
                          <td>'.$acc['acc_type'].'</td>
                          <td>'.$acc['currency'].'</td>
                          <td>'.number_format(get_balance($acc['acc_no']),2).'</td>
                          <td><strong>'.number_format(get_balance($acc['acc_no']),2).'</strong></td>
                        </tr>
        				';
        				}
                        }
        			?>
                  </tbody>
                </table>
            </div>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header">
              <h3>Recent Transactions</h3>
            </div>
            <div class="table-responsive">
                <table class="table table-striped b-t">
                  <thead>
                    <tr>
                      <th>Transaction Date</th>
                      <th>Account Number</th>
                      <th>Narrative</th>
                      <th>Debit</th>
                      <th>Credit</th>
                      <th>Status</th>
                    </tr>
                  </thead>
                  <tbody>
                    <?php
        				$recents = recent_transactions($_SESSION['user_number']);
                        if (empty($recents)) {
                        echo '<tr><td colspan="6">No records found</td></tr>';
                        }else{
        				foreach($recents as $trans)
        				{
        				echo '
                        <tr '.(($trans['trans_status'] == '1')?'style="background-color: #C3E4C4 !important"':'style="background-color: #FBC0BC !important"').'>
                          <td>'.date('jS F Y', strtotime($trans['trans_date'])).'</td>
                          <td>'.$trans['acc_no'].'</td>
                          <td>'.$trans['trans_details'].'</td>
                          <td>'.$trans['currency'].''.number_format($trans['debit'],2).'</td>
                          <td>'.$trans['currency'].''.number_format($trans['credit'],2).'</td>
                          <td>'.(($trans['trans_status'] == '1')?'<span class="label green">Completed</span>':'<span class="label red">Pending</span>').'</td>
                        </tr>
        				';
        				}
                        }
        			?>
                  </tbody>
                </table>
            </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>
<!-- 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>
