<?php
/**
 * Advanced Support Ticket Management (Pro Logic)
 */
require_once 'header.php';

$success = '';
$error = '';

// ১. টিকিট স্ট্যাটাস আপডেট বা ডিলিট লজিক
if (isset($_GET['action']) && isset($_GET['id'])) {
    $tid = (int)$_GET['id'];
    $act = $_GET['action'];

    try {
        if ($act === 'close') {
            $pdo->prepare("UPDATE support_tickets SET status = 'closed' WHERE id = ?")->execute([$tid]);
            $success = "Ticket #$tid has been closed.";
        } elseif ($act === 'delete') {
            $pdo->prepare("DELETE FROM support_tickets WHERE id = ?")->execute([$tid]);
            $success = "Ticket deleted successfully.";
        }
    } catch (PDOException $e) {
        $error = "Action failed: " . $e->getMessage();
    }
}

// ২. সব টিকিট তুলে আনা (ইউজার ইনফো সহ)
try {
    $query = "
        SELECT t.*, u.name as user_name, u.email as user_email 
        FROM support_tickets t
        LEFT JOIN users u ON t.user_id = u.id
        ORDER BY t.id DESC
    ";
    $tickets = $pdo->query($query)->fetchAll();
} catch (PDOException $e) {
    // যদি টেবিল না থাকে তবে অটো ক্রিয়েট লজিক (সেফটি)
    $error = "Table issue: " . $e->getMessage();
}
?>

<style>
    /* Premium Ticket Cards */
    .ticket-card { background: #fff; border-radius: 12px; border: 1px solid var(--border-color); padding: 20px; margin-bottom: 20px; transition: 0.3s; position: relative; }
    .ticket-card:hover { border-color: var(--primary-orange); box-shadow: 0 4px 12px rgba(0,0,0,0.05); }
    
    .ticket-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 15px; flex-wrap: wrap; gap: 10px; }
    .ticket-id { font-size: 0.8rem; font-weight: 800; color: var(--text-muted); text-transform: uppercase; background: #f1f5f9; padding: 3px 8px; border-radius: 4px; }
    
    .ticket-subject { font-size: 1.1rem; font-weight: 800; color: var(--text-dark); margin: 5px 0; display: block; }
    .ticket-msg { color: var(--text-gray); font-size: 0.9rem; line-height: 1.5; background: #f9fafb; padding: 15px; border-radius: 8px; margin: 10px 0; border-left: 4px solid var(--border-color); }
    
    /* User & Status Info */
    .meta-info { display: flex; align-items: center; gap: 15px; font-size: 0.85rem; color: var(--text-gray); font-weight: 600; flex-wrap: wrap; }
    .user-tag { color: var(--primary-orange); font-weight: 700; }
    
    .status-pill { padding: 4px 10px; border-radius: 20px; font-size: 0.7rem; font-weight: 800; text-transform: uppercase; }
    .status-open { background: #fee2e2; color: #ef4444; border: 1px solid #fecaca; }
    .status-answered { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
    .status-closed { background: #f3f4f6; color: #6b7280; border: 1px solid #d1d5db; }

    .action-row { margin-top: 15px; padding-top: 15px; border-top: 1px solid #f1f5f9; display: flex; gap: 10px; }
    .btn-ticket { padding: 8px 15px; border-radius: 6px; font-size: 0.8rem; font-weight: 700; text-decoration: none; transition: 0.2s; }
    .btn-reply { background: var(--primary-orange); color: white; }
    .btn-close { background: #f3f4f6; color: var(--text-dark); border: 1px solid var(--border-color); }
    .btn-del { color: #ef4444; margin-left: auto; }

    @media (max-width: 600px) {
        .ticket-card { padding: 15px; }
        .ticket-subject { font-size: 1rem; }
    }
</style>

<div class="page-header">
    <h1 class="page-title">Customer Support Tickets</h1>
</div>

<?php if($success): ?> <div class="alert-box alert-success">✓ <?php echo $success; ?></div> <?php endif; ?>
<?php if($error): ?> <div class="alert-box alert-danger">⚠ <?php echo $error; ?></div> <?php endif; ?>

<div class="ticket-list">
    <?php foreach($tickets as $t): ?>
    <div class="ticket-card">
        <div class="ticket-header">
            <div>
                <span class="ticket-id">#TICK-<?php echo $t['id']; ?></span>
                <span class="ticket-subject"><?php echo htmlspecialchars($t['subject']); ?></span>
            </div>
            <span class="status-pill status-<?php echo $t['status']; ?>"><?php echo $t['status']; ?></span>
        </div>

        <div class="ticket-msg">
            <?php echo nl2br(htmlspecialchars($t['message'])); ?>
        </div>

        <div class="meta-info">
            <span>By: <span class="user-tag"><?php echo htmlspecialchars($t['user_name'] ?? 'Guest'); ?></span></span>
            <span>Email: <?php echo htmlspecialchars($t['user_email'] ?? 'N/A'); ?></span>
            <span>Date: <?php echo date('d M, h:i A', strtotime($t['created_at'])); ?></span>
        </div>

        <div class="action-row">
            <a href="mailto:<?php echo $t['user_email']; ?>?subject=RE: <?php echo urlencode($t['subject']); ?>" class="btn-ticket btn-reply">Send Email Reply</a>
            
            <?php if($t['status'] !== 'closed'): ?>
                <a href="?action=close&id=<?php echo $t['id']; ?>" class="btn-ticket btn-close" onclick="return confirm('Close this ticket?')">Mark Closed</a>
            <?php endif; ?>
            
            <a href="?action=delete&id=<?php echo $t['id']; ?>" class="btn-ticket btn-del" onclick="return confirm('Delete permanently?')">
                <svg width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24" style="vertical-align: middle;"><path d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
            </a>
        </div>
    </div>
    <?php endforeach; ?>

    <?php if(empty($tickets)): ?>
        <div class="admin-card" style="text-align: center; padding: 50px; color: var(--text-muted);">
            <svg width="48" height="48" fill="none" stroke="currentColor" stroke-width="1.5" style="margin-bottom: 15px; opacity: 0.5;"><path d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"/></svg>
            <div style="font-weight: 700; font-size: 1.1rem;">No support tickets found</div>
            <p>Customer queries will appear here.</p>
        </div>
    <?php endif; ?>
</div>

<?php echo '</div></div></body></html>'; ?>