<?php
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Methods: HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method,Access-Control-Request-Headers, Authorization");
    header("HTTP/1.1 200 OK");
    die();
}
require '../../../../config/db.php';
$data = [];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $query = "SELECT *, mc.messageCategory as messageCategoryName FROM Messages m INNER JOIN MessageCategory mc ON m.messageCategory = mc.messageCategoryId WHERE m.messageCategory=3 ORDER BY m.dateCreated";

    if ($result = mysqli_query($con, $query)) {
        $i = 0;
        while ($rows = mysqli_fetch_assoc($result)) {
            $data[$i]['messageId'] = $rows['messageId'];
            $data[$i]['messageCategoryName'] = $rows['messageCategoryName'];
            $data[$i]['fname'] = $rows['fname'];
            $data[$i]['lname'] = $rows['lname'];
            $data[$i]['email'] = $rows['email'];
            $data[$i]['phone'] = $rows['phone'];
            $data[$i]['message'] = $rows['message'];
            $data[$i]['ratings'] = $rows['ratings'];
            $data[$i]['status'] = $rows['status'];
            $data[$i]['dateCreated'] = $rows['dateCreated'];
            $i++;
        }
        $response = array(
            'status' => '1',
            'data' => $data
        );
        echo json_encode($response);
    }
} else {
    echo json_encode([
        'status' => -1
    ]);
    die();
}
