<link rel="icon" href="logo.jpg" type="image/jpg">
<?php
// File: archived_records.php
// Displays a list of all individual members who have been archived.
require_once "db.php";
check_login();

$page_title = "Archived Records";

// --- ACTION HANDLING ---

// Handle Restore Member
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['restore_member'])) {
    $resident_id = $_POST['resident_id'];
    $stmt = $conn->prepare("UPDATE residents SET is_archived = 0, date_archived = NULL WHERE id = ?");
    $stmt->bind_param("i", $resident_id);
    if ($stmt->execute()) {
        $_SESSION['message'] = "Member restored successfully.";
        $_SESSION['message_type'] = "success";
    } else {
        $_SESSION['message'] = "Error restoring member: " . $stmt->error;
        $_SESSION['message_type'] = "danger";
    }
    $stmt->close();
    header("Location: archived_records.php");
    exit;
}

// Handle Permanent Delete
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['delete_permanently'])) {
    $resident_id = $_POST['resident_id'];
    $admin_password = $_POST['admin_password'];

    // First, verify the admin's password
    $user_id = $_SESSION['user_id'];
    $user_stmt = $conn->prepare("SELECT password FROM users WHERE id = ?");
    $user_stmt->bind_param("i", $user_id);
    $user_stmt->execute();
    $user_result = $user_stmt->get_result();
    $user = $user_result->fetch_assoc();

    if ($user && password_verify($admin_password, $user['password'])) {
        // Password is correct, proceed with deletion
        $delete_stmt = $conn->prepare("DELETE FROM residents WHERE id = ?");
        $delete_stmt->bind_param("i", $resident_id);
        if ($delete_stmt->execute()) {
            $_SESSION['message'] = "Member permanently deleted.";
            $_SESSION['message_type'] = "success";
        } else {
            $_SESSION['message'] = "Error deleting member: " . $delete_stmt->error;
            $_SESSION['message_type'] = "danger";
        }
        $delete_stmt->close();
    } else {
        // Password was incorrect
        $_SESSION['message'] = "Incorrect password. Deletion cancelled.";
        $_SESSION['message_type'] = "danger";
    }
    $user_stmt->close();
    header("Location: archived_records.php");
    exit;
}

include 'header.php';
?>

<div class="main-content">
    <div class="page-header">
        <h1>Archived Records</h1>
        <p>This page lists all individual residents who have been archived.</p>
    </div>

    <?php if (isset($_SESSION['message'])): ?>
        <div class="alert alert-<?php echo $_SESSION['message_type']; ?>">
            <?php echo $_SESSION['message']; unset($_SESSION['message']); unset($_SESSION['message_type']); ?>
        </div>
    <?php endif; ?>

    <div class="card">
        <div class="table-responsive">
            <table>
                <thead>
                    <tr>
                        <th>Full Name</th>
                        <th>Household No.</th>
                        <th>Date Archived</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $sql = "
                        SELECT r.id, r.first_name, r.middle_name, r.last_name, r.suffix, r.date_archived, h.household_no
                        FROM residents r
                        JOIN households h ON r.household_id = h.id
                        WHERE r.is_archived = 1
                        ORDER BY r.date_archived DESC
                    ";
                    $result = $conn->query($sql);
                    if ($result->num_rows > 0):
                        while($row = $result->fetch_assoc()):
                            $full_name = htmlspecialchars($row['last_name'] . ", " . $row['first_name'] . " " . $row['suffix'] . " " . $row['middle_name']);
                    ?>
                    <tr>
                        <td><?php echo $full_name; ?></td>
                        <td><?php echo htmlspecialchars($row['household_no']); ?></td>
                        <td><?php echo date("F j, Y, g:i a", strtotime($row['date_archived'])); ?></td>
                        <td>
                             <form action="archived_records.php" method="POST" style="display:inline-block;">
                                <input type="hidden" name="resident_id" value="<?php echo $row['id']; ?>">
                                <button type="submit" name="restore_member" class="btn btn-sm btn-success">Restore</button>
                            </form>
                            <button type="button" class="btn btn-sm btn-danger" onclick="openDeleteModal(<?php echo $row['id']; ?>)">Delete Permanently</button>
                        </td>
                    </tr>
                    <?php
                        endwhile;
                    else:
                    ?>
                        <tr><td colspan="4">No archived records found.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- Permanent Delete Confirmation Modal -->
<div id="deleteModal" class="modal">
    <div class="modal-content">
        <div class="modal-header">
            <h2>Confirm Permanent Deletion</h2>
        </div>
        <form action="archived_records.php" method="POST">
            <div class="modal-body">
                <p>This action cannot be undone. To confirm, please enter your administrator password.</p>
                <input type="hidden" name="resident_id" id="delete_resident_id">
                <div class="form-group">
                    <label for="admin_password">Admin Password</label>
                    <input type="password" name="admin_password" class="form-control" required>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" onclick="closeDeleteModal()">Cancel</button>
                <button type="submit" name="delete_permanently" class="btn btn-danger">Confirm Delete</button>
            </div>
        </form>
    </div>
</div>

<script>
    const modal = document.getElementById('deleteModal');
    function openDeleteModal(residentId) {
        document.getElementById('delete_resident_id').value = residentId;
        modal.style.display = "block";
    }
    function closeDeleteModal() {
        modal.style.display = "none";
    }
    window.onclick = function(event) {
        if (event.target == modal) {
            closeDeleteModal();
        }
    }
</script>

<?php include 'footer.php'; ?>

