<?php
/**
 * Created by PhpStorm.
 * User: jaskokoyn
 * Date: 4/26/2015
 * Time: 8:09 PM
 */

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

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

if(!isset($_SESSION['loggedin'])){
    dj($output);
}

if( !isReady($params, array( "id" ) ) ){
    dj($output);
}

$fid                        =   intval($params['id']);

$friendCheckQuery           =   $db->prepare("SELECT * FROM friends WHERE uid = :uid AND fid = :fid");
$friendCheckQuery->execute(array(
    ":uid"                  =>  $_SESSION['uid'],
    ":fid"                  =>  $fid
));

if($friendCheckQuery->rowCount() === 0){
    $friendQuery            =   $db->prepare("INSERT INTO friends(uid,fid) VALUES( :uid, :fid )");
}else{
    $friendQuery            =   $db->prepare("DELETE FROM friends WHERE uid = :uid AND fid = :fid");
}

$friendQuery->execute(array(
    ":uid"                  =>  $_SESSION['uid'],
    ":fid"                  =>  $fid
));
dj($output);