<?php
require_once __DIR__ . '/../../app/core/bootstrap.php';
require_once __DIR__ . '/../../app/core/project_permissions.php';
require_once __DIR__ . '/../../app/core/i18n.php';
auth_require_login();

$pdo = crm_pdo();
$page_title = 'Warehouse Dashboard';

// Warehouse dashboard only for roles that can manage projects or secretary
$role = gp_user_role($pdo);
if (!gp_is_full_project_access_role($role) && !gp_is_secretary_like_role($role)) {
    die('Access denied.');
}

// Get ??a ta ???a with equipment
$sql = "
    SELECT p.id, p.code, p.title, c.name as customer_name, COUNT(pe.id) as item_count
    FROM projects p
    JOIN project_equipment pe ON pe.project_id = p.id
    LEFT JOIN customers c ON c.id = p.customer_id
    WHERE p.status NOT IN ('completed', 'cancelled')
    GROUP BY p.id
    ORDER BY p.id DESC
";
$rows = $pdo->query($sql)->fetchAll();

include __DIR__ . '/../includes/header.php';
?>
<div class="page-header">
  <div class="page-header-title">
    <h1>📦 Warehouse Dashboard</h1>
    <span>Prepare equipment and orders for projects</span>
  </div>
</div>

<div class="grid-2">
    <?php foreach ($rows as $r): ?>
    <div class="card" style="display:flex; flex-direction:column; gap:16px;">
        <div style="display:flex; justify-content:space-between; align-items:center;">
            <div>
                <h3 style="margin:0;"><?php echo h($r['title']); ?></h3>
                <span class="small" style="color:var(--gp-muted);"><?php echo h($r['code'] . ' · ' . $r['customer_name']); ?></span>
            </div>
            <div class="badge"><?php echo $r['item_count']; ?> Items</div>
        </div>
        
        <div class="table-responsive">
            <table class="table" style="font-size:0.85rem;">
                <thead><tr><th>Item</th><th>Qty</th><th>Notes</th></tr></thead>
                <tbody>
                    <?php
                    $stmtE = $pdo->prepare("SELECT item_name, quantity, notes FROM project_equipment WHERE project_id = ?");
                    $stmtE->execute([$r['id']]);
                    foreach ($stmtE->fetchAll() as $item): ?>
                    <tr>
                        <td><?php echo h($item['item_name']); ?></td>
                        <td><?php echo h($item['quantity']); ?></td>
                        <td class="small"><?php echo h($item['notes'] ?? '-'); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        
        <div style="display:flex; gap:12px; margin-top:auto; padding-top:16px; border-top:1px solid var(--gp-card-border);">
            <a href="/gopools/public/api/qr.php?text=<?php echo urlencode($r['code']); ?>" target="_blank" class="btn btn-sm btn-outline-light" style="flex:1;">Print Label (QR)</a>
            <a href="/gopools/public/projects/equipment.php?project_id=<?php echo (int)$r['id']; ?>" class="btn btn-sm btn-primary" style="flex:1;">Manage</a>
        </div>
    </div>
    <?php endforeach; ?>
    
    <?php if (empty($rows)): ?>
    <div class="card" style="grid-column: 1/-1; text-align:center; padding:40px;">
        <p>No active projects with equipment found.</p>
    </div>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../includes/footer.php'; ?>
