<?php
require_once('includes/load.php');
page_require_level(3);

// 1. Check picking_no parameter
if(!isset($_GET['picking_no'])) {
    $session->msg("d", "Tidak ada picking list yang dipilih");
    redirect('picking_list.php');
}

// 2. Get and clean picking_no data
$picking_nos = is_array($_GET['picking_no']) ? $_GET['picking_no'] : [$_GET['picking_no']];
$clean_picking_nos = array_map([$db, 'escape'], $picking_nos);
$picking_list = "'" . implode("','", $clean_picking_nos) . "'";

// 3. Query to get material data
$sql = "SELECT 
          m.component as part_number,
          p.name as part_name,
          p.uom,
          SUM(m.quantity_needed) as total_qty,
          GROUP_CONCAT(DISTINCT m.picking_no ORDER BY m.picking_no SEPARATOR ', ') as picking_sources
        FROM material_forecasts m
        JOIN products p ON m.component = p.part_number
        WHERE m.picking_no IN ($picking_list)
        GROUP BY m.component
        ORDER BY p.name";

$result = $db->query($sql);

if(!$result) {
    $session->msg("d", "Error query database: " . $db->get_last_error());
    redirect('picking_list.php');
}

$total_items = $db->num_rows($result);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Print Summary Picking List</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            margin: 0; 
            padding: 15px; 
            font-size: 12px; 
        }
        table { 
            width: 100%; 
            border-collapse: collapse; 
            margin-bottom: 15px; 
        }
        th, td { 
            border: 1px solid #ddd; 
            padding: 6px; 
            text-align: left; 
        }
        th { 
            background-color: #f2f2f2; 
            font-weight: bold;
        }
        .text-right { 
            text-align: right; 
        }
        .text-center { 
            text-align: center; 
        }
        .header { 
            text-align: center; 
            margin-bottom: 15px; 
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }
        .footer { 
            margin-top: 20px; 
            font-size: 11px; 
            text-align: center; 
            color: #777;
        }
        @media print {
            .no-print { 
                display: none; 
            }
            body { 
                padding: 0; 
                font-size: 11px;
            }
        }
    </style>
</head>
<body>
    <!-- Document Header -->
    <div class="header">
        <h2>SUMMARY PICKING LIST</h2>
        <p>Printed on: <?php echo date('d/m/Y H:i:s'); ?></p>
    </div>

    <!-- Basic information -->
    <div style="margin-bottom: 10px;">
        <p><strong>Picking List:</strong> <?php echo implode(', ', $clean_picking_nos); ?></p>
        <p><strong>Total Items:</strong> <?php echo $total_items; ?></p>
    </div>
    
    <!-- Material table -->
    <table>
        <thead>
            <tr>
                <th width="5%">No</th>
                <th width="20%">Part Number</th>
                <th width="35%">Material Name</th>
                <th width="15%">Unit</th>
                <th width="25%" class="text-right">Total Qty</th>
            </tr>
        </thead>
        <tbody>
            <?php if($total_items > 0): ?>
                <?php $i = 1; while($row = $db->fetch_assoc($result)): ?>
                <tr>
                    <td class="text-center"><?php echo $i++; ?></td>
                    <td><?php echo $row['part_number']; ?></td>
                    <td><?php echo $row['part_name']; ?></td>
                    <td class="text-center"><?php echo $row['uom']; ?></td>
                    <td class="text-right"><?php echo number_format($row['total_qty'], 2); ?></td>
                </tr>
                <?php endwhile; ?>
            <?php else: ?>
                <tr>
                    <td colspan="5" class="text-center">No material data found</td>
                </tr>
            <?php endif; ?>
        </tbody>
    </table>
    
    <!-- Footer -->
    <div class="footer">
        <p>Dokumen ini dicetak secara otomatis oleh sistem</p>
    </div>
    
    <!-- Print controls -->
    <div class="no-print" style="margin-top: 20px; text-align: center;">
        <button onclick="window.print()" style="padding: 8px 15px; background: #337ab7; color: white; border: none; border-radius: 4px; cursor: pointer;">
            Print Document
        </button>
        <button onclick="window.close()" style="padding: 8px 15px; background: #6c757d; color: white; border: none; border-radius: 4px; cursor: pointer; margin-left: 10px;">
            Close
        </button>
    </div>
    
    <script>
        // Auto-print when page loads (optional)
        window.onload = function() {
            setTimeout(function() {
                window.print();
            }, 500);
        };
    </script>
</body>
</html>