<!DOCTYPE HTML>
<?PHP 
require 'functions.php';
checkLogin();
$db_link = connect();
getGroupID($db_link);
// Get all groups
$sql_groups = "SELECT * FROM `group`";
$query_groups = mysqli_query($db_link, $sql_groups);
checkSQL($db_link, $query_groups);
?>

<html>
<?PHP includeHead('Groups', 0) ?>
<body>
    <!-- MENU -->
    <?PHP includeMenu(2); ?>
    <div id="menu_main">
        <a href="group_search.php">Search</a>
        <a href="group_new.php">New Group</a>
        <a href="group_act.php">Active Groups</a>
    </div>
    <!-- LEFT SIDE: Group Details -->
<div class="content_left" style="width:60%;">
  <?php
if (isset($_GET['group_id'])) {
    $group_id = sanitize($db_link, $_GET['group_id']);
    $sql_group = "SELECT * FROM `group` WHERE group_id = '$group_id'";
    $query_group = mysqli_query($db_link, $sql_group);
    checkSQL($db_link, $query_group);
    $result_group = mysqli_fetch_assoc($query_group);

    // Get group members
    $sql_members = "SELECT * FROM group_member WHERE group_id = '$group_id'";
    $query_members = mysqli_query($db_link, $sql_members);
    checkSQL($db_link, $query_members);

    ?>
    <div class="content_left" style="width:60%;">
        <p class="heading">Group Details</p>
        <p>Group Name: <?php echo $result_group['group_name']; ?></p>
        <p class="heading">Group Members</p>
        <?php while ($row_member = mysqli_fetch_assoc($query_members)) { ?>
            <p>Member Name: <?php echo $row_member['member_name']; ?></p>
            <p>Member Contact: <?php echo $row_member['member_contact']; ?></p>
            <hr>
        <?php } ?>
        <p><a href="give_loan.php?group_id=<?php echo $group_id; ?>">Give Loan</a></p>
    </div>
<?php } else { ?>
    <!-- TABLE: Groups -->
    <div class="content_left" style="width:60%;">
        <table id="tb_table">
            <colgroup>
                <col width="20%">
                <col width="20%">
                <col width="20%">
                <col width="20%">
                <col width="20%">
            </colgroup>
            <tr>
                <th class="title" colspan="5">Groups</th>
            </tr>
            <tr>
                <th>Group ID</th>
                <th>Group Name</th>
                <th>Created</th>
                <th>Status</th>
                <th>Members</th>
            </tr>
            <?php while ($row_group = mysqli_fetch_assoc($query_groups)) { ?>
                <tr>
                    <td><a href="group.php?group_id=<?php echo $row_group['group_id']; ?>"><?php echo $row_group['group_id']; ?></a></td>
                    <td><?php echo $row_group['group_name']; ?></td>
                    <td><?php echo date("d.m.Y", $row_group['group_created']); ?></td>
                    <td><?php echo $row_group['group_status']; ?></td>
                    <td><a href="group_member.php?group_id=<?php echo $row_group['group_id']; ?>">View Members</a></td>
                </tr>
            <?php } ?>
        </table>
    </div>
<?php } ?>

    </div>
 
	<!-- RIGHT SIDE: Account Details -->
<div class="content_right" style="width:40%;">
    <table id="tb_table">
        <colgroup>
            <col width="20%">
            <col width="20%">
            <col width="20%">
            <col width="20%">
            <col width="20%">
             <?php if ($_SESSION['log_delete'] == 1) { ?>
      <col width="5%">
    <?php } ?>
        </colgroup>
        <tr>
    <th class="title" colspan="<?php if ($_SESSION['log_delete'] == 1) { echo '6'; } else { echo '5'; } ?>">Loans Account</th>
  </tr>
        <tr>
            <th>Loan id.</th>
            <th>Status</th>
            <th>Amount</th>
            <th>Balance</th>
            <th>Next payment</th>
             <?php if ($_SESSION['log_delete'] == 1) { ?>
      <th>Delete</th>
    <?php } ?>
        </tr>
        <?php
        // Select all loans for current customer
        $sql_loans = "SELECT gl.*, ls.loanstatus_status 
                      FROM group_loans gl 
                      JOIN loanstatus ls ON gl.loanstatus_id = ls.loanstatus_id 
                      WHERE gl.group_id = ?";
        $stmt = mysqli_prepare($db_link, $sql_loans);
        mysqli_stmt_bind_param($stmt, "i", $_SESSION['group_id']);
        mysqli_stmt_execute($stmt);
        $query_loans = mysqli_stmt_get_result($stmt);

        while ($row_loan = mysqli_fetch_assoc($query_loans)) {
            // Get loan balances
            $loan_balances = getgroupLoanBalance($db_link, $row_loan['loan_id']);

            // Select last unpaid Due Date from LTRANS
            $sql_ltrans = "SELECT MIN(ltrans_due) as next_due 
                           FROM group_ltrans 
                           WHERE loan_id = ? AND ltrans_due IS NOT NULL AND ltrans_date IS NULL";
            $stmt_ltrans = mysqli_prepare($db_link, $sql_ltrans);
            mysqli_stmt_bind_param($stmt_ltrans, "i", $row_loan['loan_id']);
            mysqli_stmt_execute($stmt_ltrans);
            $query_ltrans = mysqli_stmt_get_result($stmt_ltrans);
            $next_due = mysqli_fetch_assoc($query_ltrans);

            echo '<tr>
                    <td><a href="group_loan.php?loan_id='.$row_loan['loan_id'].'">'.$row_loan['loan_no'].'</a></td>
                    <td>'.$row_loan['loanstatus_status'].'</td>';
            if ($row_loan['loan_issued'] == 1) {
                echo ' <td>'.number_format($loan_balances['pdue']+$loan_balances['idue']).'</td>
                       <td>'.number_format($loan_balances['balance']).'</td>';
            } else {
                echo '<td>'.number_format($row_loan['loan_principal']).'</td>
                      <td>N/A</td>';
            }
            if ($row_loan['loanstatus_id'] == 2 && isset($next_due['next_due'])) {
                echo '<td';
                if ($next_due['next_due'] < time()) {
                    echo ' class="warn"';
                }
                if ($next_due['next_due'] != null) {
                    echo '>'.date("d.m.Y", $next_due['next_due']).'</td>';
                } else {
                    echo '></td>';
                }
            } else {
                echo '<td>N/A</td>';
            }
            if ($_SESSION['log_delete'] == 1) {
      echo '<td><a href="group_loan_del.php?loan_id='.$row_loan['loan_id'].'" onClick="return confirm(\'Are you sure you want to delete this loan?\');"><i class="fa fa-remove fa-lg"></i></a></td>';
    } 
            echo '</tr>';
        }
        ?>
    </table>
</div>





    
</body>
</html>

