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

$role = $_SESSION['role'] ?? '';
if (!in_array($role, ['admin', 'ra'], true)) {
    die('Access denied');
}

function e($v) {
    return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8');
}

$sql = "
    SELECT p.id, p.product_name, p.generic_name, p.brand_name, p.standards,
           p.strength, p.volume, p.dosage_form, p.shelf_life, p.indication,
           p.primary_packing, p.manufactured_by, p.marketed_by, p.distributed_by,
           p.dossier_type, p.status, c.country_name
    FROM products p
    LEFT JOIN countries c ON c.id = p.country_id
    WHERE p.status = 'pending'
      AND p.created_by IN (
          SELECT id FROM users WHERE role = 'admin'
      )
    ORDER BY p.id DESC
";

$result = $conn->query($sql);
$totalRows = ($result && $result->num_rows) ? $result->num_rows : 0;
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Product Requests</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
<style>
:root{
    --bg:#eef2f7;
    --card:#ffffff;
    --text:#0f172a;
    --muted:#64748b;
    --primary:#2563eb;
    --primary-dark:#1d4ed8;
    --border:#e2e8f0;
    --soft:#f8fafc;
    --success:#dcfce7;
    --success-text:#166534;
}

*{ box-sizing:border-box; }

body{
    margin:0;
    font-family:Inter,Segoe UI,Arial,sans-serif;
    color:var(--text);
    background:
        radial-gradient(circle at top left, rgba(37,99,235,.08), transparent 22%),
        radial-gradient(circle at top right, rgba(15,23,42,.06), transparent 20%),
        var(--bg);
    min-height:100vh;
    padding:90px 24px 32px;
}

.container{
    max-width:1600px;
    margin:0 auto;
}

.page-shell{
    margin-left:260px;
    min-height:100vh;
}

.page-card{
    background:rgba(255,255,255,.94);
    backdrop-filter:blur(8px);
    border:1px solid rgba(226,232,240,.9);
    border-radius:24px;
    box-shadow:0 18px 50px rgba(15,23,42,.10);
    padding:24px;
}

.page-header{
    display:flex;
    align-items:flex-start;
    justify-content:space-between;
    gap:16px;
    margin-bottom:18px;
}

.page-title{
    margin:0;
    font-size:28px;
    letter-spacing:-.02em;
    color:#000;
}

.page-subtitle{
    margin:8px 0 0;
    color:var(--muted);
    font-size:14px;
}

.badge{
    display:inline-flex;
    align-items:center;
    gap:8px;
    padding:10px 14px;
    border-radius:999px;
    background:var(--soft);
    border:1px solid var(--border);
    color:#0f172a;
    font-weight:700;
    white-space:nowrap;
}

.badge i{
    color:var(--primary);
}

.table-wrap{
    overflow:auto;
    border:1px solid var(--border);
    border-radius:18px;
    background:#fff;
    box-shadow:0 10px 25px rgba(15,23,42,.05);
}

table{
    width:100%;
    border-collapse:separate;
    border-spacing:0;
    min-width:1200px;
}

thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#374157;
    color:#fff;
    text-align:left;
    font-size:13px;
    padding:14px 12px;
    border-bottom:1px solid var(--border);
    white-space:nowrap;
}

tbody td{
    padding:13px 12px;
    font-size:14px;
    border-bottom:1px solid #eef2f7;
    vertical-align:top;
    color:#111827;
}

tbody tr:nth-child(even){
    background:#fcfdff;
}

tbody tr:hover{
    background:#f8fbff;
}

th:first-child,
td:first-child{
    padding-left:16px;
}

th:last-child,
td:last-child{
    padding-right:16px;
}

.btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    padding:9px 14px;
    background:#000;
    color:#fff;
    text-decoration:none;
    border-radius:10px;
    font-weight:700;
    transition:.18s ease;
    box-shadow:0 8px 18px rgba(37,99,235,.14);
    white-space:nowrap;
}

.btn:hover{
    transform:translateY(-1px);
    background:linear-gradient(135deg, var(--primary-dark), var(--primary));
}

.empty-state{
    padding:40px 20px;
    text-align:center;
    color:var(--muted);
    font-size:15px;
}

@media (max-width: 1200px){
    .page-shell{
        margin-left:220px;
    }
}

@media (max-width: 992px){
    body{
        padding:84px 16px 20px;
    }

    .page-shell{
        margin-left:0;
    }

    .page-card{
        padding:18px;
        border-radius:18px;
    }

    .page-header{
        flex-direction:column;
        align-items:flex-start;
    }

    .page-title{
        font-size:24px;
    }
}

@media (max-width: 640px){
    .page-card{
        padding:16px;
    }

    .badge{
        width:100%;
        justify-content:center;
    }
}
</style>
</head>
<body>

<?php if (file_exists('rasidebar.php')) include 'rasidebar.php'; ?>
<?php if (file_exists('../header.php')) include '../header.php'; ?>

<div class="container">
    <div class="page-shell">
        <div class="page-card">
            <div class="page-header">
                <div>
                    <h1 class="page-title">Product Requests</h1>
                    <p class="page-subtitle">Pending products submitted by admin users</p>
                </div>

                <div class="badge">
                    <i class="fa-solid fa-layer-group"></i>
                    Total Requests: <?php echo (int)$totalRows; ?>
                </div>
            </div>

            <div class="table-wrap">
                <table>
                    <thead>
                        <tr>
                            <th>Product Name</th>
                            <th>Generic Name</th>
                            <th>Brand Name</th>
                            <th>Standard</th>
                            <th>Strength</th>
                            <th>Volume</th>
                            <th>Dosage Form</th>
                            <th>Shelf Life</th>
                            <th>Indication</th>
                            <th>Packing</th>
                            <th>Manufactured By</th>
                            <th>Marketed By</th>
                            <th>Distributed By</th>
                            <th>Country</th>
                            <th>Type of Dossier</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php if ($result && $result->num_rows > 0): ?>
                        <?php while ($row = $result->fetch_assoc()): ?>
                            <tr>
                                <td><?php echo e($row['product_name']); ?></td>
                                <td><?php echo e($row['generic_name']); ?></td>
                                <td><?php echo e($row['brand_name']); ?></td>
                                <td><?php echo e($row['standards']); ?></td>
                                <td><?php echo e($row['strength']); ?></td>
                                <td><?php echo e($row['volume']); ?></td>
                                <td><?php echo e($row['dosage_form']); ?></td>
                                <td><?php echo e($row['shelf_life']); ?></td>
                                <td><?php echo e($row['indication']); ?></td>
                                <td><?php echo e($row['primary_packing']); ?></td>
                                <td><?php echo e($row['manufactured_by']); ?></td>
                                <td><?php echo e($row['marketed_by']); ?></td>
                                <td><?php echo e($row['distributed_by']); ?></td>
                                <td><?php echo e($row['country_name']); ?></td>
                                <td><?php echo e($row['dossier_type']); ?></td>
                                <td>
                                    <a class="btn" href="add_product.php?id=<?php echo (int)$row['id']; ?>">
                                        <i class="fa-solid fa-pen-to-square"></i>
                                        Edit
                                    </a>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="16">
                                <div class="empty-state">No pending product requests found.</div>
                            </td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

</body>
</html>