<?php
require_once 'config/db.php';

// Handle Restore and Hard Delete
if (isset($_GET['action']) && isset($_GET['id'])) {
    $id = (int)$_GET['id'];
    $action = $_GET['action'];

    try {
        if ($action === 'restore') {
            $stmt = $pdo->prepare("UPDATE leads SET is_junk = 0 WHERE id = ?");
            $stmt->execute([$id]);
            $msg = "restored";
        } elseif ($action === 'hard_delete') {
            $stmt = $pdo->prepare("DELETE FROM leads WHERE id = ?");
            $stmt->execute([$id]);
            $msg = "deleted";
        }
        header("Location: leads_junk.php?msg=$msg");
        exit;
    } catch (PDOException $e) {
        $error = "Error: " . $e->getMessage();
    }
}

if (isset($_GET['msg'])) {
    if ($_GET['msg'] === 'restored') {
        $success = "Lead successfully restored to active list.";
    } elseif ($_GET['msg'] === 'deleted') {
        $success = "Lead permanently deleted from database.";
    }
}

// Fetch all Junk Leads
$sql = "
    SELECT l.*, 
           s.name as source_name, 
           st.name as status_name, st.color_code as status_color
    FROM leads l
    LEFT JOIN lead_sources s ON l.source_id = s.id
    LEFT JOIN lead_statuses st ON l.status_id = st.id
    WHERE l.is_junk = 1
    ORDER BY l.created_at DESC
";
$stmt = $pdo->query($sql);
$junk_leads = $stmt->fetchAll();

require_once 'includes/header.php';
?>

<div class="dashboard-content">
    <div class="welcome-banner" style="margin-bottom: 20px; padding: 20px; background: linear-gradient(135deg, #1e293b 0%, #3f3f46 100%);">
        <h2><i class="fa-solid fa-ban"></i> Junk Leads</h2>
        <p>Leads placed here are soft-deleted from the main dashboard. You can restore or permanently delete them.</p>
    </div>

    <?php if (isset($success)): ?>
        <div class="badge badge-completed" style="display:block; padding:15px; margin-bottom: 20px; font-size:1rem;"><?= htmlspecialchars($success) ?></div>
    <?php endif; ?>
    <?php if (isset($error)): ?>
        <div class="badge badge-pending" style="display:block; padding:15px; margin-bottom: 20px; background-color: #fee2e2; color: #ef4444; font-size:1rem;"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <div class="recent-activity" style="padding: 0;">
        <div class="table-responsive">
            <table class="admin-table">
                <thead>
                    <tr>
                        <th style="padding-left: 20px;">Lead Name & Contact</th>
                        <th>Associated Status</th>
                        <th>Original Source</th>
                        <th>Added On</th>
                        <th style="padding-right: 20px; text-align: right;">Options</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($junk_leads) > 0): ?>
                        <?php foreach ($junk_leads as $lead): ?>
                            <tr>
                                <td style="padding-left: 20px;">
                                    <div style="font-weight:700; color:var(--dark); font-size:1.05rem;"><?= htmlspecialchars($lead['name']) ?></div>
                                    <div style="font-size:0.85rem; margin-top:3px;"><i class="fa-solid fa-phone" style="color:var(--primary); font-size:0.8rem; margin-right:3px;"></i> <?= htmlspecialchars($lead['phone']) ?></div>
                                    <?php if(!empty($lead['email'])): ?>
                                        <div style="font-size:0.85rem; margin-top:2px; color:#64748b;"><i class="fa-solid fa-envelope" style="color:var(--primary); font-size:0.8rem; margin-right:3px;"></i> <?= htmlspecialchars($lead['email']) ?></div>
                                    <?php endif; ?>
                                </td>
                                
                                <td>
                                    <?php if ($lead['status_name']): ?>
                                        <span class="badge" style="background-color: <?= htmlspecialchars($lead['status_color']) ?>20; color: <?= htmlspecialchars($lead['status_color']) ?>; border: 1px solid <?= htmlspecialchars($lead['status_color']) ?>50;">
                                            <?= htmlspecialchars($lead['status_name']) ?>
                                        </span>
                                    <?php else: ?>
                                        <span style="color:#94a3b8; font-size:0.85rem;">Unassigned</span>
                                    <?php endif; ?>
                                </td>
                                
                                <td>
                                    <span style="font-size: 0.9rem; color: #475569; font-weight: 500;"><i class="fa-solid fa-link" style="margin-right:4px; color:#94a3b8;"></i> <?= htmlspecialchars($lead['source_name'] ?? 'Unknown') ?></span>
                                </td>
                                
                                <td style="color:#64748b; font-size:0.85rem;">
                                    <?= date('M d, Y', strtotime($lead['created_at'])) ?>
                                </td>

                                <td style="text-align: right; padding-right: 20px;">
                                    <a href="leads_junk.php?action=restore&id=<?= $lead['id'] ?>" title="Restore Lead to Main Board" onclick="return confirm('Restore this lead back to active status?');" style="color:#10b981; display:inline-flex; align-items:center; justify-content:center; padding: 8px 15px; font-size:0.9rem; background:#d1fae5; border-radius:6px; margin-right:4px; text-decoration: none; font-weight: 600;"><i class="fa-solid fa-rotate-left" style="margin-right: 8px;"></i> Restore</a>
                                    
                                    <a href="leads_junk.php?action=hard_delete&id=<?= $lead['id'] ?>" title="Permanently Delete" onclick="return confirm('WARNING: Permanently delete this lead? This cannot be undone once deleted from junk.');" style="color:var(--danger); display:inline-flex; align-items:center; justify-content:center; width:38px; height:38px; background:#fee2e2; border-radius:6px;"><i class="fa-solid fa-trash"></i></a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="5" style="text-align: center; color: #94a3b8; padding: 40px 20px;">
                                <i class="fa-solid fa-trash-can-arrow-up" style="font-size: 2rem; color: #cbd5e1; margin-bottom: 10px; display: block;"></i>
                                Your junk folder is currently empty.
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
