File size: 4,024 Bytes
0dff816
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
session_start();
header('Content-Type: application/json');

// Check if user is logged in
if (!isset($_SESSION['logged_in']) || $_SESSION['logged_in'] !== true) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

// Include database and classes
include_once '../../db.php';
include_once '../models/User.php';
include_once '../models/SupportTicket.php';

// Define TicketHandler class properly
class TicketHandler {
    public static function notifyCustomerCare($ticket_number, $data) {
        // Build message
        $message = "New Support Ticket Created:\n";
        $message .= "Ticket Number: $ticket_number\n";
        $message .= "Issue Type: " . $data['issue_type'] . "\n";
        $message .= "Subject: " . $data['subject'] . "\n";
        $message .= "Priority: " . $data['priority'] . "\n";

        // Example: Send email notification
        $to = "customercare@jmotors.com";
        $subject = "New Support Ticket: $ticket_number";
        $headers = "From: support@jmotors.com\r\n";

        @mail($to, $subject, $message, $headers);

        // Optional: Add WhatsApp, Slack, or API integration here
    }
}

// Get POST data
$data = json_decode(file_get_contents('php://input'), true);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    try {
        // Validate required fields
        $required = ['issue_type', 'subject', 'description', 'priority'];
        foreach ($required as $field) {
            if (empty($data[$field])) {
                throw new Exception("Missing required field: $field");
            }
        }

        // Initialize database connection
        $database = new Database();
        $db = $database->getConnection();

        // Sync user data
        $user = new User($db);
        $user_id = $user->syncUser(
            $_SESSION['username'],
            $_SESSION['email'],
            $_SESSION['tier'],
            $_SESSION['package']
        );

        if (!$user_id) {
            throw new Exception("Failed to sync user data");
        }

        // Handle file uploads
        $attachments = [];
        if (!empty($_FILES['attachments'])) {
            $upload_dir = '../uploads/support/';
            if (!is_dir($upload_dir)) {
                mkdir($upload_dir, 0755, true);
            }

            foreach ($_FILES['attachments']['tmp_name'] as $key => $tmp_name) {
                if ($_FILES['attachments']['error'][$key] === UPLOAD_ERR_OK) {
                    $file_name = time() . '_' . basename($_FILES['attachments']['name'][$key]);
                    $file_path = $upload_dir . $file_name;
                    
                    if (move_uploaded_file($tmp_name, $file_path)) {
                        $attachments[] = $file_path;
                    }
                }
            }
        }

        // Create support ticket
        $ticket = new SupportTicket($db);
        $ticket->user_id = $user_id;
        $ticket->issue_type = $data['issue_type'];
        $ticket->subject = $data['subject'];
        $ticket->description = $data['description'];
        $ticket->priority = $data['priority'];
        $ticket->attachments = json_encode($attachments);

        $ticket_number = $ticket->create();

        if ($ticket_number) {
            // ✅ Correct way to call notification
            TicketHandler::notifyCustomerCare($ticket_number, $data);

            echo json_encode([
                'success' => true,
                'message' => 'Support ticket submitted successfully',
                'ticket_number' => $ticket_number
            ]);
        } else {
            throw new Exception("Failed to create support ticket");
        }
        

    } catch (Exception $e) {
        http_response_code(400);
        echo json_encode([
            'success' => false,
            'message' => $e->getMessage()
        ]);
    }
} else {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Method not allowed']);
}
?>