<!DOCTYPE HTML>
<?PHP 
require 'functions.php';
checkLogin();
$db_link = connect();

// 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%;">
        <!-- TABLE: Groups -->
        <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)) { 
                echo '<tr>
                    <td><a href="group.php?group_id='.$row_group['group_id'].'">'.$row_group['group_id'].'</a></td>
                    <td>'.$row_group['group_name'].'</td>
                    <td>'.date("d.m.Y", $row_group['group_created']).'</td>
                    <td>'.$row_group['group_status'].'</td>
                    <td><a href="group_member.php?group_id='.$row_group['group_id'].'">View Members</a></td>
                </tr>';
            } 
            ?>
        </table>
    </div>
    <!-- RIGHT SIDE: Group Loans Account -->
    <?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 loans
        $sql_loans = "SELECT * FROM group_loans WHERE group_id = '$group_id'";
        $query_loans = mysqli_query($db_link, $sql_loans);
        checkSQL($db_link, $query_loans);
        ?>
        <?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);
    ?>
    <div class="content_left" style="width:60%;">
        <p class="heading">Group Details</p>
        <p>Group ID: <?PHP echo $result_group['group_id']; ?></p>
        <p>Group Name: <?PHP echo $result_group['group_name']; ?></p>
        <p>Group Picture: <?PHP if (isset($result_group['group_pic'])) echo '<img src="'.$result_group['group_pic'].'" />'; ?></p>
        <p class="heading">Group Members</p>
        <?PHP 
        $sql_members = "SELECT * FROM group_member WHERE group_id = '$group_id'";
        $query_members = mysqli_query($db_link, $sql_members);
        checkSQL($db_link, $query_members);
        while ($row_member = mysqli_fetch_assoc($query_members)) {
            $member_id = $row_member['member_id'];
            $sql_member_details = "SELECT * FROM group_member WHERE member_id = '$member_id'";
            $query_member_details = mysqli_query($db_link, $sql_member_details);
            checkSQL($db_link, $query_member_details);
            $result_member_details = mysqli_fetch_assoc($query_member_details);
            ?>
            <p>Member ID: <?PHP echo $result_member_details['member_id']; ?></p>
            <p>Member Name: <?PHP echo $result_member_details['member_name']; ?></p>
            <p>Member Phone: <?PHP echo $result_member_details['member_contact']; ?></p>
            <hr>
        <?PHP } ?>
    </div>
    <div class="content_right" style="width:40%;">
        <p class="heading">Group Loan Account</p>
        <?PHP 
        // Get group loans
        $sql_loans = "SELECT * FROM group_loans WHERE group_id = '$group_id'";
        $query_loans = mysqli_query($db_link, $sql_loans);
        checkSQL($db_link, $query_loans);
        while ($row_loan = mysqli_fetch_assoc($query_loans)) {
            ?>
            <p>Loan ID: <?PHP echo $row_loan['loan_id']; ?></p>
            <p>Loan Amount: <?PHP echo $row_loan['loan_amount']; ?></p>
            <p>Loan Balance: <?PHP echo $row_loan['loan_balance']; ?></p>
            <hr>
        <?PHP } ?>
    </div>
<?PHP } ?>

        <div class="content_right" style="width:40%;">
            <!-- TABLE: Group Loans -->
            <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">Group Loans</th>
                </tr>
                <tr>
                    <th>Loan ID</th>
                    <th>Amount</th>
                    <th>Balance</th>
                    <th>Status</th>
                    <th>Next Payment</th>
                </tr>
                <?PHP 
                while ($row_loan = mysqli_fetch_assoc($query_loans)) { 
                    echo '<tr>
                        <td>'.$row_loan['loan_id'].'</td>
                        <td>'.$row_loan['loan_amount'].'</td>
                        <td>'.$row_loan['loan_balance'].'</td>
                        <td>'.$row_loan['loan_status'].'</td>
                        <td>'.$row_loan['next_payment'].'</td>
                    </tr>';
                } 
                ?>
            </table>
        </div>
    <?PHP } ?>
</body>
</html>

