<?php
include 'config.php';

// Preuzimanje filtera iz GET parametara
$dateFrom = isset($_GET['date_from']) ? $_GET['date_from'] : '';
$dateTo   = isset($_GET['date_to']) ? $_GET['date_to'] : '';

// Sastavljanje WHERE dijela – filtriramo samo dokumente tipa OTPREMNICA
$whereClauses = [];
$whereClauses[] = "dt.type_code = 'OTPREMNICA'";
if ($dateFrom && $dateTo) {
    $whereClauses[] = "dh.doc_date BETWEEN '$dateFrom' AND '$dateTo'";
} elseif ($dateFrom) {
    $whereClauses[] = "dh.doc_date >= '$dateFrom'";
} elseif ($dateTo) {
    $whereClauses[] = "dh.doc_date <= '$dateTo'";
}
$whereSQL = count($whereClauses) > 0 ? "WHERE " . implode(" AND ", $whereClauses) : "";

// Upit za dohvat otpremnica – priključujemo i klijente radi eventualnog prikaza imena
$query = "SELECT dh.id, dh.doc_date, dh.doc_number, k.naziv_klijenta, dh.total_gross, dh.doc_status 
          FROM document_header dh
          JOIN document_type dt ON dh.document_type_id = dt.id
          LEFT JOIN klijenti k ON dh.client_id = k.sifra
          $whereSQL
          ORDER BY dh.doc_date DESC";

$result = $conn->query($query);
if (!$result) {
    die("Greška u upitu: " . $conn->error);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Izvještaj za otpremnice</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .container { max-width: 1000px; margin: 0 auto; }
        h2 { text-align: center; }
        form { margin-bottom: 20px; }
        label { margin-right: 10px; }
        input[type="date"] { padding: 5px; margin-right: 10px; }
        input[type="submit"], input[type="button"] { padding: 5px 10px; margin-right: 10px; }
        table { border-collapse: collapse; width: 100%; margin-top: 20px; }
        th, td { border: 1px solid #ccc; padding: 8px; text-align: center; }
        th { background-color: #eee; }
        .no-print { margin: 10px 0; }
        @media print {
            .no-print { display: none; }
        }
    </style>
    <script>
        function resetFilters() {
            window.location.href = 'izvjestaj_otpremnice.php';
        }
        function printPage() {
            window.print();
        }
    </script>
</head>
<body>
<div class="container">
    <h2>Izvještaj za otpremnice</h2>
    <div class="no-print">
        <form method="GET" action="">
            <label for="date_from">Datum od:</label>
            <input type="date" name="date_from" id="date_from" value="<?php echo htmlspecialchars($dateFrom); ?>">
            <label for="date_to">Datum do:</label>
            <input type="date" name="date_to" id="date_to" value="<?php echo htmlspecialchars($dateTo); ?>">
            <input type="submit" value="Filtriraj">
            <input type="button" value="Reset filtera" onclick="resetFilters()">
        </form>
        <button class="no-print" onclick="printPage()">Štampaj</button>
    </div>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Datum</th>
                <th>Broj otpremnice</th>
                <th>Klijent</th>
                <th>Iznos</th>
                <th>Status</th>
            </tr>
        </thead>
        <tbody>
            <?php if ($result->num_rows > 0): ?>
                <?php while ($row = $result->fetch_assoc()): ?>
                    <?php 
                    // Formatiramo datum u dd.mm.yyyy
                    $formattedDate = date("d.m.Y", strtotime($row['doc_date']));
                    ?>
                    <tr>
                        <td><?php echo $row['id']; ?></td>
                        <td><?php echo $formattedDate; ?></td>
                        <td><?php echo htmlspecialchars($row['doc_number']); ?></td>
                        <td><?php echo htmlspecialchars($row['naziv_klijenta']); ?></td>
                        <td><?php echo number_format($row['total_gross'], 2); ?></td>
                        <td><?php echo htmlspecialchars($row['doc_status']); ?></td>
                    </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr><td colspan="6">Nema podataka za odabrane filtere.</td></tr>
            <?php endif; ?>
        </tbody>
    </table>
</div>
</body>
</html>
