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

$session = $_REQUEST['session'];
$myUid = $_REQUEST['my_uid'];
$myUserType = $_REQUEST['my_user_type'];

$status = 0;
$data = array();

$sqlsec = $db->getRows("select idno, admissionno,name,fname,fincome,class,section,mobile,photo from student where session='$session' and  status=1  and issue_TC=0 order by name");

if ($sqlsec && count($sqlsec) > 0){
    $status = 1;

    $emptyJson = new stdClass();
    foreach ($sqlsec as $item) {
        $thread_name = getChatThread($myUid, $myUserType, $item['idno']);
        $item['recent_msg'] = $db->getRow("SELECT * FROM recent_message WHERE thread = '$thread_name' ") ?: $emptyJson;
        $data[] = $item;
    }

}

echo json_encode(array("status" => $status, "data" => $data));

function getChatThread($myId, $myutype, $otherId){
    if($myId < $otherId){
        return $myutype.$myId."_S".$otherId;
    }else{
        return "S".$otherId."_".$myutype.$myId;
    }
}