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

	$session=$_REQUEST['session'];
	$array = array();
	$status=0;

    $attendanceArray=array();
    $homeworkArray=array();
    $classTeacherArray=array();
    $subjectTeacherArray=array();
    $cbseExamArray=array();
    $boardExamArray=array();

    $staff = $db->getRows("select * from staff where session='".$session."' and status=1 order by name");
    if(count($staff) > 0){
        $status = 1;
    }
        foreach($staff as $teacher){
            $classarray = "";
            $roles = $db->getRows("select * from Attandence_role where session='".$session."' and teacher_id=$teacher[idno] and school_id=1 order by section ASC ");
                foreach($roles as $role){
                    $classarray .= "$role[class] - "."$role[section], ";
                }
                array_push($attendanceArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class"=>"$classarray","image"=>"$teacher[photo]"]);
        }


        foreach($staff as $teacher){
            $classarray = "";
            $roles = $db->getRows("select * from Homework_role where session='".$session."' and teacher_id=$teacher[idno] and school_id=1 order by section ASC ");
                foreach($roles as $role){
                    $classarray .= "$role[class] - "."$role[section], ";
                }
                array_push($homeworkArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class"=>"$classarray","image"=>"$teacher[photo]"]);
        }


        foreach($staff as $teacher){
            $classarray = "";
            $roles = $db->getRows("select * from assign_teacher where session='".$session."' and teacher_id=$teacher[idno] and school_id=1 and is_class_teacher=1 order by section ASC ");
            foreach($roles as $role){
            $classarray .= "$role[class] - "."$role[section], ";
            }
        array_push($classTeacherArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class"=>"$classarray","subject"=>"$teacher[experience]","image"=>"$teacher[photo]"]);
        }


        foreach($staff as $teacher){
            $classarray = "";
            $roles = $db->getRows("select * from assign_teacher where session='".$session."' and teacher_id=$teacher[idno] and school_id=1 and is_class_teacher=0 order by section ASC ");
            foreach($roles as $role){
            $classarray .= "$role[class] - "."$role[section], ";
            }
        array_push($subjectTeacherArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class"=>"$classarray","subject"=>"$teacher[experience]","image"=>"$teacher[photo]"]);
        }


        foreach($staff as $teacher){
            $teachersubject = $db->getRows("select * from teacher_subject where teacher_id=$teacher[idno] and session='".$session."' and school_id=1 ");
            $cs="";
            $sub="";
            $classarray = array();
            foreach($teachersubject as $ts){
                $sec=json_decode($ts[section]);
                    $sec = implode(", ",$sec);
                    $classsubject = $db->getRow("select * from subject where id=$ts[cls_sub_id] order by class ASC");
                    array_push($classarray,["class"=>"$classsubject[class]", "section"=>"$sec", "subject"=>"$classsubject[subject]"]);
            }
            array_push($cbseExamArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class_subject"=>$classarray,"image"=>"$teacher[photo]"]);
        }


        foreach($staff as $teacher){
            $teachersubject = $db->getRows("select * from teacher_subject_mp where teacher_id=$teacher[idno] and session='".$session."' and school_id=1 ");
            $cs="";
            $sub="";
            $classarray = array();
            foreach($teachersubject as $ts){
                $sec=json_decode($ts[section]);
                    $sec = implode(", ",$sec);
                    $classsubject = $db->getRow("select * from subject_mp where id=$ts[cls_sub_id] order by class ASC");
                    array_push($classarray,["class"=>"$classsubject[class]", "section"=>"$sec", "subject"=>"$classsubject[subject]"]);
            }
            array_push($boardExamArray,["name"=>"$teacher[name]","mobile"=>"$teacher[mobile]","designation"=>"$teacher[designation]","class_subject"=>$classarray,"image"=>"$teacher[photo]"]);
        }


    $array['status'] = $status;
    $array['attendance_role'] = $attendanceArray;
    $array['homework_role'] = $homeworkArray;
    $array['class_teacher'] = $classTeacherArray;
    $array['subject_teacher'] = $subjectTeacherArray;
    $array['cbse_exam'] = $cbseExamArray;
    $array['board_exam'] = $boardExamArray;
    
    echo json_encode($array);
?>