<?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";
  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";
      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";
  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  a.AccountNo
               ,a.BalanceDate
               ,a.FundCode
               ,a.FundName
               ,a.InvestmentUnits
               ,b.FundDate
               ,b.FundPrice
          FROM [dbo].[AFFINHWANG_tbl_investorsholding] a
          LEFT OUTER JOIN [dbo].[AFFINHWANG_tbl_dailyprice] b
            ON b.FundCode=a.FundCode
         WHERE a.PersonnelID='99999992'
       ";
*/       
$sql = "SELECT  AccountNo
               ,BalanceDate
               ,FundCode
               ,FundName
               ,InvestmentUnits
          FROM [dbo].[AFFINHWANG_tbl_investorsholding]
         WHERE PersonnelID='$user_id_withrank'
       ";
$rs  = ew_Execute($sql);
if ($rs->RecordCount() > 0) {
  $rs->MoveFirst();
  $myarray = array();
  while ($rs && !$rs->EOF) {
    $accountno       = $rs->fields['AccountNo'      ];
    $balancedate     = date("Y-m-d", strtotime($rs->fields['BalanceDate']));
    $fundcode        = $rs->fields['FundCode'       ];
    $fundname        = $rs->fields['FundName'       ];
    $investmentunits = sprintf('%0.4f', $rs->fields['InvestmentUnits']);
    //$funddate        =  date("Y-m-d", strtotime($rs->fields['FundDate']));
    //$fundprice       = sprintf('%0.4f', $rs->fields['FundPrice']);
    
    
    //$myarray[] = array('Kod Dana'=>$fundcode, 'Dana'=>$fundname, 'Unit'=>$investmentunits, 'Tarikh Baki'=>$balancedate, 'Harga Dana'=>$fundprice, 'Tarikh Dana'=>$funddate);
    $myarray[] = array('Kod Dana'=>$fundcode, 'Dana'=>$fundname, 'Unit'=>$investmentunits, 'Tarikh Baki'=>$balancedate);
        
    $rs->MoveNext();
  }
  //echo $sData;
  $rs->Close();
} else {
  //$this->setFailureMessage("No records are found.");
} 


/////////////////////////
// Retrieve daily price
/////////////////////////
$sql = "SELECT  FundCode
               ,FundName
               ,FundPrice
               ,FundDate
          FROM [dbo].[AFFINHWANG_tbl_dailyprice]
       ";
$rs  = ew_Execute($sql);
if ($rs->RecordCount() > 0) {
  $rs->MoveFirst();
  $myarray2 = array();
  while ($rs && !$rs->EOF) {
    $fundcode        = $rs->fields['FundCode'       ];
    $fundname        = $rs->fields['FundName'       ];
    $funddate        =  date("Y-m-d", strtotime($rs->fields['FundDate']));
    $fundprice       = sprintf('%0.4f', $rs->fields['FundPrice']);
    
    
    $myarray2[] = array('Kod Dana'=>$fundcode, 'Dana'=>$fundname, 'Harga Dana'=>$fundprice, 'Tarikh Dana'=>$funddate);
        
    $rs->MoveNext();
  }
  //echo $sData;
  $rs->Close();
} else {
  //$this->setFailureMessage("No records are found.");
}

//print_r($myarray2);


?>

<style>
.modal.fade.in .modal-body {
    bottom: 0; 
    opacity: 1;
}
.modal-body {
    position: absolute;
    bottom: -250px;
    right: 0;
    padding: 30px 15px 15px;
    width: 100%;
    height: 250px;
    background-color: #e5e5e5;
    border-radius: 6px 6px 0 0;
    opacity: 0;
    -webkit-transition: opacity 0.3s ease-out, bottom 0.3s ease-out;
    -moz-transition: opacity 0.3s ease-out, bottom 0.3s ease-out;
    -o-transition: opacity 0.3s ease-out, bottom 0.3s ease-out;
    transition: opacity 0.3s ease-out, bottom 0.3s ease-out;
}
.close {
    margin-top: -20px;
    text-shadow: 0 1px 0 #ffffff;
}
.popup-button {
    margin-left: 140px;
    margin-top: 77px;
    font-weight: bold;
}
</style>



<!-- Full Width Column -->
<div class="content-wrapper">
    <div class="container">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Unit Trust
            <small>Semakan Unit Trust</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i class="active">Laman Utama</a></li>
            <li>Unit Trust</li>
          </ol>
        </section>    
    
        <!-- Main content -->
        <section class="content">
            <!-- Default box -->
            <div class="box box-primary">
                <div class="box-body">




<?php 
  if (!isset($myarray)) echo "<!--";
?>
<div class="text-right"><strong>Account No:</strong> <button type="button" class="btn btn-info"><?php echo $accountno; ?></button><p></div>
<?php 
  if (!isset($myarray)) echo "-->";
?>


<!-- Modal Structure -->
<div class="modal fade" id="slide-bottom-popup" data-keyboard="false" data-backdrop="false">
  <div class="modal-body">
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Tutup</span></button>

    <?php 
    
        echo "<div class=\"content-wrapper\">\r\n";
        echo "    <div class=\"container\">\r\n";
        echo "        <section class=\"content-header\">\r\n";
        echo "          <h1>\r\n";
        echo "            Unit Trust\r\n";
        echo "            <small>Harga Dana Harian</small>\r\n";
        echo "          </h1>\r\n";
        echo "          </ol>\r\n";
        echo "        </section>\r\n";
        echo "        <section class=\"content\">\r\n";
        echo "            <div class=\"box box-primary\">\r\n";
        echo "                <div class=\"box-body\">\r\n";

        echo "\r\n<table id=\"dailyprice\" class=\"table table-striped table-bordered table-hover display responsive nowrap\"  width=\"100%\" cellspacing=\"0\">\r\n";
        echo "  <thead>\r\n";
        echo build_table($myarray2); 
        echo "    </tr>\r\n";
        echo "  </tfoot>\r\n";
        echo "</table>\r\n";
        
        //echo '<table id="dailyprice" class="table table-striped table-bordered dt-responsive nowrap"><thead>';
        //echo build_table($myarray2); 
        //echo '</tfoot></table>';
        

        echo "                </div>\r\n";
        echo "            </div>\r\n";
        echo "        </section>\r\n";
        echo "    </div>\r\n";
        echo "</div>\r\n";
    ?>
  </div>
  <!-- /.modal-body -->
</div>
<!-- /.modal -->


<?php 
  if (isset($myarray))
  {
      echo "\r\n<table id=\"investorunits\" 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>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#slide-bottom-popup">
  SEMAK HARGA HARIAN
</button>



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

<?php include_once "footer.php" ?>
<script>
  $(function () {
    $('#dailyprice').DataTable({   
      "paging": false,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false
    });

    $('#investorunits').DataTable({      
      "paging": true,
      "lengthChange": true,
      "lengthMenu": [ 5, 10, 25, 50 ],
      "pageLength": 5,
      "searching": true,
      "ordering": true,
      "info": true,
      "autoWidth": false
    });
  });
</script>