<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Stock Movement Report - <?php echo htmlspecialchars($restaurant['name'] ?? 'Restaurant'); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Inter', sans-serif;
            color: #1e293b;
            background: #ffffff;
            margin: 0;
            padding: 40px;
            font-size: 12px;
            line-height: 1.5;
        }
        
        /* Letterhead Header */
        .letterhead {
            border-bottom: 2px solid #e2e8f0;
            padding-bottom: 20px;
            margin-bottom: 25px;
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
        }
        .restaurant-name {
            font-size: 20px;
            font-weight: 800;
            color: #0f172a;
            margin: 0 0 5px 0;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .restaurant-sub {
            font-size: 11px;
            color: #64748b;
            margin: 0;
        }
        .report-title {
            text-align: right;
        }
        .report-name {
            font-size: 16px;
            font-weight: 700;
            color: #0f172a;
            margin: 0 0 5px 0;
            letter-spacing: -0.2px;
        }
        .report-date {
            font-size: 10px;
            color: #64748b;
            font-weight: 500;
        }
        
        /* KPIs Row */
        .kpis-row {
            display: flex;
            gap: 15px;
            margin-bottom: 30px;
        }
        .kpi-card {
            flex: 1;
            padding: 15px;
            background: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
        }
        .kpi-lbl {
            font-size: 10px;
            font-weight: 600;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin-bottom: 5px;
        }
        .kpi-val {
            font-size: 18px;
            font-weight: 700;
            color: #0f172a;
            margin-bottom: 2px;
        }
        .kpi-sub {
            font-size: 9px;
            color: #94a3b8;
        }
        
        /* Tables and Sections */
        .section-title {
            font-size: 12px;
            font-weight: 700;
            color: #0f172a;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            margin: 25px 0 10px 0;
            border-bottom: 1px solid #e2e8f0;
            padding-bottom: 5px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        th {
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            letter-spacing: 0.5px;
            background: #f1f5f9;
            color: #475569;
            padding: 8px 10px;
            border-bottom: 1.5px solid #cbd5e1;
            text-align: left;
        }
        td {
            padding: 8px 10px;
            border-bottom: 1px solid #e2e8f0;
            color: #334155;
            vertical-align: top;
        }
        
        /* Valuation table column widths */
        .col-qty { text-align: right; width: 80px; }
        .col-val { text-align: right; width: 120px; }
        .col-type { font-weight: 600; }
        
        /* Print rules */
        @media print {
            body {
                padding: 0;
                font-size: 11px;
            }
            .no-print {
                display: none !important;
            }
            .page-break {
                page-break-before: always;
            }
            tr {
                page-break-inside: avoid;
            }
        }
        
        .print-btn-container {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 20px;
        }
        .print-btn {
            background: #10b981;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 6px;
            font-size: 12px;
            font-weight: 600;
            cursor: pointer;
            display: inline-flex;
            align-items: center;
            gap: 6px;
        }
        .print-btn:hover {
            opacity: 0.9;
        }
    </style>
</head>
<body>

    <!-- Print Button (visible on screen only) -->
    <div class="print-btn-container no-print">
        <button class="print-btn" onclick="window.print()">
            Print / Save PDF
        </button>
    </div>

    <!-- Letterhead -->
    <div class="letterhead">
        <div>
            <h1 class="restaurant-name"><?php echo htmlspecialchars($restaurant['name'] ?? 'Restaurant POS'); ?></h1>
            <p class="restaurant-sub">
                <?php if ($cashierName): ?>
                    Branch Context: <strong><?php echo htmlspecialchars($cashierName); ?></strong>
                <?php else: ?>
                    All Branches / Combined Reports
                <?php endif; ?>
            </p>
        </div>
        <div class="report-title">
            <h2 class="report-name">Stock Movement &amp; Purchase Report</h2>
            <p class="report-date">
                Period: <?php echo date('M d, Y', strtotime($report['from'])); ?> — <?php echo date('M d, Y', strtotime($report['to'])); ?><br>
                Generated: <?php echo date('Y-m-d H:i'); ?>
            </p>
        </div>
    </div>

    <!-- KPI Cards -->
    <div class="kpis-row">
        <div class="kpi-card">
            <div class="kpi-lbl">Stock Purchased (In)</div>
            <div class="kpi-val">৳ <?php echo number_format($report['kpis']['stock_in'], 2); ?></div>
            <div class="kpi-sub"><?php echo $report['kpiCounts']['stock_in']; ?> inbound logs</div>
        </div>
        <div class="kpi-card">
            <div class="kpi-lbl">Stock Usage (Out)</div>
            <div class="kpi-val">৳ <?php echo number_format($report['kpis']['stock_out'], 2); ?></div>
            <div class="kpi-sub"><?php echo $report['kpiCounts']['stock_out']; ?> usage logs</div>
        </div>
        <div class="kpi-card">
            <div class="kpi-lbl">Stock Waste / Loss</div>
            <div class="kpi-val">৳ <?php echo number_format($report['kpis']['waste'], 2); ?></div>
            <div class="kpi-sub"><?php echo $report['kpiCounts']['waste']; ?> waste logs</div>
        </div>
    </div>

    <!-- Category Valuation -->
    <div class="section-title">Valuation by Category</div>
    <?php if (empty($report['categoryBreakdown'])): ?>
        <p>No category movements in this period.</p>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Category</th>
                    <th style="text-align: right">Stock In Value</th>
                    <th style="text-align: right">Stock Out Value</th>
                    <th style="text-align: right">Waste Value</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($report['categoryBreakdown'] as $cat => $vals): ?>
                <tr>
                    <td style="font-weight: 600;"><?php echo htmlspecialchars($cat); ?></td>
                    <td style="text-align: right">৳ <?php echo number_format($vals['stock_in'], 2); ?></td>
                    <td style="text-align: right">৳ <?php echo number_format($vals['stock_out'], 2); ?></td>
                    <td style="text-align: right; color: #ea580c">৳ <?php echo number_format($vals['waste'], 2); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <!-- Supplier Valuation -->
    <div class="section-title">Purchases by Supplier</div>
    <?php if (empty($report['supplierPurchases'])): ?>
        <p>No supplier purchases recorded in this period.</p>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th>Supplier Name</th>
                    <th class="col-qty">Quantity Received</th>
                    <th class="col-qty">Delivery Logs</th>
                    <th class="col-val">Total Value</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($report['supplierPurchases'] as $sup): ?>
                <tr>
                    <td style="font-weight: 600;"><?php echo htmlspecialchars($sup['supplier_name']); ?></td>
                    <td class="col-qty"><?php echo number_format($sup['total_qty'], 2); ?></td>
                    <td class="col-qty"><?php echo $sup['purchases_count']; ?></td>
                    <td class="col-val" style="font-weight: bold;">৳ <?php echo number_format($sup['total_value'], 2); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <div class="page-break"></div>

    <!-- Movement Ledger -->
    <div class="section-title" style="margin-top: 0;">Movement Ledger Details</div>
    <?php if (empty($report['logs'])): ?>
        <p>No transactions found for this period.</p>
    <?php else: ?>
        <table>
            <thead>
                <tr>
                    <th style="width: 110px">Date / Time</th>
                    <th>Stock Item</th>
                    <th style="width: 80px">Action</th>
                    <th style="text-align: right; width: 70px">Qty Change</th>
                    <th style="text-align: right; width: 90px">Txn Value</th>
                    <th>Supplier / Reference / Notes</th>
                    <th style="width: 100px">User</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($report['logs'] as $l):
                    $txnValue = abs((float)$l['quantity_change']) * (float)($l['unit_price'] ?? 0);
                    
                    $lblText = $l['type'];
                    if ($l['type'] === 'stock_in') $lblText = 'Stock In';
                    elseif ($l['type'] === 'stock_out') $lblText = 'Stock Out';
                    elseif ($l['type'] === 'waste') $lblText = 'Waste';
                    elseif ($l['type'] === 'adjustment') $lblText = 'Adjustment';
                    elseif ($l['type'] === 'initial') $lblText = 'Initial Stock';
                ?>
                <tr>
                    <td style="color: #64748b;"><?php echo date('Y-m-d H:i', strtotime($l['created_at'])); ?></td>
                    <td style="font-weight: 500;"><?php echo htmlspecialchars($l['item_name']); ?></td>
                    <td class="col-type"><?php echo $lblText; ?></td>
                    <td style="text-align: right; font-weight: 600; color: <?php echo (float)$l['quantity_change'] >= 0 ? '#10b981' : '#ef4444'; ?>">
                        <?php echo (float)$l['quantity_change'] >= 0 ? '+' : ''; ?><?php echo rtrim(rtrim(number_format($l['quantity_change'], 2), '0'), '.'); ?>
                        <span style="font-size: 8px; font-weight: normal; color: #94a3b8"><?php echo htmlspecialchars($l['item_unit']); ?></span>
                    </td>
                    <td style="text-align: right; font-weight: 500;">
                        <?php if ($txnValue > 0): ?>
                            ৳ <?php echo number_format($txnValue, 2); ?>
                        <?php else: ?>
                            —
                        <?php endif; ?>
                    </td>
                    <td>
                        <span style="font-size: 10px; color: #475569;">
                            <?php if ($l['supplier_name']): ?>
                                [Supplier: <?php echo htmlspecialchars($l['supplier_name']); ?>] 
                            <?php endif; ?>
                            <?php if ($l['reference']): ?>
                                (Ref: <?php echo htmlspecialchars($l['reference']); ?>) 
                            <?php endif; ?>
                            <?php if ($l['notes']): ?>
                                <?php echo htmlspecialchars($l['notes']); ?>
                            <?php endif; ?>
                            <?php if (!$l['supplier_name'] && !$l['reference'] && !$l['notes']): ?>
                                —
                            <?php endif; ?>
                        </span>
                    </td>
                    <td style="color: #64748b;"><?php echo htmlspecialchars($l['user_name'] ?? 'System'); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>

    <!-- Self-executing print prompt -->
    <script>
        window.onload = function() {
            setTimeout(function() {
                window.print();
            }, 600);
        }
    </script>
</body>
</html>
