<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, Authorization");
include '../../../config/index.php';
$data = array();
$sql_10P = "SELECT * FROM parents";
$result_10P = mysqli_query($conn, $sql_10P);
if (mysqli_num_rows($result_10P) > 0) {
    while ($row_10P = mysqli_fetch_assoc($result_10P)) {
        $students = array();
        $sql_ps = "SELECT * FROM ParentStudents   WHERE parent_id='" . $row_10P["parent_id"] . "'  AND center_id='" . $_GET["center_id"] . "' ";

        $result_ps = mysqli_query($conn, $sql_ps);
        $NoStudents = mysqli_num_rows($result_ps);
        if (mysqli_num_rows($result_ps) > 0) {
            while ($row_ps = mysqli_fetch_assoc($result_ps)) {

                $sql_d = "SELECT * FROM students WHERE  center_id='" . $_GET["center_id"] . "' AND   ParentStudents_id='" . $row_ps["ParentStudents_id"] . "' AND student_id='" . $row_ps["student_id"] . "'";
                $result_d = mysqli_query($conn, $sql_d);
                if (mysqli_num_rows($result_d) > 0) {
                    while ($row_d = mysqli_fetch_assoc($result_d)) {
                        $class_name;
                        $StreamName;
                        $sql_clp = "SELECT * FROM centerClasses WHERE  centerClasses_id='" . $row_d['centerClasses_id'] . "'";
                        $result_clp = mysqli_query($conn, $sql_clp);
                        if (mysqli_num_rows($result_clp) > 0) {
                            while ($row_clp = mysqli_fetch_assoc($result_clp)) {

                                $sql_10 = "SELECT * FROM classes WHERE  class_id='" . $row_clp['class_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                if (mysqli_num_rows($result_10) > 0) {
                                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                                        $class_name = $row_10["class_name"];
                                    }
                                }
                                $sql_10 = "SELECT * FROM Streams WHERE  Streams_id='" . $row_clp['Streams_id'] . "'";
                                $result_10 = mysqli_query($conn, $sql_10);
                                if (mysqli_num_rows($result_10) > 0) {
                                    while ($row_10 = mysqli_fetch_assoc($result_10)) {
                                        $StreamName = $row_10["StreamName"];
                                    }
                                }
                            }
                        }

                        $curl_post_data = array(
                            'class_name' => $class_name,
                            'student_id' => $row_d["student_id"],
                            'parent_id' => $row_d["parent_id"],
                            'center_id' => $row_d["center_id"],
                            'centerClasses_id' => $row_d["centerClasses_id"],
                            'learner_name' => $row_d["learner_name"],
                            'StreamName' => $StreamName,
                            'learner_last_name' => $row_d["learner_last_name"],
                            'learner_age' => $row_d["learner_age"],
                            'learner_image' => $row_d["learner_image"],
                            'learner_gender' => $row_d["learner_gender"],
                            'status' => $row_d["status"],
                            'homeschool' => $row_d["homeschool"],
                            'username' => $row_d["username"],
                            'password' => $row_d["password"],
                            'reset' => $row_d["reset"],
                        );
                        array_push($students, $curl_post_data);
                        $class_name = "";
                        $StreamName = "";
                    }
                }
            }
            $curl_post_data = array(
                'parent_id' => $row_10P["parent_id"],
                'p_name' => $row_10P["p_name"],
                'p_last_name' => $row_10P["p_last_name"],
                'p_email' => $row_10P["p_email"],
                'p_phone' => $row_10P["p_phone"],
                'p_image' => $row_10P["p_image"],
                'p_id_number' => $row_10P["p_id_number"],
                'p_age' => $row_10P["p_age"],
                'p_gender' => $row_10P["p_gender"],
                'p_refugee' => $row_10P["p_refugee"],
                'disability_status' => $row_10P["disability_status"],
                'p_location' => $row_10P["p_location"],
                'p_status' => $row_10P["p_status"],
                'p_relationship' => $row_10P["p_relationship"],
                'flag_1' => $row_10P["flag_1"],
                'students' => $students,
            );
            array_push($data, $curl_post_data);
        }
    }
}

echo json_encode($data);
