<?php
$page_title = 'Scan Results';
require_once('includes/load.php');
page_require_level(10);

// Mengambil header layout  
include_once('layouts/header.php');

// 1. Ambil total resi
$current_date = date('Y-m-d');
$total_scan_in_query = "SELECT COUNT(*) AS total_scan_in FROM resi WHERE date(scan_in) = '{$current_date}'";
$total_scan_out_query = "SELECT COUNT(*) AS total_scan_out FROM resi WHERE date(scan_in) = '{$current_date}' AND scan_out IS NOT NULL";

$total_scan_in_result = $db->query($total_scan_in_query);
$total_scan_out_result = $db->query($total_scan_out_query);

$total_scan_in = $total_scan_in_result->fetch_assoc()['total_scan_in'];
$total_scan_status = $total_scan_out_result->fetch_assoc()['total_scan_out'];

// 2. Ambil data per lokasi
$scan_data = [];
$scan_per_location_query = "
    SELECT w.warehouse_name, 
           SUM(CASE WHEN r.scan_in IS NOT NULL THEN 1 ELSE 0 END) AS scan_in, 
           SUM(CASE WHEN r.scan_out IS NOT NULL THEN 1 ELSE 0 END) AS scan_status 
    FROM warehouse w 
    JOIN resi r ON w.warehouse_id = r.id_warehouse 
    WHERE date(r.scan_in) = '{$current_date}'
    GROUP BY w.warehouse_id
";

$scan_per_location_result = $db->query($scan_per_location_query);

while ($row = $scan_per_location_result->fetch_assoc()) {
    $scan_data[$row['warehouse_name']] = [
        'scan_in' => $row['scan_in'],
        'scan_status' => $row['scan_status']
    ];
}

$scan_per_location_picker_query = "
    SELECT w.warehouse_name, 
           r.id_packer, 
           COUNT(r.id) AS count 
    FROM warehouse w 
    LEFT JOIN resi r ON w.warehouse_id = r.id_warehouse 
    WHERE date(r.scan_out) = '{$current_date}'
    GROUP BY w.warehouse_name, r.id_packer
";

$scan_per_location_picker_query_result = $db->query($scan_per_location_picker_query);

// Mengelompokkan data berdasarkan warehouse_name
$scan_data_grouped = [];
while ($row = $scan_per_location_picker_query_result->fetch_assoc()) {
    $scan_data_grouped[$row['warehouse_name']][] = [
        'id_packer' => $row['id_packer'],
        'count' => $row['count']
    ];
}

// 3. Hitung persentase progres
$total_resi = $total_scan_in + $total_scan_status;
$progress_percentage = $total_resi > 0 ? ($total_scan_in / $total_resi) * 100 : 0;
?>

<!-- Container utama -->
<div class="scoreboard-container">
    <div class="scoreboard-title">Real-time Scan Results</div>
    <div id="realtimeResults">
        <!-- Progress Bar -->
        <div class="progress-container">
            <div class="progress-bar" style="width: <?php echo round($progress_percentage, 2); ?>%;"></div>
        </div>
        <div class="progress-text">Progress: <?php echo round($progress_percentage, 2); ?>%</div>
        <!-- Hasil scan keseluruhan -->
        <div class="scan-section">
            <h3>Overall</h3>
            <table class="scoreboard-table">
                <thead>
                    <tr>
                        <th>Type</th>
                        <th>Total Resi</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>Scanned In</td>
                        <td><?php echo $total_scan_in; ?></td>
                    </tr>
                    <tr>
                        <td>Scan Out</td>
                        <td><?php echo $total_scan_status; ?></td>
                    </tr>
                </tbody>
            </table>
        </div>

        <!-- Hasil scan per lokasi -->
        <div class="scan-section">
            <h3>Scan Results per Location</h3>
            <table class="scoreboard-table">
                <thead>
                    <tr>
                        <th>Location</th>
                        <th>Scanned In</th>
                        <th>Scan Out</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($scan_data as $location_name => $data): ?>
                        <tr>
                            <td><?php echo $location_name; ?></td>
                            <td><?php echo $data['scan_in']; ?></td>
                            <td><?php echo $data['scan_status']; ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="scan-section">
            <h3>Scan Results per Location, Packer Number</h3>
            <table class="scoreboard-table">
                <thead>
                    <tr>
                        <th>Location</th>
                        <th>Packer Number</th>
                        <th>Total Orders</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($scan_data_grouped as $warehouse_name => $packers): ?>
                        <tr>
                            <td rowspan="<?php echo count($packers); ?>"><?php echo $warehouse_name; ?></td>
                            <td><?php echo $packers[0]['id_packer']; ?></td>
                            <td><?php echo $packers[0]['count']; ?></td>
                        </tr>
                        <?php for ($i = 1; $i < count($packers); $i++): ?>
                            <tr>
                                <td><?php echo $packers[$i]['id_packer']; ?></td>
                                <td><?php echo $packers[$i]['count']; ?></td>
                            </tr>
                        <?php endfor; ?>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>



    </div>
</div>

<?php include_once('layouts/footer.php'); ?>


<!-- CSS Styling -->
<style>
    /* Overall container styling */
    .scoreboard-container {
        max-width: 800px;
        margin: 0 auto;
        text-align: center;
        background: linear-gradient(135deg, #3a3f58, #23293b);
        padding: 30px;
        border-radius: 15px;
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
        color: #fff;
        font-family: 'Poppins', sans-serif;
    }

    /* Title Styling */
    .scoreboard-title {
        font-size: 30px;
        margin-bottom: 25px;
        font-weight: 700;
        color: #e0e0e0;
    }

    /* Table styling */
    .scoreboard-table {
        width: 100%;
        margin-bottom: 35px;
        border-collapse: collapse;
        background-color: #2e3b4e;
        border-radius: 10px;
        overflow: hidden;
    }

    .scoreboard-table th,
    .scoreboard-table td {
        padding: 15px;
        border: 1px solid #555;
        text-align: center;
        font-size: 16px;
        color: #f5f5f5;
    }

    .scoreboard-table th {
        background-color: #1f2a3a;
        font-weight: 600;
    }

    .scoreboard-table tbody tr:nth-child(even) {
        background-color: #3f4d63;
    }

    .scoreboard-table tbody tr:hover {
        background-color: #5a6b82;
        transition: background-color 0.3s;
    }

    /* Progress bar container styling */
    .progress-container {
        background-color: #6a7b8c;
        width: 100%;
        height: 25px;
        border-radius: 12px;
        margin: 25px 0;
        box-shadow: inset 0 3px 5px rgba(0, 0, 0, 0.3);
    }

    /* Progress bar styling */
    .progress-bar {
        background-color: #5cb85c;
        height: 100%;
        text-align: center;
        line-height: 25px;
        color: white;
        border-radius: 12px;
        transition: width 0.4s ease;
    }

    /* Progress text styling */
    .progress-text {
        margin-top: 12px;
        font-size: 18px;
        font-weight: 500;
        color: #d0d0d0;
    }

    /* Section styling */
    .scan-section {
        margin-bottom: 45px;
    }

    /* Ensure table and container fit inside screen */
    @media (max-width: 768px) {
        .scoreboard-container {
            width: 95%;
        }

        .scoreboard-table th,
        .scoreboard-table td {
            font-size: 14px;
        }
    }
</style>

<!-- Link Google Fonts -->
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap" rel="stylesheet">