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

	$teacher_id=$_REQUEST['teacher_id'];
	$array = array();
	$status=0;
	
    $classArray = array();
    $sectionArray = array();
    $cls_secArray = array();
    $examArray = array();
    
    $tableinfo=$db->getRow("select * from tbl_info");

    if($tableinfo[result_pattern]==1){

        if($tableinfo[teacher_sub_control]==1){

            $allClasses= $db->getRows("select * from class where school_id=1 ");
            if(count($allClasses) > 0){
                $status = 1;
                
                foreach($allClasses as $class){
                    $classArray[]=$class['class'];
                }
            }
            

            $allSection=$db->getRows("select * from section where school_id=1 ");
            if(count($allSection) > 0){
                $status = 1;
                foreach($allSection as $section){
                $sectionArray[] = $section['name'];
            }
            }
            

            $examTerm=$db->getRows("select name,total from examterm");
            if(count($examTerm) > 0){
                $status = 1;
                foreach($examTerm as $term){
                array_push($examArray,["exam_name"=>$term[name],"max_mark"=>$term[total]]);
            }
            }

        }
        
        if($tableinfo[teacher_sub_control]==2){
            
            $allClasses= $db->getRows("select class from teacher_subject where teacher_id=$teacher_id and school_id=1 group by class ");
            if(count($allClasses) > 0){
                $status = 1;
                foreach($allClasses as $class){
                    $classArray[]=$class['class'];
                }
            }

            $allClasses1= $db->getRows("select class,section from teacher_subject where teacher_id=$teacher_id and school_id=1 order by class ");

            if(count($allClasses) > 0){
                $status = 1;
                $temp_arr = array();
                foreach($allClasses1 as $class){
                    $cls_qry = $class['class'];
                    $sec_qry = json_decode($class['section']);

                    if(array_key_exists($cls_qry,$temp_arr)){
                        $old_sec = $temp_arr[$cls_qry];
                        $temp_arr[$cls_qry] = array_values(array_unique(array_merge($old_sec,$sec_qry)));
                    }else{
                        $temp_arr[$cls_qry] = $sec_qry;
                    }

                }

                foreach($temp_arr as $key => $val) {
                    sort($val);
                    array_push($cls_secArray,array("class"=>$key,"sec"=>$val));
                }
            }


            $allSection=$db->getRows("select * from section where school_id=1 ");
            if(count($allSection) > 0){
                $status = 1;
                foreach($allSection as $section){
                    $sectionArray[] = $section['name'];
                }
            }


            $examTerm=$db->getRows("select name,total from examterm");
            if(count($examTerm) > 0){
                $status = 1;
                foreach($examTerm as $term){
                    array_push($examArray,["exam_name"=>$term[name],"max_mark"=>$term[total]]);
                }
            }
            

        }

    }


    if($tableinfo[result_pattern]==2){

        if($tableinfo[teacher_sub_control]==1){

            $allClasses= $db->getRows("select * from class where school_id=1 ");
            if(count($allClasses) > 0){
                $status = 1;
                foreach($allClasses as $class){
                $classArray[]=$class['class'];
            }
            }
            

            $allSection=$db->getRows("select * from section where school_id=1 ");
            if(count($allSection) > 0){
                $status = 1;
                foreach($allSection as $section){
                $sectionArray[] = $section['name'];
            }
            }
            

            $examTerm=$db->getRows("select  type,total from examtype group by type order by id ");
            if(count($examTerm) > 0){
                $status = 1;
                foreach($examTerm as $term){
                    array_push($examArray,["exam_name"=>$term[type],"max_mark"=>$term[total]]);
                }
            }

        }

        if($tableinfo[teacher_sub_control]==2){
           $allClasses= $db->getRows("select class from teacher_subject_mp where teacher_id=$teacher_id and school_id=1 group by class  ");
           if(count($allClasses) > 0){
                $status = 1;
                foreach($allClasses as $class){
                    $classArray[]=$class['class'];
                }
            }

            $allClasses1= $db->getRows("select class,section from teacher_subject_mp where teacher_id=$teacher_id and school_id=1 order by class ");
            if(count($allClasses) > 0){
                $status = 1;
                $temp_arr = array();
                foreach($allClasses1 as $class){
                    $cls_qry = $class['class'];
                    $sec_qry = json_decode($class['section']);

                    if(array_key_exists($cls_qry,$temp_arr)){
                        $old_sec = $temp_arr[$cls_qry];
                        $temp_arr[$cls_qry] = array_values(array_unique(array_merge($old_sec,$sec_qry)));
                    }else{
                        $temp_arr[$cls_qry] = $sec_qry;
                    }

                }

                foreach($temp_arr as $key => $val) {
                    sort($val);
                    array_push($cls_secArray,array("class"=>$key,"sec"=>$val));
                }
            }

            $allSection=$db->getRows("select * from section where school_id=1 ");
            if(count($allSection) > 0){
                $status = 1;
                foreach($allSection as $section){
                    $sectionArray[] = $section['name'];
                }
            }


            $examTerm=$db->getRows("select  type,total from examtype group by type order by id ");
            if(count($examTerm) > 0){
                $status = 1;
                foreach($examTerm as $term){
                    array_push($examArray,["exam_name"=>$term[type],"max_mark"=>$term[total]]);
                }
            }

        }

    }

    $array['class'] = $classArray;
    $array['section'] = $sectionArray;
    $array['class_section'] = $cls_secArray;
    $array['exam_term'] = $examArray;

    echo json_encode(array("status"=>"$status","result_pattern"=>$tableinfo[result_pattern],"subject_ctrl"=>$tableinfo[teacher_sub_control] , "data"=>$array));
?>