<?php

class Friends extends CodonModule   {
    
    function index()    {
        $this->show('Friends/index.php');
    }
    
    function AcceptRequest() {
    	
    	//Actions 
    	$action = $this->post->action;
    	$accept = $this->post->accept;
    	
    	$friend_id = $this->post->friendid;
    	$my_id = $this->post->selfid;
    	$id = $this->post->id;
    	$status = ICSFriends::checkFriends($friend_id, $my_id);
		/*Checks if I am viewing my own account */
		$self = ICSFriends::check_self_acc($friend_id, $my_id);
		
		$request_data = ICSFriends::getRequest($id);
		$friend = PilotData::getPilotData($request_data->friend_id);
    	
    	if($action == 'reject') {
    		
    		ICSFriends::RejectRequest($friend_id, $my_id);
    		$this->set('message', 'Friend request rejected!');
    		$this->show('core_success.php');
    		$this->index();
    		
    	} 
    	else if($action == 'accept') {
    		
	    		if($self == 0) {
    				$this->set('message', 'Did you just try to accept your own request?');
    				$this->show('core_error.php');
    			} else if($request_data->req_status == 1) {
    				$this->set('message', 'You are already Friends');
    				$this->show('core_error.php');
    			} else {
    				ICSFriends::AcceptRequest($friend_id, $my_id);
    				$friend_data = PilotData::getPilotData($friend_id);
    				$my_data = PilotData::getPilotData($my_id);
    				$subject = 'Hey there, We are friends now!';
    				$newmessage = 'I have accepted your friend request. Hope to fly with you soon!';
    				$name = $my_data->firstname.' and '.$friend_data->firstname.' are friends now';
                    $posted_by = 'Membership Bot';
                    $privacy = '0';
                    ICSActs::new_ics_activity($name, $posted_by, $privacy);
    				ICSFriends::send_new_mail($friend_id, $my_id, $subject, $newmessage);
    				$this->set('message', 'Friend request accepted. Now '.$friend->firstname.' will be able to see your Logbook, Send you Group flight invites and more.');
    				$this->show('core_success.php');
    				$this->index();
    			}
    	}
    	
    }
}