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

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

$page = "Statements";
$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-->

    <?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>";
    }
    ?>


    <div class="padding">
      <div class="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header">
              <h2>Account Statement</h2>
              <small>Select account, date range and click view to view statement.</small>
            </div>
            <div class="box-divider m-a-0"></div>
            <div class="box-body p-v-md">
            <form id="Stmnt" method="post" action="" enctype="multipart/form-data" ui-jp="parsley">
              <div class="row row-sm">
                <div class="col-sm-6">
                    <div class="md-form-group">
                      <select name="uAcc" class="md-input" data-parsley-error-message="Select Account" required>
                        <option value="">Select Account</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'].'</option>
            				';
            				}
                            }
            			?>
        			  </select>
                      <label>Select Account</label>
                    </div>
                </div>
              </div>
              <div class="row row-sm">
                <div class="col-sm-4">
                    <div class="md-form-group float-label">
                        <input type="text" name="uSdate" class="md-input" ui-jp="datetimepicker" ui-options="{
                            format: 'YYYY-MM-DD',
                            icons: {
                              time: 'fa fa-clock-o',
                              date: 'fa fa-calendar',
                              up: 'fa fa-chevron-up',
                              down: 'fa fa-chevron-down',
                              previous: 'fa fa-chevron-left',
                              next: 'fa fa-chevron-right',
                              today: 'fa fa-screenshot',
                              clear: 'fa fa-trash',
                              close: 'fa fa-remove'
                            }
                          }"  data-parsley-error-message="Select Start Date"  required>
                    	<label>Start Date</label>
                    </div>
                </div>
                <div class="col-sm-4">
                    <div class="md-form-group float-label">
                        <input type="text" name="uEdate" class="md-input" ui-jp="datetimepicker" ui-options="{
                            format: 'YYYY-MM-DD',
                            icons: {
                              time: 'fa fa-clock-o',
                              date: 'fa fa-calendar',
                              up: 'fa fa-chevron-up',
                              down: 'fa fa-chevron-down',
                              previous: 'fa fa-chevron-left',
                              next: 'fa fa-chevron-right',
                              today: 'fa fa-screenshot',
                              clear: 'fa fa-trash',
                              close: 'fa fa-remove'
                            }
                          }"  data-parsley-error-message="Select End Date"  required>
                    	<label>End Date</label>
                    </div>
                </div>
                <div class="col-sm-4">
                  <button type="submit" class="btn rounded btn-outline main mainbd p-x-md">View Statement</button>
                </div>
              </div>
            </form>
            </div>
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-sm-12">
          <div class="box">
            <div class="box-header">
              <h3>Transaction Details</h3>
            </div>
            <div class="table-responsive">
                <table ui-jp="dataTable" 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
            			if ($_SERVER["REQUEST_METHOD"] == "POST") {
            			    if ($_POST['uAcc'] == '') { store_msg("Select Account", "danger"); redirect(STATEMENT);}
                            elseif ($_POST['uSdate'] == '') { store_msg("Select Start Date", "danger"); redirect(STATEMENT);}
                            elseif ($_POST['uEdate'] == '') { store_msg("Select End Date", "danger"); redirect(STATEMENT);}
            			    else{
            			    $account = clean_data($_POST['uAcc']);
                            $start = clean_data($_POST['uSdate']);
                            $end = clean_data($_POST['uEdate']);
            			    }
            			    dbconnect();
            			    $sql = "SELECT * FROM `".TRANS_TABLE_NAME."` WHERE DATE(trans_date) BETWEEN DATE('{$start}') AND DATE('{$end}') AND acc_no = {$account} AND trans_status = 1 ORDER BY trans_date DESC";
            			    $stmt = $pdo->query($sql);
                            if ($stmt->rowCount() > 0) {
            			    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
                                echo '<tr>';
                                echo '<td>'.date('jS F Y', strtotime($row['trans_date'])).'</td>';
                                echo '<td>'.$row['acc_no'].'</td>';
                                echo '<td>'.$row['trans_details'].'</td>';
                                echo '<td>'.$row['currency'].''.number_format($row['debit'],2).'</td>';
                                echo '<td>'.$row['currency'].''.number_format($row['credit'],2).'</td>';
                                echo '<td>'.(($row['trans_status'] == '1')?'<span class="label green">Completed</span>':'<span class="label red">Pending</span>').'</td>';
                                echo '</tr>'; }
                            }else{
                                echo '<tr>';
                                echo '<td colspan="6">No records found</td>';
                                echo '</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>
