<?php

/*
 * Following code will list all the products
 */
// array for JSON response
$response = array();

// include db connect class
include("db_connect.php");

// connecting to db
$con = new DB_CONNECT();
$connection = $con->connect();

$connection->query("SET NAMES 'utf8'");

$connection->query("SET CHARACTER SET utf8");

$connection->query("SET SESSION collation_connection = 'utf8_unicode_ci'");

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');

header('Content-Type: application/json');


// get all products from products table

if (isset($_GET['client_id'])) {

    $client_id = $_GET['client_id'];

    $result = $connection->query("SELECT * FROM `complaints` WHERE `client_id`='$client_id' ORDER BY `id` DESC ")  or die(mysqli_error($connection));

    // check for empty result
    if (mysqli_num_rows($result) > 0) {
        // looping through all results
        // products node

        $response["product"] = array();

        while ($row = mysqli_fetch_array($result)) {

            $product = array();
            $arr_mess = array();


            $product["complaint_id"] = $row['id'];
            $complaint_id = $row['id'];
            $product["content"] = $row['content'];
            $product["title"] = $row['title'];

            $product["date"] = $row['date'];
            $product["type"] = "1";

            array_push($arr_mess, $product);

            $result_2 = $connection->query("SELECT * FROM `messages` WHERE `complaint_id`='$complaint_id' order by `id` ASC")  or die(mysqli_error($connection));
            while ($row_2 = mysqli_fetch_array($result_2)) {

                $messages = array();

                $messages["content"] = $row_2["content"];
                $messages["is_read"] = $row_2["is_read"];
                $messages["date"] = $row_2["date"];
                $messages["type"] = $row_2["type"];


                array_push($arr_mess, $messages);
            }
            $product["messages"] = $arr_mess;

            array_push($response["product"], $product);
        }

        $response["success"] = 1;
        echo json_encode($response);
    } else {

        $response["product"] = array();

        // temp user array
        $product = array();

        // success
        $response["success"] = 1;

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // no products found
    $response["success"] = 0;
    $response["message"] = "هناك بيانات مفقوده برجاء مراجعة بياناتك";

    // echo no users JSON
    echo json_encode($response);
}
?>