<?php
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST');
    header("Access-Control-Allow-Headers: X-Requested-With");
    include '../../config/index.php';
    $data = array();
  
    $sql_4="SELECT * FROM parentInvoices  WHERE  Status='Paid'  ORDER BY dated DESC    ";
    $result_4 = mysqli_query($conn, $sql_4);  
    if (mysqli_num_rows($result_4) > 0) {
        while($row_4 = mysqli_fetch_assoc($result_4)) {
            $p_name; $p_last_name;
            $p_email; $p_phone;
            $sql_d="SELECT * FROM parents WHERE parent_id='".$row_4["parent_id"]."'";
            $result_d = mysqli_query($conn, $sql_d);  
            if (mysqli_num_rows($result_d) > 0) {
                while($row_d = mysqli_fetch_assoc($result_d)) {
                    $p_name=$row_d['p_name'];
                    $p_last_name=$row_d['p_last_name'];
                    $p_email=$row_d['p_email'];
                    $p_phone=$row_d['p_phone'];
                }
            }  

            $center_name;
            $sql_d="SELECT * FROM digicenters WHERE center_id='".$row_4["center_id"]."'";
            $result_d = mysqli_query($conn, $sql_d);  
            if (mysqli_num_rows($result_d) > 0) {
                while($row_d = mysqli_fetch_assoc($result_d)) {
                    $center_name=$row_d['center_name'];
                 
                }
            }   
            $course_name;
              
            $sql_d="SELECT * FROM courses WHERE course_id='".$row_4["course_id"]."'";
            $result_d = mysqli_query($conn, $sql_d);  
            if (mysqli_num_rows($result_d) > 0) {
                while($row_d = mysqli_fetch_assoc($result_d)) {
                    $course_name=$row_d['course_name'];
                 }
            } 
            $students = array();
            $sql_d="SELECT * FROM students WHERE parent_id='".$row_4["parent_id"]."' AND flag_2='".$row_4["parentInvoices_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;
                    $sql_d="SELECT * FROM centerClasses WHERE centerClasses_id='".$row_d["centerClasses_id"]."'";
                    $result_d = mysqli_query($conn, $sql_d);  
                    if (mysqli_num_rows($result_d) > 0) {
                        while($row_d = mysqli_fetch_assoc($result_d)) {
                            $sql_d="SELECT * FROM classes WHERE class_id='".$row_4["class_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=$row_d['class_name'];
                                }
                            }        
                        }
                    }else{

                    }
                    $curl_post_data = array(
                        'class_name' => $class_name,
                        'parent_id' => $row_4["parent_id"],
                        'student_id' => $row_d["student_id"],
                        'center_id' => $row_d["center_id"],
                        'parentInvoices_id' => $row_4["parentInvoices_id"],
                        'centerClasses_id' => $row_d["centerClasses_id"],
                        'learner_name' => $row_d["learner_name"], 
                        'learner_last_name' => $row_d["learner_last_name"],
                        'learner_age' => $row_d["learner_age"],
                        'learner_image' => $row_d["learner_image"],
                        'status' => $row_d["status"], 
                    );
                    array_push($students, $curl_post_data );
                }
            }  
            
         
            $curl_post_data = array( 
                'parent_id' => $row_4["parent_id"],
                'course_id' => $row_4["course_id"],
                'NoKids' => $row_4["NoKids"],
                'Price' => $row_4["Price"], 
                'invoice_no' => $row_4["invoice_no"],
                'invoice_date' => $row_4["invoice_date"],
                'invoice_due_date' => $row_4["invoice_due_date"],
                'Status' => $row_4["Status"], 
                'ParentName' => $p_name,
                'ParentLastName' => $p_last_name,
                'ParentEmail' => $p_email,
                'ParentPhone' => $p_phone,
                'center_name' => $center_name,
                'course_name' => $course_name,
                'students' => $students,
            );
            array_push($data, $curl_post_data );    
        }
    }

   

    echo json_encode($data);
?>