<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 2/18/2015
 * Time: 11:45 PM
 */

require('../../inc/db.php');
include('../../inc/functions.php');

$params                     =   getParams();
$output                     =   array('status' => 1);

if( !isReady( $params, array("username") ) ){
    dj($output);
}
if( !isset( $_SESSION['loggedin'] ) ){
    dj($output);
}
// Get Chat Settings
$username                   =   secure($params['username']);

// Get Chat Messages
$messagesQuery          =   $db->prepare("SELECT * FROM messages
                                          WHERE m_type = :mt AND
                                          (
                                          ( submitted_by = :s1 AND recipient = :r1 ) OR
                                          ( submitted_by = :s2 AND recipient = :r2 )
                                          )
                                          ORDER BY `id` DESC LIMIT 25");
$messagesQuery->execute(array(
    ":mt"   =>  2,
    ":s1"   =>  $_SESSION['username'],
    ":r1"   =>  $username,
    ":s2"   =>  $username,
    ":r2"   =>  $_SESSION['username']
));
$messagesRow            =   $messagesQuery->fetchAll(PDO::FETCH_ASSOC);

$output['status']       =   2;
$output['messages']     =   array_reverse($messagesRow);
$output['mid']          =   end($output['messages']);
dj($output);