Path : /home/vishqocm/pcib.in/student/
File Upload :
Current File : /home/vishqocm//pcib.in/student/support.php

<?php
$pageTitle = "Support Center";
include_once('includes/header.php');

// Check if user is logged in
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'student') {
    header("Location: ../login.php");
    exit();
}

// Create tables if they don't exist
$conn->query("
    CREATE TABLE IF NOT EXISTS `support_tickets` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ticket_id` varchar(20) NOT NULL,
      `student_id` int(11) NOT NULL,
      `subject` varchar(255) NOT NULL,
      `message` text NOT NULL,
      `category` varchar(50) NOT NULL,
      `priority` varchar(20) NOT NULL DEFAULT 'medium',
      `status` enum('open','in_progress','resolved','closed') NOT NULL DEFAULT 'open',
      `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `updated_at` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
      `course_id` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `ticket_id` (`ticket_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
");

$conn->query("
    CREATE TABLE IF NOT EXISTS `support_ticket_replies` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ticket_id` int(11) NOT NULL,
      `user_type` enum('admin','student') NOT NULL,
      `user_id` int(11) NOT NULL,
      `message` text NOT NULL,
      `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      KEY `ticket_id` (`ticket_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
");

// Get student ID
$student_id = $_SESSION['user_id'];

// Get all support tickets for this student
$query = "
    SELECT t.*, 
           (SELECT COUNT(*) FROM support_ticket_replies WHERE ticket_id = t.id) as reply_count,
           (SELECT MAX(created_at) FROM support_ticket_replies WHERE ticket_id = t.id) as last_reply,
           c.title as course_name
    FROM support_tickets t
    LEFT JOIN courses c ON t.course_id = c.id
    WHERE t.student_id = ?
    ORDER BY 
    CASE 
        WHEN t.status = 'open' THEN 1
        WHEN t.status = 'in_progress' THEN 2
        WHEN t.status = 'resolved' THEN 3
        WHEN t.status = 'closed' THEN 4
    END,
    t.updated_at DESC, t.created_at DESC
";

$stmt = $conn->prepare($query);
$stmt->bind_param("i", $student_id);
$stmt->execute();
$result = $stmt->get_result();
$tickets = [];

while ($row = $result->fetch_assoc()) {
    $tickets[] = $row;
}

// Count tickets by status
$ticket_counts = [
    'open' => 0,
    'in_progress' => 0,
    'resolved' => 0,
    'closed' => 0,
    'total' => count($tickets)
];

foreach ($tickets as $ticket) {
    if (isset($ticket_counts[$ticket['status']])) {
        $ticket_counts[$ticket['status']]++;
    }
}

// Define categories
$categories = [
    'account' => 'Account Access',
    'payment' => 'Payment Issues',
    'enrollment' => 'Course Enrollment',
    'certificate' => 'Certificate Issues',
    'exam' => 'Exam Related',
    'technical' => 'Technical Support',
    'general' => 'General Information',
    'other' => 'Other'
];

// Define status labels
$status_labels = [
    'open' => ['label' => 'Open', 'class' => 'bg-danger'],
    'in_progress' => ['label' => 'In Progress', 'class' => 'bg-warning'],
    'resolved' => ['label' => 'Resolved', 'class' => 'bg-success'],
    'closed' => ['label' => 'Closed', 'class' => 'bg-secondary']
];

// Define priority labels
$priority_labels = [
    'low' => ['label' => 'Low', 'class' => 'bg-info'],
    'medium' => ['label' => 'Medium', 'class' => 'bg-primary'],
    'high' => ['label' => 'High', 'class' => 'bg-danger']
];
?>

<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="h3">Support Tickets</h2>
        <a href="create_ticket.php" class="btn btn-primary">
            <i class="fas fa-plus me-1"></i> Create New Ticket
        </a>
    </div>
    
    <!-- Status summary cards -->
    <div class="row mb-4">
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="card h-100 border-left-primary">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-primary text-uppercase mb-1">Open Tickets</div>
                            <div class="h5 mb-0 font-weight-bold"><?php echo $ticket_counts['open']; ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-ticket-alt fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="card h-100 border-left-warning">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-warning text-uppercase mb-1">In Progress</div>
                            <div class="h5 mb-0 font-weight-bold"><?php echo $ticket_counts['in_progress']; ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-spinner fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="card h-100 border-left-success">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-success text-uppercase mb-1">Resolved</div>
                            <div class="h5 mb-0 font-weight-bold"><?php echo $ticket_counts['resolved']; ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-check-circle fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="col-lg-3 col-md-6 mb-3">
            <div class="card h-100 border-left-secondary">
                <div class="card-body">
                    <div class="row align-items-center">
                        <div class="col mr-2">
                            <div class="text-xs font-weight-bold text-secondary text-uppercase mb-1">Total Tickets</div>
                            <div class="h5 mb-0 font-weight-bold"><?php echo $ticket_counts['total']; ?></div>
                        </div>
                        <div class="col-auto">
                            <i class="fas fa-clipboard-list fa-2x text-gray-300"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Tickets table -->
    <div class="card">
        <div class="card-body">
            <?php if (count($tickets) > 0): ?>
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th width="12%">Ticket #</th>
                            <th width="25%">Subject</th>
                            <th width="15%">Category</th>
                            <th width="10%">Priority</th>
                            <th width="12%">Status</th>
                            <th width="15%">Last Update</th>
                            <th width="11%">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($tickets as $ticket): ?>
                        <tr>
                            <td>
                                <a href="view_ticket.php?id=<?php echo $ticket['id']; ?>" class="text-decoration-none">
                                    <?php echo htmlspecialchars($ticket['ticket_id']); ?>
                                </a>
                                <?php if ($ticket['reply_count'] > 1): ?>
                                <span class="badge bg-primary rounded-pill ms-1"><?php echo $ticket['reply_count'] - 1; ?></span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <div class="text-truncate" style="max-width: 250px;">
                                    <a href="view_ticket.php?id=<?php echo $ticket['id']; ?>" class="text-decoration-none">
                                        <?php echo htmlspecialchars($ticket['subject']); ?>
                                    </a>
                                </div>
                                <?php if (!empty($ticket['course_name'])): ?>
                                <small class="text-muted d-block">Course: <?php echo htmlspecialchars($ticket['course_name']); ?></small>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if (!empty($ticket['category'])): ?>
                                <span class="badge bg-info text-dark">
                                    <?php echo isset($categories[$ticket['category']]) ? 
                                        htmlspecialchars($categories[$ticket['category']]) : 
                                        htmlspecialchars($ticket['category']); ?>
                                </span>
                                <?php else: ?>
                                <span class="text-muted">-</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="badge <?php echo $priority_labels[$ticket['priority']]['class']; ?>">
                                    <?php echo $priority_labels[$ticket['priority']]['label']; ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge <?php echo $status_labels[$ticket['status']]['class']; ?>">
                                    <?php echo $status_labels[$ticket['status']]['label']; ?>
                                </span>
                            </td>
                            <td>
                                <?php 
                                $update_date = !empty($ticket['last_reply']) ? $ticket['last_reply'] : 
                                              (!empty($ticket['updated_at']) ? $ticket['updated_at'] : $ticket['created_at']);
                                $date = new DateTime($update_date);
                                $now = new DateTime();
                                $diff = $now->diff($date);
                                
                                if ($diff->days == 0) {
                                    if ($diff->h == 0) {
                                        echo $diff->i . ' minutes ago';
                                    } else {
                                        echo $diff->h . ' hours ago';
                                    }
                                } elseif ($diff->days == 1) {
                                    echo 'Yesterday';
                                } else {
                                    echo $date->format('M d, Y');
                                }
                                ?>
                            </td>
                            <td>
                                <a href="view_ticket.php?id=<?php echo $ticket['id']; ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-eye me-1"></i> View
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php else: ?>
            <div class="text-center py-5">
                <img src="../assets/img/empty-tickets.svg" alt="No Tickets" class="img-fluid mb-3" style="max-width: 150px;">
                <h5>No Support Tickets Found</h5>
                <p class="text-muted">You haven't created any support tickets yet.</p>
                <a href="create_ticket.php" class="btn btn-primary mt-2">
                    <i class="fas fa-plus me-1"></i> Create Your First Ticket
                </a>
            </div>
            <?php endif; ?>
        </div>
    </div>
    
    <!-- Help card -->
    <div class="card mt-4">
        <div class="card-header bg-light">
            <h5 class="mb-0">Support Information</h5>
        </div>
        <div class="card-body">
            <div class="row">
                <div class="col-md-6">
                    <h6>Need Help?</h6>
                    <p>Our support team is here to help you with any questions or issues you may have.</p>
                    <ul class="mb-0">
                        <li>Check our <a href="faq.php">Frequently Asked Questions</a></li>
                        <li>For urgent issues, call our helpline: <strong>(123) 456-7890</strong></li>
                        <li>Support hours: Monday-Friday, 9:00 AM - 6:00 PM</li>
                    </ul>
                </div>
                <div class="col-md-6">
                    <h6>Ticket Status Explained</h6>
                    <ul class="list-unstyled mb-0">
                        <li class="mb-2">
                            <span class="badge bg-danger">Open</span>
                            <span class="ms-2">Your ticket has been received and is awaiting response</span>
                        </li>
                        <li class="mb-2">
                            <span class="badge bg-warning">In Progress</span>
                            <span class="ms-2">Our team is actively working on your issue</span>
                        </li>
                        <li class="mb-2">
                            <span class="badge bg-success">Resolved</span>
                            <span class="ms-2">Your issue has been resolved</span>
                        </li>
                        <li>
                            <span class="badge bg-secondary">Closed</span>
                            <span class="ms-2">The ticket has been closed</span>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include_once('includes/footer.php'); ?>