<?php
require_once 'header.php';
require_permission('orders');

// ako imamo direktno project_id → preusmjeri na detaljni prikaz
if (isset($_GET['project_id']) && (int)$_GET['project_id'] > 0 && $_SERVER['REQUEST_METHOD'] === 'GET') {
    $params = $_GET;
    $query  = http_build_query($params);
    header('Location: project_detail.php?' . $query);
    exit;
}

// lista projekata za odabir
$projects = $mysqli->query("SELECT id, name, location FROM projects ORDER BY name");
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Izvještaj po projektu</div>
        <div class="text-muted small">Odaberite projekt i razdoblje za pregled utroška</div>
    </div>
</div>

<div class="card card-soft">
    <div class="card-body card-soft-body">
        <form method="get">
            <div class="mb-3">
                <label class="form-label">Projekt</label>
                <select name="project_id" class="form-select" required>
                    <option value="">-- odaberite projekt --</option>
                    <?php while ($p = $projects->fetch_assoc()): ?>
                        <option value="<?php echo $p['id']; ?>">
                            <?php echo htmlspecialchars($p['name']); ?>
                            <?php if (!empty($p['location'])): ?>
                                (<?php echo htmlspecialchars($p['location']); ?>)
                            <?php endif; ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="row g-3">
                <div class="col-md-4 col-6">
                    <label class="form-label">Datum od</label>
                    <input type="text" name="date_from" class="form-control"
                           placeholder="dd.mm.yyyy">
                </div>
                <div class="col-md-4 col-6">
                    <label class="form-label">Datum do</label>
                    <input type="text" name="date_to" class="form-control"
                           placeholder="dd.mm.yyyy">
                </div>
                <div class="col-md-4 col-12 d-flex align-items-end">
                    <button class="btn btn-primary btn-soft w-100" type="submit">
                        Otvori izvještaj
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<?php require_once 'footer.php'; ?>
