<div class="container" style="background-color:#e0a3a3;padding:8px;">
  <ul class="nav nav-pills nav-justified">
    <li <?php if(isset($_GET['Type']) && $_GET['Type']=="FD") echo'class="active"';?>><a href="View_ac?Type=FD">Fixed Deposits</a></li>
    <li <?php if(isset($_GET['Type']) && $_GET['Type']=="Loan") echo'class="active"';?>><a href="View_ac?Type=Loan">Loan A/C</a></li>
    <li <?php if(isset($_GET['Type']) && $_GET['Type']=="Recurring") echo'class="active"';?>><a href="View_ac?Type=Recurring">Recurring A/Cs</a></li>
  </ul>
</div>


<?php
// var_dump($_SESSION['EMAIL']);
function view_list_ac($type) {
echo '
<div class="container" style="margin-top: 70px;">
<h5>VIEW CUSTOMERS</h5><hr>
</div>
<div class="container">
<table class="table table-striped table-bordered table-hover table-responsive">
  <tr>
    <th>SL</th>
    <th>Type</th>
    <th>AC No</th>
    <th>Name</th>
    <th>Account Creation Date</th>
    <th>Loan Amount</th>
    <th>PHONE</th>
    <th>Balance</th>
    <th>Total No Of Installment to be paid</th>
    <th>No Of Paid Installment</th>
    <th>No of Expected Installment till today</th>
    <th>Installment Amount</th>
    <th>Due Installment</th>
    <th>Due Amount</th>
    <th>Due after Fine</th>
  </tr>';
  $conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }

  $sql = "SELECT * FROM `".$GLOBALS['arif_ac']."` WHERE `AA_TYPE`='".$type."' AND `AA_AGENT`='".$_SESSION['EMAIL']."' ORDER BY `AA_ID` DESC";
  $result = $conn->query($sql);

  if ($result->num_rows > 0) {
      while($row = $result->fetch_assoc()) {
          //$ID=$row["GC_ID"];
          $tt=$row["AA_ID"]-10;
          $date1 = date_create($row["AA_DATE"]);
          $date2 = date_create(date("Y/m/d"));
          $diff = date_diff($date1, $date2);
          if($type === 'Loan'){
            $no_paid_inst = ($row["AA_MATURE_VALUE"] + $row["AA_BAL"]) / $row["AA_INSTALLMENT"];
          } else {
            if($row["AA_INSTALLMENT"]==0)$no_paid_inst = "NaN"; else $no_paid_inst = $row["AA_BAL"] / $row["AA_INSTALLMENT"];
          }
          if ($row["AA_ACTYPE"] == 'D'){$diff = $diff->format("%a"); $diff=(int)$diff;$due_i=$diff-$row["AA_NO_OF_PAYPAID"];} else {$diff=$diff->format("%m"); $diff=(int)$diff;$due_i=$diff-$row["AA_NO_OF_PAYPAID"];}
          echo "
          <tr>
            <td>".$tt."</td>
            <td>".$row["AA_ACTYPE"].",".$row["AA_TYPE"]."</td>
            <td><a href='./Details?no=".$row["AA_ACNO"]."&type=".$row["AA_TYPE"]."'>".$row["AA_ACNO"]."</a> &nbsp;&nbsp; <a href='./Trans_New?no=".$row["AA_ACNO"]."&type=".$row["AA_TYPE"]."'>Transact</a></td>
            <td>".$row["AA_NAME"]."</td>
            <td>".$row["AA_DATE"]."</td>
            <td>".$row["AA_MATURE_VALUE"]."</td>
            <td>".$row["AA_PHONE"]."</td>
            <td>".$row["AA_BAL"]. "</td>
            <td>".$row["AA_NO_OF_PAYMENT"]."</td>
            <td>".$no_paid_inst."</td>
            <td>".$diff."</td>
            <td>".$row["AA_INSTALLMENT"]."</td>
            <td>".$due_i."</td>";
            if ($due_i > 0) {
              $due_amount = $due_i * $row["AA_INSTALLMENT"];
              echo "<td class='alert danger text-danger'>".$due_amount."</td>";
            }
            else {
              $due_amount = 'All Clear';
              echo "<td class='alert success text-success'>".$due_amount."</td>";
            }
            if ($due_i > 0 && $type == "Recurring") {
              $due_amount = $due_i * $row["AA_INSTALLMENT"];
              $due_amount = (($due_amount) * 20)/100;
              echo "<td class='alert danger text-danger'>".$due_amount."</td>";
            }
            elseif ($due_i > 0 && $type == "Loan") {
              $due_amount = $due_i * $row["AA_INSTALLMENT"];
              $due_amount = (($due_amount) * 40)/100;
              echo "<td class='alert danger text-danger'>".$due_amount."</td>";
            }
             else {
              $due_amount = 'Up to Date!!';
              echo "<td class='alert success text-success'>".$due_amount."</td>";
            }

            echo "</tr>";
      }
  } else {
      echo "0 results";
  };
  $conn->close();

echo '
</table>
</div>
';
}


if(isset($_GET['Type']) && $_GET['Type']=="Loan") view_list_ac('Loan');
if(isset($_GET['Type']) && $_GET['Type']=="Recurring") view_list_ac('Recurring');
if(isset($_GET['Type']) && $_GET['Type']=="FD") view_list_ac('FD');
?>
