<?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 id, login_id,name,mobile,photo from admin where session='$session' ");

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

    $emptyJson = new stdClass();
    foreach ($sqlsec as $item) {
        $thread_name = getChatThread($myUid, $myUserType, $item['id']);
        $item['photo'] = "media/photo/" . $item['photo'];
        $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."_A".$otherId;
    }else{
        return "A".$otherId."_".$myutype.$myId;
    }
}