<?php

if ( !$user['superAdmin'] ) {
    if ( !$exists = $db->cell("SELECT id FROM client_project WHERE clientId = ? AND projectId = ?", $user['client']['id'], $_POST['projectId']) ) {
        session_start();
        $_SESSION['error'] = 'You do not have permission to generate this PDF';

        header('Location: '.URL_APP.'/selections/?projectId='.$_POST['projectId']);
        exit;
    }
}

$project = $db->row("SELECT * FROM project WHERE id = ?", $_POST['projectId']);

$in = implode(',', $_POST['groupCheck']);
$groups = $db->run("SELECT * FROM selection_group WHERE id IN(".$in.") ORDER BY sort ASC");

$i = 0;
foreach ( $groups as $group ) {
    $groups[$i]['selections'] = $db->run("SELECT * FROM selection_item WHERE groupId = ? ORDER BY sort ASC", $group['id']);
    $groups[$i]['sum'] = $db->cell("SELECT SUM(quantity * cost) FROM selection_item WHERE groupId = ?", $group['id']);

    for ( $k = 0; $k < count($groups[$i]['selections']); $k++ ) {
        $groups[$i]['selections'][$k]['tags'] = $db->run("SELECT title FROM selection_item_tag, selection_tag WHERE selection_item_tag.selectionItemId = ? AND selection_item_tag.tagId = selection_tag.id", $groups[$i]['selections'][$k]['id']);
    }

    $i++;
}
?>

<!-- <pre>
<?php //print_r($_POST); ?>
<?php //print_r($groups); ?>
</pre> -->

<?php include '../template/generate-pdf.php'; ?>