<?php

require_once 'contentServer.php';
$content = $_GET['content'];
if (isset($_GET['type'])) {
    $type = $_GET['type'];
} else {
    $type = 'HTML';
}
$server = new ContentServer();
if ($type == 'HTML') {
    die($server->getHTML($content));
} else {
    if ($type == 'JSON') {
        header('Content-Type: application/json');
        if ($content == 'employee-dept') {
            $sql = "SELECT d.dept_no, d.dept_name FROM departments d INNER JOIN dept_emp de ON d.dept_no = de.dept_no \r\n                INNER JOIN employees e on de.emp_no = e.emp_no GROUP BY d.dept_no";
            die($server->getJSON($sql));
        } else {
            if ($content == 'employees') {
                $deptNo = $_GET['dept'];
                $sql = "SELECT e.emp_no, concat(first_name, ' ', last_name ) AS name, title, CONCAT ('\$', max(salary)) AS salary FROM \r\n               employees e INNER JOIN salaries s ON e.emp_no = s.emp_no INNER JOIN titles t \r\n               ON e.emp_no = t.emp_no INNER JOIN dept_emp de ON e.emp_no = de.emp_no WHERE\r\n               de.dept_no = '{$deptNo}' GROUP BY s.emp_no";
                die($server->getJSON($sql));
            } else {
                if ($content == 'departments') {
                    $sql = "SELECT dept_no, dept_name FROM departments";
                    die($server->getSimpleJSON($sql));
                }
            }
        }
    }
 public function processCommand($command)
 {
     if (in_array($command, $this->postCommands)) {
         $this->postData = $this->getPostData();
         if (!is_array($this->postData)) {
             return false;
         }
         if ($command === 'ban.remove' || $command === 'ban.delete') {
             $this->removeBanWithUser($command === 'ban.delete');
             $this->setResponse('bans', $this->getBanList());
             return true;
         }
     }
     if (in_array($command, $this->userCommands)) {
         $isLogin = $command == 'user.login';
         $userObject = $isLogin ? $this->getUser($this->loginUser) : $this->getCurrentUser();
         if (is_null($userObject)) {
             if ($isLogin) {
                 $this->handleLoginFailure();
             }
             throw new Exception($this->getSetting('login-error-message', 'Invalid user or password'));
         }
         $userObject->processCommand($command, $this->postData, $this);
         return true;
     }
     $userRequests = array();
     $document = $this->createUserDocument();
     $outputRequests = array();
     $pathUser = $this->getUserPath();
     if (!file_exists($pathUser)) {
         return false;
     }
     $userData = file_get_contents($pathUser);
     if ($userData === false) {
         return false;
     }
     $userArray = json_decode($userData, true);
     if (!is_array($userArray)) {
         return false;
     }
     if (isset($userArray['requests'])) {
         $userRequests = array_merge($userRequests, $userArray['requests']);
         unset($userArray['requests']);
     }
     $document = array_merge($document, $userArray);
     $postData = $this->postData;
     $next_id = 1;
     $queryType = $this->getSetting('query-type', null);
     foreach ($userRequests as $userRequest) {
         if (!isset($userRequest['id'])) {
             continue;
         }
         $id = $userRequest['id'];
         if ($id < PHP_INT_MAX && $id >= $next_id) {
             $next_id = $id + 1;
         }
         if ($command == 'respond' || $command == 'watch.update') {
             if (isset($postData[$id])) {
                 $responseItem = $postData[$id];
                 $this->completeRequest($userRequest, $responseItem);
             }
         } else {
             if ($command == 'edit') {
                 if (isset($postData[$id])) {
                     $responseItem = $postData[$id];
                     $this->resetRequest($userRequest);
                     if (isset($responseItem['value'])) {
                         $userRequest['item'] = $responseItem['value'];
                     }
                     $outputRequests[] = $userRequest;
                 }
             } else {
                 if ($command == 'delete') {
                     if (in_array($id, $postData)) {
                         continue;
                     }
                 } else {
                     if ($command == 'reset') {
                         if (in_array($id, $postData)) {
                             $this->resetRequest($userRequest);
                         }
                     }
                 }
             }
         }
         if (!($command == 'query' && isset($userRequest['completed'])) || $queryType == 'FULL') {
             $outputRequests[] = $userRequest;
         }
         $document['requests'][] = $userRequest;
     }
     if ($command == 'request') {
         $newRequest = $this->createRequest($next_id);
         $outputRequests[] = $newRequest;
         $document['requests'][] = $newRequest;
         $this->writeDocument($document);
     }
     if ($command == 'query' || $command == 'request') {
         $this->response['requests'] = $outputRequests;
         return true;
     }
     if (in_array($command, $this->requestCommands) || $command == 'watch.update') {
         $this->writeDocument($document);
         if ($command == 'delete' || $command == 'reset' || $command == 'edit') {
             $this->response['requests'] = $outputRequests;
         }
         return true;
     }
     return parent::processCommand($command);
 }