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

//CREATE-Button
if (isset($_POST['create'])){
    //Sanitize user input
    $group_name = sanitize($db_link, $_POST['group_name']);

    //Insert new Group into GROUP
    $sql_insert_group = "INSERT INTO `group` (group_name, group_created, user_id) VALUES ('$group_name', $timestamp, $_SESSION[log_id])";
    $query_insert_group = mysqli_query($db_link, $sql_insert_group);
    checkSQL($db_link, $query_insert_group);

    //Get the generated group_id
    $group_id = mysqli_insert_id($db_link);

    for ($i = 1; $i <= 5; $i++) {
        $member_name = sanitize($db_link, $_POST['member_name_'.$i]);
        $member_contact = sanitize($db_link, $_POST['member_contact_'.$i]);
        $member_residence = sanitize($db_link, $_POST['member_residence_'.$i]);
        $member_occupation = sanitize($db_link, $_POST['member_occupation_'.$i]);
        $member_position = sanitize($db_link, $_POST['member_position_'.$i]);

        if (!empty($member_name)) {
            //Insert new Member into MEMBER
            $sql_insert_member = "INSERT INTO group_member (group_id, member_name, member_contact, member_residence, member_occupation, member_position) VALUES ('$group_id', '$member_name', '$member_contact', '$member_residence', '$member_occupation', '$member_position')";
            $query_insert_member = mysqli_query($db_link, $sql_insert_member);
            checkSQL($db_link, $query_insert_member);
        }
    }

    $_SESSION['group_id'] = $group_id;
    header('Location: group_new_pic.php?from=new');
}

//Build new GROUP NAME
$newGroupName = 'Group '.date("YmdHis");

?>

<html>
<?PHP includeHead('New Group',0) ?>
<body>
    <!-- MENU -->
    <?PHP includeMenu(2); ?>
    <div id="menu_main">
        <a href="group_search.php">Search</a>
        <a href="group_new.php" id="item_selected">New Group</a>
        <a href="group_act.php">Active Groups</a>
        <a href="group_inact.php">Inactive Groups</a>
    </div>

    <!-- PAGE HEADING -->
    <p class="heading">New Group (<?PHP echo $newGroupName; ?>)</p>

 
    <!-- CONTENT -->
<div class="content_center">
    <form action="group_new.php" method="post">
        <table id ="tb_fields">
            <tr>
                <td>Group Name:</td>
                <td><input type="text" name="group_name" /></td>
            </tr>
            <?php for ($i = 1; $i <= 5; $i++) { ?>
            <tr>
                <td colspan="4"><b>Member <?php echo $i; ?></b></td>
            </tr>
            <tr>
                <td>Member Name:</td>
                <td><input type="text" name="member_name_<?php echo $i; ?>" /></td>
                <td>Contact:</td>
                <td><input type="text" name="member_contact_<?php echo $i; ?>" /></td>
            </tr>
            <tr>
                <td>Residence:</td>
                <td><input type="text" name="member_residence_<?php echo $i; ?>" /></td>
                <td>Occupation:</td>
                <td><input type="text" name="member_occupation_<?php echo $i; ?>" /></td>
            </tr>
            <tr>
                <td>Position:</td>
                <td><input type="text" name="member_position_<?php echo $i; ?>" /></td>
            </tr>
            <?php } ?>
            <tr>
                <td></td>
                <td><input type="submit" name="create" value="Create" /></td>
            </tr>
        </table>
    </form>
</div>