<?php include_once "header.php" ?>
<?php

//$user->log($user_id, 14, "");

function build_table($array){
  // start table
  //$html = '<table id="waristable" class="table table-bordered table-hover"><thead>';
  // header row
  $html .= "    <tr>\r\n";
  $html .= "      <th></th>";
  foreach($array[0] as $key=>$value){
          $html .= "      <th>" . $key . "</th>\r\n";
  }
  $html .= "    </tr>\r\n";
  $html .= "  </thead>\r\n";
  $html .= "  <tbody>\r\n";

  // data rows
  foreach( $array as $key=>$value){
      $html .= "    <tr>\r\n";
      $html .= "      <td></td>";
      foreach($value as $key2=>$value2){
        $html .= "      <td>" . $value2 . "</td>\r\n";
      }
      $html .= "    </tr>\r\n";
  }

  // finish table and return it
  //$html .= '</tbody></table>';
  $html .= "  </tbody>\r\n";
  $html .= "  <tfoot>\r\n";
  $html .= "    <tr>\r\n";
  $html .= "      <th></th>";
  foreach($array[0] as $key=>$value){
          $html .= "      <th>" . $key . "</th>\r\n";
  }
      
  return $html;
}

// Open connection
if (!isset($conn)) $conn = ew_Connect();

$user_id_withrank = trim(ew_RemoveXSS(ew_StripSlashes($_SESSION['ARMYNOWITHRANK'])));

////////////////////
// Retrieve data
////////////////////
     
$sql = "SELECT  USER_ID
               ,STATUS_ID
               ,STATUS_DESC
               ,ACTIVITY_MESSAGE
               ,CREATED_AT
            FROM VIEW_WEBACTIVITYLOGS
         WHERE USER_ID=:uid
           AND CREATED_AT >= dateadd(mm, -3, getdate())
         ORDER BY CREATED_AT DESC
       ";
$stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute(array(':uid'=>$user_id));
if ($stmt->rowCount() > 0){
  $myarray = array();
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){    
    $user_id          = $row['USER_ID'         ];
    $status_id        = $row['STATUS_ID'       ];
    $status_desc      = $row['STATUS_DESC'     ];
    $activity_message = $row['ACTIVITY_MESSAGE'];
    $created_at       = date("Y-m-d H:i:s", strtotime($row['CREATED_AT'      ]));
    
    
    //$myarray[] = array('Kod Dana'=>$fundcode, 'Dana'=>$fundname, 'Unit'=>$investmentunits, 'Tarikh Baki'=>$balancedate, 'Harga Dana'=>$fundprice, 'Tarikh Dana'=>$funddate);
    $myarray[] = array('User ID'=>$user_id, 'Status'=>$status_desc, 'Activity'=>$activity_message, 'Activity Date'=>$created_at);
        
  }
} else {
  //$this->setFailureMessage("No records are found.");
} 

//print_r($myarray);


?>



<!-- Full Width Column -->
<div class="content-wrapper">
    <div class="container">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Activity Logs
            <small>Check Activity Logs</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i class="active">Home</a></li>
            <li>Activity Logs</li>
          </ol>
        </section>    
    
        <!-- Main content -->
        <section class="content">
            <!-- Default box -->
            <div class="box box-primary">
                <div class="box-body">
                
<div class="text-left">You can check your activity logs history here. Only the latest 3 months history will be displayed.</div><br>


<?php 
  if (isset($myarray))
  {
      echo "\r\n<table id=\"activitylogs\" class=\"table table-striped table-bordered table-hover display responsive nowrap\"  width=\"100%\" cellspacing=\"0\">\r\n";
      echo "  <thead>\r\n";
      echo build_table($myarray); 
      echo "    </tr>\r\n";
      echo "  </tfoot>\r\n";
      echo "</table>\r\n";
    
      
      //echo '<table id="investorunits" class="table table-striped table-bordered table-hover display responsive nowrap"><thead>';
      //echo build_table($myarray); 
      //echo '</tfoot></table>';
  } else {
    echo "<br><br>Tiada rekod dijumpai.";
  }
?>


<br>


                </div>   
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </section>
        <!-- /.content -->
    </div>
    <!-- /.container -->
</div>
<!-- /.content-wrapper -->

<?php include_once "footer.php" ?>
<script>
  $(function () {
    var dataTable = $('#activitylogs').DataTable({      
      "columnDefs": [ {
          "searchable": false,
          "orderable": false,
          "targets": 0
      } ],    
      "paging": true,
      "lengthChange": true,
      "lengthMenu": [ 5, 10, 25, 50 ],
      "pageLength": 5,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": false
    });

    dataTable.on( 'order.dt search.dt', function () {
        dataTable.column(0, {search:'applied', order:'applied'}).nodes().each( function (cell, i) {
            cell.innerHTML = i+1;
        } );
    } ).draw();     
    
  });
</script>