<?php
session_start();

if (!isset($_SESSION['admin_logged_in'])) {
    header("Location: adminlogin.php");
    exit();
}

$username = $_SESSION['admin_username'];
if (isset($_SESSION['success_msg'])) {
    $msg = $_SESSION['success_msg'];
    unset($_SESSION['success_msg']);
    $showMsg = true;
} else {
    $showMsg = false;
}

require_once 'config.php';

// Fetch all records
$stmt = $pdo->query("SELECT * FROM papers ORDER BY id DESC");
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html>
<head>
    <title>Admin Panel</title>
    <style>
*{
    margin:0;
    padding:0;
    box-sizing:border-box;
    font-family: 'Inter', sans-serif;
}

body{
    min-height:100vh;
    padding:30px;
}

.container{
    max-width:1250px;
    margin:auto;
     background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    backdrop-filter:blur(15px);
    border:1px solid rgba(255,255,255,0.2);
    border-radius:20px;
    padding:30px;
    box-shadow:0 15px 35px rgba(0,0,0,0.25);
}

.header{
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:25px;
}

.header h2{
    color:#fff;
    font-size:28px;
}

.logout-btn{
    background:#dc3545;
    color:#fff;
    text-decoration:none;
    padding:10px 18px;
    border-radius:8px;
    transition:.3s;
    font-weight:600;
}

.logout-btn:hover{
    background:#c82333;
}

#flash-msg{
    background:#28a745;
    color:#fff;
    padding:15px;
    border-radius:8px;
    margin-bottom:20px;
    text-align:center;
    font-weight:600;
}

.table-wrapper{
    overflow-x:auto;
}

table{
    width:100%;
    border-collapse:collapse;
    background:#fff;
    border-radius:12px;
    overflow:hidden;
}

th{
    background:#022c5e;
    color:#fff;
    padding:15px;
    text-align:left;
}

td{
    padding:14px;
    border-bottom:1px solid #eee;
}

tr:hover{
    background:#f8f9fa;
}

.file-link{
    color:#0d6efd;
    text-decoration:none;
    font-weight:600;
}

.file-link:hover{
    text-decoration:underline;
}

.status-approved{
    color:#28a745;
    font-weight:bold;
}

.status-rejected{
    color:#dc3545;
    font-weight:bold;
}

.status-pending{
    color:#ff9800;
    font-weight:bold;
}

.action-btn{
    text-decoration:none;
    padding:6px 12px;
    border-radius:6px;
    color:#fff;
    font-size:14px;
    margin-right:5px;
}

.approve-btn{
    background:#28a745;
}

.reject-btn{
    background:#dc3545;
}

.approve-btn:hover,
.reject-btn:hover{
    opacity:.85;
}

.no-action{
    color:#6c757d;
    font-style:italic;
}
.revision-btn {
    background: #ffc107;
    color: #000;
}

.revision-btn:hover {
    background: #e0a800;
}
.status-approved {
    color: #28a745;
    font-weight: bold;
}

.status-rejected {
    color: #dc3545;
    font-weight: bold;
}

.status-revision {
    color: #ffc107;
    font-weight: bold;
}

.status-pending {
    color: #6c757d;
    font-weight: bold;
}

@media(max-width:768px){
    .header{
        flex-direction:column;
        gap:15px;
    }

    .container{
        padding:15px;
    }
}
    </style>
</head>
<body>

<?php if ($showMsg): ?>
    <div id="flash-msg" style="padding:10px;background:green;color:white;margin-bottom:10px;">
        <?= htmlspecialchars($msg) ?>
    </div>

    <script>
        setTimeout(function () {
            let msg = document.getElementById('flash-msg');
            if (msg) {
                msg.style.display = 'none';
            }

            location.reload();
        }, 3000);
    </script>
<?php endif; ?>

<div class="container">

<div class="header">
    <h2>Submitted Authors</h2>

    <a href="adminlogout.php" class="logout-btn">
        Logout (<?= htmlspecialchars($username) ?>)
    </a>
</div>

<div class="table-wrapper">
<table>
    <tr>
        <th>Author Name</th>
        <th>Email</th>
        <th>File</th>
        <th>Status</th>
        <th>Action</th>
    </tr>

    <?php foreach($records as $row): ?>
    <tr>
        <td><?= htmlspecialchars($row['author_name']) ?></td>

        <td><?= htmlspecialchars($row['email']) ?></td>

            <td>
                <a class="file-link"
                   href="view_file.php?file=<?= urlencode(trim($row['pdf_file'])) ?>"
                   target="_blank">
                   View File
                </a>
            </td>

        <td>
            <?php
            $status = $row['status'] ?? 'Pending';

            if($status == 'Approved'){
                echo '<span class="status-approved">Approved</span>';
            }elseif($status == 'Reject'){
                echo '<span class="status-rejected">Reject</span>';
            } elseif ($status == 'Revision') {
        echo '<span class="status-revision">Revision</span>';
    }else{
                echo '<span class="status-pending">Pending</span>';
            }
            ?>
        </td>

        <td>
            <?php if (empty($row['status']) || $row['status'] === 'Pending'): ?>
                <a class="action-btn approve-btn"
                   href="approve?id=<?= $row['id'] ?>">
                   Approve
                </a>

                <a class="action-btn reject-btn"
                   href="reject?id=<?= $row['id'] ?>">
                   Reject
                </a>
				
				 <a class="action-btn revision-btn"
           href="revisions?id=<?= $row['id'] ?>">
           Revision
        </a>
				
            <?php else: ?>
                <span class="no-action">No Action</span>
            <?php endif; ?>
        </td>
    </tr>
    <?php endforeach; ?>
</table>
</div>

</div>

</body>
</html>