<?php
session_start();
require_once("../config/config.php");

$allowed_roles = ['store manager', 'admin', 'project manager'];
if (!isset($_SESSION['user_id']) || !in_array($_SESSION['role'], $allowed_roles)) {
    header("Location: ../login.php");
    exit();
}

$user_id = $_SESSION['user_id'];

// Fetch Warehouse GRNs (especially from Return Challan)
$grns = $conn->query("
    SELECT g.*, 
           u.full_name as received_by_name,
           p.project_name, p.project_code,
           rc.rc_code as return_challan_code
    FROM goods_received_note_warehouse g
    LEFT JOIN users u ON g.received_by = u.id
    LEFT JOIN project_master p ON g.project_id = p.id
    LEFT JOIN return_challan rc ON g.source_id = rc.id AND g.source_type = 'RC'
    WHERE g.source_type = 'RC' OR g.vendor = 'Return from Project'
    ORDER BY g.grn_date DESC, g.id DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Warehouse GRN List</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    
    <style>
        :root {
            --primary: #a855f7;
            --accent: #c084fc;
            --glass: rgba(255, 255, 255, 0.10);
            --glass-border: rgba(255, 255, 255, 0.20);
        }
        * { margin:0; padding:0; box-sizing:border-box; }
        body {
            font-family: 'Inter', system-ui, sans-serif;
            background: linear-gradient(135deg, rgba(0,0,0,0.78), rgba(0,0,0,0.92)), url('../background.jpeg') center/cover fixed;
            color: #f1f5f9;
            padding: 25px 15px;
        }
        .container { max-width: 1350px; margin: auto; }
        .glass-container {
            background: var(--glass);
            backdrop-filter: blur(24px);
            border-radius: 32px;
            border: 1px solid var(--glass-border);
            box-shadow: 0 25px 80px rgba(0,0,0,0.65);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(90deg, rgba(168,85,247,0.35), rgba(147,51,234,0.25));
            padding: 40px 40px;
            text-align: center;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 15px;
        }
        .header h1 { font-size: 2.4rem; font-weight: 700; }

        .form-area { padding: 40px; }

        .section {
            background: rgba(255,255,255,0.07);
            border: 1px solid rgba(255,255,255,0.18);
            border-radius: 20px;
            padding: 25px;
            margin-bottom: 30px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: rgba(0,0,0,0.25);
            border-radius: 16px;
            overflow: hidden;
        }
        th {
            background: rgba(168,85,247,0.3);
            padding: 16px 12px;
            text-align: left;
        }
        td {
            padding: 16px 12px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }
        tr:hover { background: rgba(168,85,247,0.1); }

        .btn {
            padding: 8px 16px;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 6px;
            transition: 0.3s;
        }
        .btn-view {
            background: #22c55e;
            color: white;
        }
        .btn-view:hover { background: #16a34a; transform: translateY(-2px); }

        .status {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 0.9rem;
            font-weight: 600;
        }
        .status-received { background: rgba(34,197,94,0.2); color: #86efac; }

        .message {
            padding: 16px 24px;
            border-radius: 16px;
            margin-bottom: 25px;
        }
        .success { background: rgba(34,197,94,0.25); border:1px solid #4ade80; color:#86efac; }

        @media (max-width: 768px) {
            .header h1 { font-size: 2rem; }
            table { font-size: 0.95rem; }
        }
    </style>
</head>
<body>
<?php include '../config/header.php'; ?>
<div class="container">
    <div class="glass-container">
        <div class="header">
            <h1><i class="fas fa-list"></i> Warehouse GRN List</h1>
            <a href="grn_from_return_challan.php" class="btn" style="background:linear-gradient(135deg,#a855f7,#c084fc); color:white; padding:12px 24px;">
                <i class="fas fa-plus"></i> New Warehouse GRN from RC
            </a>
        </div>

        <div class="form-area">
            <?php if (isset($_GET['success'])): ?>
                <div class="message success">✅ GRN Created Successfully!</div>
            <?php endif; ?>

            <div class="section">
                <table>
                    <thead>
                        <tr>
                            <th>GRN Code</th>
                            <th>Date</th>
                            <th>Return Challan</th>
                            <th>Project</th>
                            <th>Received By</th>
                            <th>Items</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if ($grns && $grns->num_rows > 0): ?>
                            <?php while ($row = $grns->fetch_assoc()): ?>
                                <tr>
                                    <td><strong><?= htmlspecialchars($row['grn_code']) ?></strong></td>
                                    <td><?= date('d M Y', strtotime($row['grn_date'])) ?></td>
                                    <td><?= htmlspecialchars($row['return_challan_code'] ?? 'N/A') ?></td>
                                    <td>
                                        <?= htmlspecialchars($row['project_code'] ?? '') ?> 
                                        <?= htmlspecialchars($row['project_name'] ?? 'Central Warehouse') ?>
                                    </td>
                                    <td><?= htmlspecialchars($row['received_by_name'] ?? 'Unknown') ?></td>
                                    <td>
                                        <a href="view_grn_warehouse.php?id=<?= $row['id'] ?>" class="btn btn-view" style="font-size:0.85rem; padding:6px 12px;">
                                            View Items
                                        </a>
                                    </td>
                                    <td><span class="status status-received">Received</span></td>
                                    <td>
                                        <a href="view_grn_warehouse.php?id=<?= $row['id'] ?>" class="btn btn-view">
                                            <i class="fas fa-eye"></i> View
                                        </a>
                                    </td>
                                </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr>
                                <td colspan="8" style="text-align:center; padding:50px; color:#94a3b8;">
                                    No Warehouse GRNs found yet.
                                </td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

</body>
</html>

<?php $conn->close(); ?>