<?php
$conn = new mysqli("localhost", "job_portal", "job_portal", "job_portal");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_GET['delete_id'])) {
    $id = intval($_GET['delete_id']);
    $res = $conn->query("SELECT resume FROM career_applications WHERE id=$id");
    if ($row = $res->fetch_assoc()) {
        if (file_exists($row['resume'])) unlink($row['resume']);
    }
    $conn->query("DELETE FROM career_applications WHERE id=$id");
    header("Location: display_jobs.php");
    exit;
}

$result = $conn->query("SELECT * FROM career_applications ORDER BY submitted_at DESC");
?>
<!DOCTYPE html>
<html>
<head>
    <title>Career Applications</title>
     <style>
        body {
            font-family: Arial, sans-serif;
        }

        h2 {
            margin-bottom: 10px;
        }

        table {
            border-collapse: collapse;
            width: 100%;
            font-size: 12px;
        }

        th, td {
            padding: 6px;
            border: 1px solid #999;
            text-align: left;
        }

        th {
            background-color: #004080;
            color: white;
        }

        .btn {
            padding: 6px 12px;
            border-radius: 3px;
            text-decoration: none;
            color: white;
            font-size: 13px;
            margin: 5px 0;
            display: inline-block;
        }

        .delete { background-color: #dc3545; }
        .print { background-color: #198754; }
        .logout { background-color: #0d6efd; float: right; }

        @media print {
            .btn, .logout {
                display: none !important;
            }
            body {
                margin: 0;
            }
            table {
                page-break-inside: avoid;
            }
        }

        /* A4 paper styling for print */
        @page {
            size: A4;
            margin: 1cm;
        }
    </style>
</head>
<body>
 <h2>Career Applications 
        
    </h2>
<a href="logout.php" class="btn logout">Logout</a>
    <button onClick="window.print()" class="btn print">Print</button>


<table>
    <tr>
        <th>ID</th><th>Name</th><th>Email</th><th>Phone</th><th>City</th>
        <th>Resume</th><th>Reference</th><th>Submitted</th><th>Action</th>
    </tr>
    <?php while ($row = $result->fetch_assoc()) { ?>
    <tr>
        <td><?= $row['id'] ?></td>
        <td><?= htmlspecialchars($row['name']) ?></td>
        <td><?= htmlspecialchars($row['email']) ?></td>
        <td><?= htmlspecialchars($row['phone']) ?></td>
        <td><?= htmlspecialchars($row['city']) ?></td>
        <td>
            <?php if ($row['resume']) { ?>
                <a class="resume-link" href="<?= $row['resume'] ?>" target="_blank">View</a>
            <?php } else { echo "N/A"; } ?>
        </td>
        <td><?= htmlspecialchars($row['profileref']) ?></td>
        <td><?= $row['submitted_at'] ?></td>
        <td><a class="delete-btn" href="?delete_id=<?= $row['id'] ?>" onclick="return confirm('Delete this entry?')">Delete</a></td>
    </tr>
    <?php } ?>
</table>

</body>
</html>
