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

// filter projekti
$projRes = $mysqli->query('SELECT id, name FROM projects ORDER BY name');

$project_filter = isset($_GET['project_id']) ? (int)$_GET['project_id'] : 0;
$date_from_display = trim($_GET['date_from'] ?? '');
$date_to_display   = trim($_GET['date_to'] ?? '');

$date_from_sql = '';
$date_to_sql   = '';

if ($date_from_display !== '') {
    $dt = DateTime::createFromFormat('d.m.Y', $date_from_display);
    if ($dt) $date_from_sql = $dt->format('Y-m-d');
}
if ($date_to_display !== '') {
    $dt = DateTime::createFromFormat('d.m.Y', $date_to_display);
    if ($dt) $date_to_sql = $dt->format('Y-m-d');
}

$where = [];
if ($project_filter > 0) {
    $where[] = 'o.project_id = ' . $project_filter;
}
if ($date_from_sql !== '') {
    $where[] = "o.usage_date >= '" . $mysqli->real_escape_string($date_from_sql) . "'";
}
if ($date_to_sql !== '') {
    $where[] = "o.usage_date <= '" . $mysqli->real_escape_string($date_to_sql) . "'";
}
$where_sql = $where ? 'WHERE ' . implode(' AND ', $where) : '';

$sql = "SELECT o.id, o.usage_date, o.note, p.name AS project_name, p.project_status
        FROM consumption_orders o
        JOIN projects p ON p.id = o.project_id
        $where_sql
        ORDER BY o.usage_date DESC, o.id DESC";
$orders = $mysqli->query($sql);

$query_params = [
    'project_id' => $project_filter,
    'date_from'  => $date_from_display,
    'date_to'    => $date_to_display
];
$export_query = http_build_query($query_params);
?>
<div class="row g-3 mb-3 align-items-center">
    <div class="col">
        <div class="section-title">Nalozi utroška</div>
        <div class="text-muted small">Pregled i filtriranje po projektu i datumu</div>
    </div>
    <div class="col-auto d-flex flex-wrap gap-2">
        <a href="order_new.php" class="btn btn-primary btn-soft btn-sm">+ Novi nalog</a>
        <a href="export_orders_excel.php?<?php echo $export_query; ?>"
           class="btn btn-outline-success btn-soft btn-sm">
            Export u Excel
        </a>
    </div>
</div>

<div class="card card-soft mb-3">
    <div class="card-header card-soft-header">
        <span class="fw-semibold">Filter</span>
    </div>
    <div class="card-body card-soft-body">
        <form method="get" class="row g-3">
            <div class="col-md-4 col-12">
                <label class="form-label">Projekt</label>
                <select name="project_id" class="form-select form-select-sm">
                    <option value="0">-- svi projekti --</option>
                    <?php
                    $projRes->data_seek(0);
                    while ($p = $projRes->fetch_assoc()):
                        $sel = ($project_filter == (int)$p['id']) ? 'selected' : '';
                    ?>
                        <option value="<?php echo $p['id']; ?>" <?php echo $sel; ?>>
                            <?php echo htmlspecialchars($p['name']); ?>
                        </option>
                    <?php endwhile; ?>
                </select>
            </div>
            <div class="col-md-3 col-6">
                <label class="form-label">Datum od</label>
                <input type="text" name="date_from" class="form-control form-control-sm"
                       placeholder="dd.mm.yyyy"
                       value="<?php echo htmlspecialchars($date_from_display); ?>">
            </div>
            <div class="col-md-3 col-6">
                <label class="form-label">Datum do</label>
                <input type="text" name="date_to" class="form-control form-control-sm"
                       placeholder="dd.mm.yyyy"
                       value="<?php echo htmlspecialchars($date_to_display); ?>">
            </div>
            <div class="col-md-2 col-12 d-flex align-items-end">
                <button class="btn btn-outline-primary btn-soft w-100 btn-sm" type="submit">Primijeni</button>
            </div>
        </form>
    </div>
</div>

<div class="card card-soft">
    <div class="card-header card-soft-header d-flex justify-content-between align-items-center">
        <span class="fw-semibold">Popis naloga</span>
        <span class="pill-counter d-none d-sm-inline">
            <?php echo (int)$orders->num_rows; ?> naloga
        </span>
    </div>
    <div class="card-body card-soft-body">

        <div class="table-responsive d-none d-md-block">
            <table class="table table-sm align-middle">
                <thead class="small text-muted">
                <tr>
                    <th>#</th>
                    <th>Projekt</th>
                    <th>Datum</th>
                    <th>Napomena</th>
                    <th class="text-end">Akcije</th>
                </tr>
                </thead>
                <tbody>
                <?php while ($o = $orders->fetch_assoc()):
                    $dt = DateTime::createFromFormat('Y-m-d', $o['usage_date']);
                    $date_hr = $dt ? $dt->format('d.m.Y') : $o['usage_date'];
                ?>
                    <tr>
                        <td><?php echo $o['id']; ?></td>
                        <td><?php echo htmlspecialchars($o['project_name']); ?></td>
                        <td><?php echo $date_hr; ?></td>
                        <td><?php echo htmlspecialchars($o['note']); ?></td>
                        <td class="text-end">
                            <a href="order_view.php?id=<?php echo $o['id']; ?>" class="btn btn-sm btn-outline-info btn-soft">Pregled</a>
                            <?php if (($o['project_status'] ?? 'open') !== 'closed'): ?>
                                <a href="order_edit.php?id=<?php echo $o['id']; ?>" class="btn btn-sm btn-outline-primary btn-soft">Uredi</a>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endwhile; ?>
                </tbody>
            </table>
        </div>

        <div class="d-md-none">
            <?php
            $orders->data_seek(0);
            if ($orders->num_rows === 0): ?>
                <div class="text-muted small">Nema naloga.</div>
            <?php endif; ?>
            <?php while ($o = $orders->fetch_assoc()):
                $dt = DateTime::createFromFormat('Y-m-d', $o['usage_date']);
                $date_hr = $dt ? $dt->format('d.m.Y') : $o['usage_date'];
            ?>
                <div class="list-card">
                    <div class="list-card-title">
                        <?php echo htmlspecialchars($o['project_name']); ?>
                    </div>
                    <div class="list-card-sub">
                        Datum: <strong><?php echo $date_hr; ?></strong> • Nalog #<?php echo $o['id']; ?>
                    </div>
                    <div class="list-card-meta">
                        <?php echo htmlspecialchars($o['note']); ?>
                    </div>
                    <div class="d-flex justify-content-end gap-2 mt-2">
                        <a href="order_view.php?id=<?php echo $o['id']; ?>" class="btn btn-outline-info btn-sm">Pregled</a>
                        <?php if (($o['project_status'] ?? 'open') !== 'closed'): ?>
                            <a href="order_edit.php?id=<?php echo $o['id']; ?>" class="btn btn-outline-primary btn-sm">Uredi</a>
                        <?php endif; ?>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>

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