<?php
include("../../db.php");
header('Content-Type: application/json');

/*
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
*/

$class = $_GET['class'];
$session = $_GET['session'];
$section = $_GET['section'];
//$myUid = $_GET['uid'];

$checkcontrol = $db->getVal("select teacher_chat_control from tbl_info");

$emptyJson = new stdClass();

if ($checkcontrol == 0) {

    $result = $db->getRows("select * from staff where session='$session' ");

    if ($result) {
        foreach ($result as $rowc) {

            $thread_name = getChatThread($class,  $rowc['idno']);
            $rec = $db->getRow("SELECT * FROM recent_message WHERE thread = '$thread_name' ");

            if ($rec != ''){

                $sec_demo = json_decode($rec['section']);
                if ($sec_demo == null || $sec_demo == ''){
                    $sec_demo = array();
                }

                $rec['section'] = $sec_demo;
                if (empty($rec['section']) || in_array($section, $rec['section'])){

                }else{
                    $rec = $emptyJson;
                }
            }else{
                $rec = $emptyJson;
            }

            $jsonc[] = array("idno" => $rowc['idno'], "name" => $rowc['name'], "fname" => $rowc['fname'], "subject" => $rowc['experience'], "dob" => $rowc['dob'],
                "qualification" => $rowc['qualification'], "mobile" => $rowc['mobile'], "address" => $rowc['address'],
                "department" => $rowc['department'], "photo" => $rowc['photo'], "recent_msg" => $rec);


        }

        $recx = $db->getRow("SELECT * FROM recent_message WHERE thread = 'allclass' ");

        if ($recx == null || $recx == ''){
            $recx = $emptyJson;
        }else{
            $recx['author_name'] = $db->getVal("SELECT name FROM staff WHERE idno = '$recx[author]' ");
        }

        $jsonc[] = array("idno" => 0, "name" => "All Class", "fname" => "", "subject" => "", "dob" => "", "qualification" => "",
                         "mobile" => "", "address" => "", "department" => "", "photo" => "", "recent_msg" => $recx);

        $json['status'] = 1;
        $json['msg'] = "Profile found Successfully..!";
        $json['data'] = $jsonc;

        echo json_encode($json);
    } else {
        echo json_encode(array('status' => 0, 'msg' => 'detail not found..!'));
    }

}

if ($checkcontrol == 1) {

    $result_term = $db->getRows("select distinct(s.idno), s.idno,s.department,s.name,s.experience,s.photo,e.class,e.teacher_id,e.session from staff as s inner join assign_teacher as e on s.idno=e.teacher_id where e.class='$class' and section='$section' and e.session='$session' ");
    // while ($rowc = mysql_fetch_assoc($result_term))
    foreach ($result_term as $rowc) {

        $thread_name = getChatThread($class, $rowc['idno']);
        $rec = $db->getRow("SELECT * FROM recent_message WHERE thread = '$thread_name' ") ;

        if ($rec != ''){
            $sec_con = json_decode($rec['section']);

            if ($sec_con == null || $sec_con == ''){
                $sec_con = array();
            }

            $rec['section'] = $sec_con;
        }else{
            $rec = $emptyJson;
        }

        $jsond[] = array("idno" => $rowc['idno'], "name" => "$rowc[name]", "fname" => "$rowc[fname]", "subject" => "$rowc[experience]", "dob" => "$rowc[dob]",
            "qualification" => "$rowc[qualification]", "mobile" => "$rowc[mobile]", "address" => "$rowc[address]",
            "department" => "$rowc[department]", "photo" => "$rowc[photo]", "recent_msg" => $rec);

    }

    $recx = $db->getRow("SELECT * FROM recent_message WHERE thread = 'allclass' ");

    if ($recx == null || $recx == ''){
        $recx = $emptyJson;
    }else{
        $recx['author_name'] = $db->getVal("SELECT name FROM staff WHERE idno = '$recx[author]' ");
    }

    $jsond[] = array("idno" => 0, "name" => "All Class", "fname" => "", "subject" => "", "dob" => "", "qualification" => "",
        "mobile" => "", "address" => "", "department" => "", "photo" => "", "recent_msg" => $recx);

    if ($jsond == null) {
        $jsond = array();
    }

    $json['status'] = 1;
    $json['msg'] = "successful";
    $json['data'] = $jsond;

    if (is_array($json) && count($json) > 0) {
        echo json_encode($json);

    } else {
        echo json_encode(array('status' => 0, 'msg' => 'data not found..!'));
    }

}


function getChatThread($cls, $tid){
    return $cls."/T".$tid;
}