<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

date_default_timezone_set('Asia/Kolkata');
$current_date = date('Y-m-d');

 	$session=$_REQUEST['session'];

	$array = array();
	$status=0;
	$msg='';

    $teacherArray = array();


    $allteachers=$db->getRows("select * from staff where channel_id!='' and status=1 and school_id=1 and session='$session' ");

    if(is_array($allteachers) && count($allteachers)>0){
        $status=1;
        $msg='Profile found successfully';
        foreach($allteachers as $teacher){
            $currentlive = $db->getRow("select * from liveclass where channel_id='$teacher[channel_id]' and session='$session' order by id DESC limit 1 ");
            $live_class = array("id"=>"$currentlive[id]", "name"=>"$currentlive[name]", "detail"=>"$currentlive[detail]","video_url"=>"$currentlive[video_url]", "video_id"=>"$currentlive[video_id]", "broadcastid"=>"$currentlive[broadcastid]", "streamid"=>"$currentlive[streamid]", "status"=>"$currentlive[status]");

            $classArray = array();

            $liveclasses = $db->getRows("select * from live_upcoming_class where teacher='$teacher[idno]' and date >= '$current_date' order by date DESC");
            foreach($liveclasses as $live){
                array_push($classArray,["id"=>"$live[id]", "date"=>"$live[date]", "time"=>"$live[time]","class"=>"$live[class]","chapter"=>"$live[chapter]","topic"=>"$live[topic]"]);
            }

            $previousArray = array();
            $previousclass = $db->getRows("select * from liveclass where channel_id='$teacher[channel_id]' and status!=1 and session='$session' ORDER by id DESC  LIMIT 1, 9999999999");
            foreach($previousclass as $prevclass){
                array_push($previousArray,["id"=>"$prevclass[id]", "name"=>"$prevclass[name]", "detail"=>"$prevclass[detail]","video_url"=>"$prevclass[video_url]","video_id"=>"$prevclass[video_id]","broadcastid"=>"$prevclass[broadcastid]", "streamid"=>"$prevclass[streamid]", "status"=>"$prevclass[status]"]);
            }

            array_push($teacherArray, ["idno"=>"$teacher[idno]", "name"=>"$teacher[name]", "fname"=>"$teachher[fname]", "subject"=>"$teacher[experience]", "dob"=>"$teacher[dob]", "qualification"=>"$teacher[qualification]", "mobile"=>"$teacher[mobile]", "address"=>"$teacher[address]", 
            "department"=>"$teacher[department]", "photo"=>"$teacher[photo]", "channel_id"=>"$teacher[channel_id]","live_class"=>$live_class, "upcoming_class"=>$classArray, "previous_classes"=>$previousArray ]);

        }
    }

    $array['status'] = $status;
    $array['msg'] = $msg;
    $array['data'] = $teacherArray;

    echo json_encode($array);
?>