<?php

function getRoomLists($room_id = null){
	global $db;
	$html = "";
	$db->bind('user_id', $_SESSION['user_id']);
	$orderLists = $db->query("SELECT order_id, count(order_id) as berapa FROM notice_rooms join notice_members on notice_rooms.room_id = notice_members.room_id where notice_members.user_id = :user_id and notice_rooms.room_type = 'order' and notice_rooms.order_id is not null GROUP BY notice_rooms.order_id");
	foreach($orderLists as $key => $value){
		$db->bind('order_id', $value['order_id']);
		$roomLists = $db->query("SELECT * FROM `notice_members` join notice_rooms on notice_members.room_id = notice_rooms.room_id WHERE notice_members.user_id = '".$_SESSION['user_id']."' and notice_rooms.room_type = 'order' and (notice_rooms.po_id <> 0 or notice_rooms.order_id <> 0) and notice_members.status = 1 and notice_rooms.order_id = :order_id");
			if($value['berapa'] > 1){
				$html .= "<li>".getSOData($value['order_id'], "order_no")."<ul>";							
			}
			foreach($roomLists as $key => $value2){
				$html .= "<li " . ($room_id == $value2['room_id'] ? "data-jstree='".'{ "selected" : true }'."'" : ">" )
							."<a href='/messenger/order/".$value2['room_id']."'>".($value2['order_id'] <> null ? getSOData($value2['order_id'], "order_no") : getPOData($value2['po_id'], "order_no")) . " " . $value2['chat_name'] . " <span class='badge badge-danger'>".getRoomUnread($value2['room_id'])."</span></a>"
						."</li>";
			}
			if($value['berapa'] > 1){
				$html .= "</ul></li>";							
			}	
		}
	return $html;

}

function getRoomData($room_id, $kolom){
	global $db;
	$db->bind('room_id', $room_id);
	$result = $db->query("SELECT * FROM `notice_rooms` WHERE room_id = :room_id");
	
	 if ($result) {
        return $result[0][$kolom];
    } else {
        return "0";
    }
}

function getinRoomRoles($room_id){
	global $db;
	$db->bind('room_id', $room_id);
	$db->bind('user_id', $_SESSION['user_id']);
	$data = $db->query("SELECT * FROM `notice_members` WHERE room_id = :room_id and user_id = :user_id");
	
	if ($data) {
		return $data[0]["roles"];
	} else {
        return "1";
    }
	
}
function getImporterinRoom($room_id){
	global $db;
	$db->bind('room_id', $room_id);
	$data = $db->query("SELECT * FROM `notice_members` join users on notice_members.user_id = users.user_id WHERE notice_members.room_id = :room_id and users.group_id = 2");
	
	if(count($data) > 0){
		return $data;
	}else{
		return "";
	}
	
}
function getForwarderinRoom($room_id){
	global $db;
	$db->bind('room_id', $room_id);
	$data = $db->query("SELECT * FROM `notice_members` join users on notice_members.user_id = users.user_id WHERE notice_members.room_id = :room_id and users.group_id = 1");
	
	
	if(count($data) > 0){
		return $data;
	}else{
		return "";
	}
	
}

function roomIDExist($room_id) {
    global $db;
    $db->bind("room_id", $room_id);
    $row = $db->query("SELECT * FROM notice_rooms WHERE room_id = :room_id");
    return (count($row) > 0 ? true : false);
}

function getUserInRoom($room_id){
    global $db;
    $db->bind("room_id", $room_id);
    $data = $db->query("SELECT * FROM notice_members WHERE room_id = :room_id");
	
	return $data;
	
}

function userInRoom($room_id, $user_id) {
    global $db;
    $db->bind("room_id", $room_id);
    $db->bind("user_id", $user_id);
    $row = $db->query("SELECT * FROM notice_members WHERE room_id = :room_id and user_id = :user_id and status = 1");
    return (count($row) > 0 ? true : false);
}

function getMessageData($message_id, $data){
    global $db;
    $db->bind("message_id", $message_id);
	$message = $db->query("SELECT * FROM `notice_messages` WHERE message_id = :message_id");
	
	return (count($message) > 0 ? $message[0][$data] : "");
	
}
function getRoomIDFromSO($so_id){
    global $db;
    $db->bind("so_id", $so_id);
	$row = $db->query("SELECT * FROM notice_rooms WHERE order_id = :so_id");
	
	return (count($row) > 0 ? $row[0]["room_id"] : "");
}
function getRoomIDFromPO($po_id){
    global $db;
    $db->bind("po_id", $po_id);
	$row = $db->query("SELECT * FROM notice_rooms WHERE po_id = :po_id");
	
	return (count($row) > 0 ? $row[0]["room_id"] : "");
}