<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require 'functions.php';
require 'function_pic.php';
checkLogin();
$db_link = connect();

// Check if group_id is set
if (!isset($_SESSION['group_id'])) {
    echo "Error: group_id is not set.";
    exit;
}

// Rest of the script...

//SKIP-Button
if (isset($_POST['skip'])){
    if ($from == "new") 
        header('Location: group_act.php?group='.$_SESSION['group_id']);
    else 
        header('Location: group.php?group='.$_SESSION['group_id']);
}

//UPLOAD-Button
if (isset($_POST['upload']) AND isset($_FILES['image'])){
    //Settings
    $max_file_size = 1024*2048; // 2048kb
    $valid_exts = array('jpeg', 'jpg', 'png', 'tif', 'tiff');
    $path = 'uploads/photos/groups/group'.$_SESSION['group_id'].'_';

    //Thumbnail Sizes
    $sizes = array(100 => 130, 146 => 190, 230 => 300);

    //Check for maximum file size
    if ($_FILES['image']['size'] < $max_file_size) {
        // Get file extension
        $ext = strtolower(pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION));
        if (in_array($ext, $valid_exts)) {
            $files = array();
            foreach ($sizes as $width => $height) {
                $files[] = resizeImage($width, $height, $path);
            }

            if (!empty($files)) {
                $sql_picpath = "UPDATE `group` SET group_pic = '$files[1]' WHERE group_id = '$_SESSION[group_id]'";
                $query_picpath = mysqli_query($db_link, $sql_picpath);
                checkSQL($db_link, $query_picpath);

                if ($from == "new") {
                    header('Location: group_act.php?group='.$_SESSION['group_id']);
                } else {
                    header('Location: group.php?group='.$_SESSION['group_id']);
                }
            } else {
                $error_msg = 'Failed to resize image.';
            }
        } else {
            $error_msg = 'Unsupported file type.';
        }
    } else {
        $error_msg = 'Please choose an image smaller than 2048kB.';
    }
}



$result_group = getGroup($db_link, $_SESSION['group_id']);
?>

<html>
<?PHP includeHead('New Picture Upload',1) ?>
<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>
    <div class="content_center">
        <p class="heading">Upload Photo for <?PHP echo $result_group['group_name']; ?></p>
        <?php if(isset($error_msg)): ?>
        <p class="alert"><?php echo $error_msg; ?></p>
        <?php endif ?>
        <!-- File uploading form -->
        <form action="" method="post" enctype="multipart/form-data">
            <label for="image" class="file-upload">
                <i class="fa fa-image"></i> Choose image
            </label>
            <input type="file" name="image" id="image" accept="image/*" />
            <br/>
            <input type="submit" name="upload" value="Upload" />
            <input type="submit" name="skip" value="Skip" />
        </form>
    </div>
</body>
</html>
