<?php
/**
 * Admin - Overlap Attempts Report
 * Shows all attempts where users tried to add land that overlaps with existing parcels
 * Helps identify potential land fraud or boundary disputes
 */

session_start();
require_once '../db_connect.php';
require_once '../includes/polygon_functions.php';

if(!isset($_SESSION['user_id']) || $_SESSION['role'] != 'admin') {
    header('Location: ../login');
    exit;
}

$admin_id = $_SESSION['user_id'];
$message = '';
$message_type = '';

// Handle review action
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['action'])) {
    $attempt_id = intval($_POST['attempt_id']);
    $action = $_POST['action'];

    if(in_array($action, ['reviewed', 'flagged', 'cleared'])) {
        $notes = mysqli_real_escape_string($conn, $_POST['notes'] ?? '');
        if(reviewOverlapAttempt($conn, $attempt_id, $admin_id, $action, $notes)) {
            $message = 'Attempt marked as ' . ucfirst($action);
            $message_type = 'success';
        } else {
            $message = 'Failed to update attempt';
            $message_type = 'error';
        }
    }
}

// Filter
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'pending';
$severity_filter = isset($_GET['severity']) ? $_GET['severity'] : '';

// Build WHERE clause
$where_conditions = [];
if($filter && $filter != 'all') {
    $filter_esc = mysqli_real_escape_string($conn, $filter);
    $where_conditions[] = "review_status = '$filter_esc'";
}
if($severity_filter) {
    $severity_esc = mysqli_real_escape_string($conn, $severity_filter);
    $where_conditions[] = "severity = '$severity_esc'";
}
$where_clause = !empty($where_conditions) ? "WHERE " . implode(" AND ", $where_conditions) : "";

// Pagination
$per_page = 20;
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
$offset = ($page - 1) * $per_page;

// Get counts
$pending_count = countOverlapAttempts($conn, 'pending');
$flagged_count = countOverlapAttempts($conn, 'flagged');
$reviewed_count = countOverlapAttempts($conn, 'reviewed');
$cleared_count = countOverlapAttempts($conn, 'cleared');
$total_count = countOverlapAttempts($conn, null);

// Get attempts
$sql = "SELECT oa.*,
               l.land_name as new_land_current_name, l.total_area as new_land_area,
               el.land_name as existing_land_current_name, el.total_area as existing_land_area,
               r.full_name as reviewed_by_name
        FROM overlap_attempts oa
        LEFT JOIN lands l ON oa.new_land_id = l.id
        LEFT JOIN lands el ON oa.existing_land_id = el.id
        LEFT JOIN users r ON oa.reviewed_by = r.id
        $where_clause
        ORDER BY
            CASE oa.severity
                WHEN 'critical' THEN 1
                WHEN 'warning' THEN 2
                ELSE 3
            END,
            oa.created_at DESC
        LIMIT $per_page OFFSET $offset";

$attempts = mysqli_query($conn, $sql);

// Total for pagination
$count_sql = "SELECT COUNT(*) as cnt FROM overlap_attempts $where_clause";
$total_filtered = mysqli_fetch_assoc(mysqli_query($conn, $count_sql))['cnt'];
$total_pages = ceil($total_filtered / $per_page);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Overlap Attempts Report - Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="../assets/font-awesome/css/all.min.css">
    <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', sans-serif;
        }

        :root {
            --primary: #1a3a6e;
            --primary-light: #2d5a9e;
            --secondary: #2d5a3d;
            --danger: #dc3545;
            --warning: #f59e0b;
            --success: #10b981;
            --info: #0ea5e9;
            --gray-50: #f8fafc;
            --gray-100: #f1f5f9;
            --gray-200: #e2e8f0;
            --gray-300: #cbd5e1;
            --gray-400: #94a3b8;
            --gray-500: #64748b;
            --gray-600: #475569;
            --gray-700: #334155;
            --gray-800: #1e293b;
            --sidebar-width: 280px;
        }

        body {
            background: var(--gray-100);
            min-height: 100vh;
        }

        .admin-main {
            margin-left: var(--sidebar-width);
            min-height: 100vh;
        }

        @media (max-width: 1024px) {
            .admin-main { margin-left: 0; }
        }

        .page-header {
            background: linear-gradient(135deg, var(--primary) 0%, var(--secondary) 100%);
            padding: 32px;
            color: white;
        }

        .page-header h1 {
            font-size: 24px;
            font-weight: 700;
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .page-header p {
            opacity: 0.9;
            margin-top: 8px;
        }

        .content-container {
            padding: 24px;
            max-width: 1400px;
            margin: 0 auto;
        }

        .stats-row {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 16px;
            margin-bottom: 24px;
        }

        .stat-card {
            background: white;
            padding: 20px;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            text-align: center;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
            color: inherit;
            border: 2px solid transparent;
        }

        .stat-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }

        .stat-card.active {
            border-color: var(--primary);
        }

        .stat-card h3 {
            font-size: 28px;
            font-weight: 700;
            color: var(--gray-800);
        }

        .stat-card p {
            font-size: 13px;
            color: var(--gray-500);
            margin-top: 4px;
        }

        .stat-card.pending h3 { color: var(--warning); }
        .stat-card.flagged h3 { color: var(--danger); }
        .stat-card.reviewed h3 { color: var(--info); }
        .stat-card.cleared h3 { color: var(--success); }

        .alert-message {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 16px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .alert-message.success {
            background: rgba(16, 185, 129, 0.1);
            color: #065f46;
        }

        .alert-message.error {
            background: rgba(220, 53, 69, 0.1);
            color: #991b1b;
        }

        .attempts-card {
            background: white;
            border-radius: 16px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.05);
            overflow: hidden;
        }

        .card-header {
            padding: 20px 24px;
            border-bottom: 1px solid var(--gray-200);
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 12px;
        }

        .card-header h2 {
            font-size: 18px;
            font-weight: 600;
            color: var(--gray-800);
        }

        .filter-pills {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .filter-pill {
            padding: 6px 14px;
            border-radius: 20px;
            font-size: 13px;
            font-weight: 500;
            text-decoration: none;
            transition: all 0.2s;
            border: 1px solid var(--gray-200);
            color: var(--gray-600);
            background: white;
        }

        .filter-pill:hover {
            border-color: var(--primary);
            color: var(--primary);
        }

        .filter-pill.active {
            background: var(--primary);
            color: white;
            border-color: var(--primary);
        }

        .filter-pill.critical { border-color: var(--danger); color: var(--danger); }
        .filter-pill.critical.active { background: var(--danger); color: white; }
        .filter-pill.warning { border-color: var(--warning); color: var(--warning); }
        .filter-pill.warning.active { background: var(--warning); color: white; }

        .attempt-item {
            padding: 20px 24px;
            border-bottom: 1px solid var(--gray-100);
            transition: background 0.2s;
        }

        .attempt-item:hover {
            background: var(--gray-50);
        }

        .attempt-item:last-child {
            border-bottom: none;
        }

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

        .attempt-severity {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 4px 10px;
            border-radius: 6px;
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .severity-critical {
            background: rgba(220, 53, 69, 0.1);
            color: var(--danger);
        }

        .severity-warning {
            background: rgba(245, 158, 11, 0.1);
            color: var(--warning);
        }

        .severity-info {
            background: rgba(14, 165, 233, 0.1);
            color: var(--info);
        }

        .attempt-status {
            padding: 4px 10px;
            border-radius: 6px;
            font-size: 11px;
            font-weight: 600;
            text-transform: uppercase;
        }

        .status-pending { background: rgba(245, 158, 11, 0.1); color: var(--warning); }
        .status-flagged { background: rgba(220, 53, 69, 0.1); color: var(--danger); }
        .status-reviewed { background: rgba(14, 165, 233, 0.1); color: var(--info); }
        .status-cleared { background: rgba(16, 185, 129, 0.1); color: var(--success); }

        .attempt-parties {
            display: grid;
            grid-template-columns: 1fr auto 1fr;
            gap: 20px;
            align-items: center;
            margin-bottom: 16px;
        }

        .party-card {
            padding: 16px;
            background: var(--gray-50);
            border-radius: 10px;
        }

        .party-label {
            font-size: 11px;
            font-weight: 600;
            color: var(--gray-400);
            text-transform: uppercase;
            margin-bottom: 8px;
        }

        .party-name {
            font-size: 15px;
            font-weight: 600;
            color: var(--gray-800);
            margin-bottom: 4px;
        }

        .party-details {
            font-size: 13px;
            color: var(--gray-500);
        }

        .party-details i {
            width: 16px;
            color: var(--gray-400);
        }

        .vs-divider {
            width: 40px;
            height: 40px;
            background: var(--gray-200);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 12px;
            font-weight: 700;
            color: var(--gray-500);
        }

        .attempt-description {
            background: linear-gradient(135deg, rgba(220, 53, 69, 0.05), rgba(245, 158, 11, 0.05));
            padding: 12px 16px;
            border-radius: 8px;
            border-left: 4px solid var(--warning);
            margin-bottom: 16px;
        }

        .attempt-description p {
            font-size: 14px;
            color: var(--gray-700);
        }

        .attempt-meta {
            display: flex;
            gap: 24px;
            flex-wrap: wrap;
            font-size: 12px;
            color: var(--gray-500);
            margin-bottom: 16px;
        }

        .attempt-meta span {
            display: flex;
            align-items: center;
            gap: 6px;
        }

        .attempt-actions {
            display: flex;
            gap: 8px;
            flex-wrap: wrap;
        }

        .btn {
            display: inline-flex;
            align-items: center;
            gap: 6px;
            padding: 8px 16px;
            border-radius: 8px;
            font-size: 13px;
            font-weight: 500;
            border: none;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
        }

        .btn-sm {
            padding: 6px 12px;
            font-size: 12px;
        }

        .btn-primary {
            background: var(--primary);
            color: white;
        }

        .btn-primary:hover {
            background: var(--primary-light);
        }

        .btn-danger {
            background: var(--danger);
            color: white;
        }

        .btn-success {
            background: var(--success);
            color: white;
        }

        .btn-outline {
            background: white;
            border: 1px solid var(--gray-300);
            color: var(--gray-600);
        }

        .btn-outline:hover {
            border-color: var(--primary);
            color: var(--primary);
        }

        .empty-state {
            padding: 60px 24px;
            text-align: center;
        }

        .empty-state i {
            font-size: 48px;
            color: var(--gray-300);
            margin-bottom: 16px;
        }

        .empty-state h3 {
            font-size: 18px;
            color: var(--gray-600);
            margin-bottom: 8px;
        }

        .empty-state p {
            color: var(--gray-400);
        }

        .pagination {
            display: flex;
            justify-content: center;
            gap: 8px;
            padding: 20px;
        }

        .pagination a, .pagination span {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 36px;
            height: 36px;
            border-radius: 8px;
            font-size: 13px;
            text-decoration: none;
            transition: all 0.2s;
        }

        .pagination a {
            background: white;
            border: 1px solid var(--gray-200);
            color: var(--gray-600);
        }

        .pagination a:hover {
            border-color: var(--primary);
            color: var(--primary);
        }

        .pagination .active {
            background: var(--primary);
            color: white;
        }

        /* Modal */
        .modal-backdrop {
            position: fixed;
            inset: 0;
            background: rgba(0,0,0,0.5);
            z-index: 999;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s;
        }

        .modal-backdrop.show {
            opacity: 1;
            visibility: visible;
        }

        .modal {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%) scale(0.9);
            background: white;
            border-radius: 16px;
            width: 90%;
            max-width: 500px;
            z-index: 1000;
            opacity: 0;
            visibility: hidden;
            transition: all 0.3s;
        }

        .modal.show {
            opacity: 1;
            visibility: visible;
            transform: translate(-50%, -50%) scale(1);
        }

        .modal-header {
            padding: 20px 24px;
            border-bottom: 1px solid var(--gray-200);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .modal-header h3 {
            font-size: 18px;
            font-weight: 600;
        }

        .modal-close {
            background: none;
            border: none;
            font-size: 20px;
            color: var(--gray-400);
            cursor: pointer;
        }

        .modal-body {
            padding: 24px;
        }

        .form-group {
            margin-bottom: 16px;
        }

        .form-label {
            display: block;
            font-size: 13px;
            font-weight: 600;
            color: var(--gray-700);
            margin-bottom: 6px;
        }

        .form-control {
            width: 100%;
            padding: 10px 14px;
            border: 2px solid var(--gray-200);
            border-radius: 8px;
            font-size: 14px;
        }

        .form-control:focus {
            outline: none;
            border-color: var(--primary);
        }

        .modal-footer {
            padding: 16px 24px;
            border-top: 1px solid var(--gray-200);
            display: flex;
            justify-content: flex-end;
            gap: 12px;
        }

        @media (max-width: 768px) {
            .attempt-parties {
                grid-template-columns: 1fr;
            }
            .vs-divider {
                margin: 0 auto;
            }
            .content-container {
                padding: 16px;
            }
        }
    </style>
</head>
<body>
    <?php include 'includes/sidebar.php'; ?>

    <main class="admin-main">
        <div class="page-header">
            <h1><i class="fas fa-user-secret"></i> Overlap Attempts Report</h1>
            <p>Track and investigate users who attempted to register land overlapping with existing parcels</p>
        </div>

        <div class="content-container">
            <?php if($message): ?>
            <div class="alert-message <?php echo $message_type; ?>">
                <i class="fas fa-<?php echo $message_type == 'success' ? 'check-circle' : 'exclamation-circle'; ?>"></i>
                <?php echo htmlspecialchars($message); ?>
            </div>
            <?php endif; ?>

            <!-- Stats -->
            <div class="stats-row">
                <a href="?filter=pending" class="stat-card pending <?php echo $filter == 'pending' ? 'active' : ''; ?>">
                    <h3><?php echo $pending_count; ?></h3>
                    <p>Pending Review</p>
                </a>
                <a href="?filter=flagged" class="stat-card flagged <?php echo $filter == 'flagged' ? 'active' : ''; ?>">
                    <h3><?php echo $flagged_count; ?></h3>
                    <p>Flagged</p>
                </a>
                <a href="?filter=reviewed" class="stat-card reviewed <?php echo $filter == 'reviewed' ? 'active' : ''; ?>">
                    <h3><?php echo $reviewed_count; ?></h3>
                    <p>Reviewed</p>
                </a>
                <a href="?filter=cleared" class="stat-card cleared <?php echo $filter == 'cleared' ? 'active' : ''; ?>">
                    <h3><?php echo $cleared_count; ?></h3>
                    <p>Cleared</p>
                </a>
                <a href="?filter=all" class="stat-card <?php echo $filter == 'all' ? 'active' : ''; ?>">
                    <h3><?php echo $total_count; ?></h3>
                    <p>Total</p>
                </a>
            </div>

            <!-- Attempts List -->
            <div class="attempts-card">
                <div class="card-header">
                    <h2><i class="fas fa-list"></i> Overlap Attempts</h2>
                    <div class="filter-pills">
                        <a href="?filter=<?php echo $filter; ?>&severity=" class="filter-pill <?php echo !$severity_filter ? 'active' : ''; ?>">All Severity</a>
                        <a href="?filter=<?php echo $filter; ?>&severity=critical" class="filter-pill critical <?php echo $severity_filter == 'critical' ? 'active' : ''; ?>">Critical</a>
                        <a href="?filter=<?php echo $filter; ?>&severity=warning" class="filter-pill warning <?php echo $severity_filter == 'warning' ? 'active' : ''; ?>">Warning</a>
                    </div>
                </div>

                <?php if(mysqli_num_rows($attempts) == 0): ?>
                <div class="empty-state">
                    <i class="fas fa-shield-alt"></i>
                    <h3>No Overlap Attempts Found</h3>
                    <p>No users have attempted to register overlapping land parcels<?php echo $filter != 'all' ? ' with this status' : ''; ?>.</p>
                </div>
                <?php else: ?>
                <?php while($attempt = mysqli_fetch_assoc($attempts)): ?>
                <div class="attempt-item">
                    <div class="attempt-header">
                        <div>
                            <span class="attempt-severity severity-<?php echo $attempt['severity']; ?>">
                                <i class="fas fa-<?php echo $attempt['severity'] == 'critical' ? 'exclamation-circle' : ($attempt['severity'] == 'warning' ? 'exclamation-triangle' : 'info-circle'); ?>"></i>
                                <?php echo ucfirst($attempt['severity']); ?>
                            </span>
                            <span class="attempt-status status-<?php echo $attempt['review_status']; ?>">
                                <?php echo ucfirst($attempt['review_status']); ?>
                            </span>
                        </div>
                        <small style="color: var(--gray-400);">#<?php echo $attempt['id']; ?></small>
                    </div>

                    <div class="attempt-parties">
                        <div class="party-card">
                            <div class="party-label">Attempted By (New Land)</div>
                            <div class="party-name"><?php echo htmlspecialchars($attempt['attempted_by_user_name'] ?: 'Unknown'); ?></div>
                            <div class="party-details">
                                <?php if($attempt['attempted_by_phone']): ?>
                                <div><i class="fas fa-phone"></i> <?php echo htmlspecialchars($attempt['attempted_by_phone']); ?></div>
                                <?php endif; ?>
                                <?php if($attempt['attempted_by_email']): ?>
                                <div><i class="fas fa-envelope"></i> <?php echo htmlspecialchars($attempt['attempted_by_email']); ?></div>
                                <?php endif; ?>
                                <div><i class="fas fa-map"></i> <?php echo htmlspecialchars($attempt['new_land_name'] ?: 'Land not saved'); ?></div>
                            </div>
                        </div>

                        <div class="vs-divider">VS</div>

                        <div class="party-card">
                            <div class="party-label">Existing Land Owner</div>
                            <div class="party-name"><?php echo htmlspecialchars($attempt['existing_land_owner_name']); ?></div>
                            <div class="party-details">
                                <?php if($attempt['existing_land_owner_phone']): ?>
                                <div><i class="fas fa-phone"></i> <?php echo htmlspecialchars($attempt['existing_land_owner_phone']); ?></div>
                                <?php endif; ?>
                                <div><i class="fas fa-map"></i> <?php echo htmlspecialchars($attempt['existing_land_name']); ?></div>
                            </div>
                        </div>
                    </div>

                    <div class="attempt-description">
                        <p><strong>Overlap Type:</strong> <?php echo str_replace('_', ' ', $attempt['overlap_type']); ?></p>
                        <p><?php echo htmlspecialchars($attempt['overlap_description']); ?></p>
                        <?php if($attempt['overlap_percentage'] > 0): ?>
                        <p><strong>Overlap:</strong> <?php echo number_format($attempt['overlap_percentage'], 1); ?>%</p>
                        <?php endif; ?>
                    </div>

                    <div class="attempt-meta">
                        <span><i class="fas fa-calendar"></i> <?php echo date('M d, Y H:i', strtotime($attempt['created_at'])); ?></span>
                        <?php if($attempt['region']): ?>
                        <span><i class="fas fa-map-marker-alt"></i> <?php echo htmlspecialchars($attempt['region']); ?><?php echo $attempt['district'] ? ', ' . htmlspecialchars($attempt['district']) : ''; ?></span>
                        <?php endif; ?>
                        <span><i class="fas fa-globe"></i> <?php echo htmlspecialchars($attempt['ip_address']); ?></span>
                        <?php if($attempt['reviewed_by_name']): ?>
                        <span><i class="fas fa-user-check"></i> Reviewed by <?php echo htmlspecialchars($attempt['reviewed_by_name']); ?></span>
                        <?php endif; ?>
                    </div>

                    <?php if($attempt['review_status'] == 'pending'): ?>
                    <div class="attempt-actions">
                        <button class="btn btn-sm btn-primary" onclick="openReviewModal(<?php echo $attempt['id']; ?>, 'reviewed')">
                            <i class="fas fa-check"></i> Mark Reviewed
                        </button>
                        <button class="btn btn-sm btn-danger" onclick="openReviewModal(<?php echo $attempt['id']; ?>, 'flagged')">
                            <i class="fas fa-flag"></i> Flag as Suspicious
                        </button>
                        <button class="btn btn-sm btn-success" onclick="openReviewModal(<?php echo $attempt['id']; ?>, 'cleared')">
                            <i class="fas fa-shield-alt"></i> Clear
                        </button>
                        <?php if($attempt['new_land_id']): ?>
                        <a href="../view_land.php?id=<?php echo $attempt['new_land_id']; ?>" class="btn btn-sm btn-outline" target="_blank">
                            <i class="fas fa-eye"></i> View New Land
                        </a>
                        <?php endif; ?>
                        <a href="../view_land.php?id=<?php echo $attempt['existing_land_id']; ?>" class="btn btn-sm btn-outline" target="_blank">
                            <i class="fas fa-eye"></i> View Existing Land
                        </a>
                    </div>
                    <?php else: ?>
                    <div class="attempt-actions">
                        <?php if($attempt['review_notes']): ?>
                        <span style="color: var(--gray-500); font-size: 13px;"><i class="fas fa-comment"></i> <?php echo htmlspecialchars($attempt['review_notes']); ?></span>
                        <?php endif; ?>
                        <?php if($attempt['new_land_id']): ?>
                        <a href="../view_land.php?id=<?php echo $attempt['new_land_id']; ?>" class="btn btn-sm btn-outline" target="_blank">
                            <i class="fas fa-eye"></i> View New Land
                        </a>
                        <?php endif; ?>
                        <a href="../view_land.php?id=<?php echo $attempt['existing_land_id']; ?>" class="btn btn-sm btn-outline" target="_blank">
                            <i class="fas fa-eye"></i> View Existing Land
                        </a>
                    </div>
                    <?php endif; ?>
                </div>
                <?php endwhile; ?>

                <?php if($total_pages > 1): ?>
                <div class="pagination">
                    <?php for($i = 1; $i <= $total_pages; $i++): ?>
                    <a href="?filter=<?php echo $filter; ?>&severity=<?php echo $severity_filter; ?>&page=<?php echo $i; ?>" class="<?php echo $i == $page ? 'active' : ''; ?>">
                        <?php echo $i; ?>
                    </a>
                    <?php endfor; ?>
                </div>
                <?php endif; ?>
                <?php endif; ?>
            </div>
        </div>
    </main>

    <!-- Review Modal -->
    <div class="modal-backdrop" id="modalBackdrop" onclick="closeModal()"></div>
    <div class="modal" id="reviewModal">
        <div class="modal-header">
            <h3 id="modalTitle">Review Attempt</h3>
            <button class="modal-close" onclick="closeModal()"><i class="fas fa-times"></i></button>
        </div>
        <form method="POST">
            <input type="hidden" name="attempt_id" id="modalAttemptId">
            <input type="hidden" name="action" id="modalAction">
            <div class="modal-body">
                <div class="form-group">
                    <label class="form-label">Review Notes (Optional)</label>
                    <textarea name="notes" class="form-control" rows="3" placeholder="Add any notes about this review..."></textarea>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline" onclick="closeModal()">Cancel</button>
                <button type="submit" class="btn btn-primary" id="modalSubmitBtn">Confirm</button>
            </div>
        </form>
    </div>

    <script>
        function openReviewModal(attemptId, action) {
            document.getElementById('modalAttemptId').value = attemptId;
            document.getElementById('modalAction').value = action;

            const titles = {
                'reviewed': 'Mark as Reviewed',
                'flagged': 'Flag as Suspicious',
                'cleared': 'Clear Attempt'
            };
            document.getElementById('modalTitle').textContent = titles[action] || 'Review Attempt';

            const btnClasses = {
                'reviewed': 'btn-primary',
                'flagged': 'btn-danger',
                'cleared': 'btn-success'
            };
            const btn = document.getElementById('modalSubmitBtn');
            btn.className = 'btn ' + (btnClasses[action] || 'btn-primary');

            document.getElementById('modalBackdrop').classList.add('show');
            document.getElementById('reviewModal').classList.add('show');
        }

        function closeModal() {
            document.getElementById('modalBackdrop').classList.remove('show');
            document.getElementById('reviewModal').classList.remove('show');
        }
    </script>
</body>
</html>
