<?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';
require_once '../include/Functions.php';


$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true
    ]
]);




$app->post('/addTicket_single_luggage', function (Request $request, Response $response) {

    if(isTheseParametersAvailable(array('ticketfk', 'companyfk', 'routefk', 'stationfk', 'posid', 'custname', 'custnumber', 'cashiername', 'barcode', 'transactiondate'))){
    	$requestData = $request->getParsedBody();
         $db = new Functions();
        $ticketfk = $requestData['ticketfk'];
        $companyfk = $requestData['companyfk'];
        $routefk = $requestData['routefk'];
        $stationfk = $requestData['stationfk'];
        $posid = $requestData['posid'];
        $custname = $requestData['custname'];
        $cashiername = $requestData['cashiername'];
        $barcode = $requestData['barcode'];
        $transactiondate = $requestData['transactiondate'];
        $result = $db->addTicketSingleLuggage($ticketfk, $companyfk, $routefk, $stationfk, $posid, $custname, $custnumber, $cashiername, $barcode, $transactiondate);
        
    }else{
            $result['error'] = true;
            $result['message'] = 'Error occurred, some parameters are empty.';
    }

    $response->getBody()->write((json_encode($result)));
});



$app->get('/fetch_all_company_buses', function (Request $request, Response $response) {

        $requestData = $request->getParsedBody();
        $db = new Functions();
        $result = $db->fetchAllBuses();

    $response->getBody()->write((json_encode($result)));
});




$app->get('/fetch_all_routes', function (Request $request, Response $response) {

        $requestData = $request->getParsedBody();
        $db = new Functions();
        $result = $db->fetchAllRoutes();
    $response->getBody()->write((json_encode($result)));
});


$app->get('/fetch_all_users', function (Request $request, Response $response) {

        $requestData = $request->getParsedBody();
        $db = new Functions();
        $result = $db->fetchAllUsers();

    $response->getBody()->write((json_encode($result)));
});


$app->get('/fetch_all_stations', function (Request $request, Response $response) {

        $requestData = $request->getParsedBody();
        $db = new Functions();
        $result = $db->fetchAllStations();

    $response->getBody()->write((json_encode($result)));
});



$app->post('/upload_ticket_transactions', function (Request $request, Response $response) {
    if(isTheseParametersAvailable(array('ticketsTransactions'))){
        $requestData = $request->getParsedBody();
        $db = new Functions();
        $tickets = $requestData['ticketsTransactions'];
        $ticketsarray = json_decode($tickets, TRUE);
        $result = $db->insertTicketTransactions($ticketsarray);
    }else{
            $result['error'] = true;
            $result['message'] = 'Error occurred, some parameters are empty.';
    }

   $response->getBody()->write((json_encode($result)));
});


$app->post('/upload_parcel_transactions', function (Request $request, Response $response) {
    if(isTheseParametersAvailable(array('parcelTransactions'))){
        $requestData = $request->getParsedBody();
        $db = new Functions();
        $tickets = $requestData['parcelTransactions'];
        $ticketsarray = json_decode($tickets, TRUE);
        $result = $db->insertParcelTransactions($ticketsarray);
    }else{
            $result['error'] = true;
            $result['message'] = 'Error occurred, some parameters are empty.';
    }

   $response->getBody()->write((json_encode($result)));
});



$app->post('/login', function (Request $request, Response $response) {
    if(isTheseParametersAvailable(array('username','password'))){
    	$requestData = $request->getParsedBody();
         $db = new Functions();
        $username = $requestData['username'];
        $password = $requestData['password'];
        $result = $db->login($username, $password);
        
    }else{
            $result['error'] = true;
            $result['message'] = 'Error occurred, some parameters are empty.';
    }

   $response->getBody()->write((json_encode($result)));
});




$app->get('/fetch_sales_statistics', function (Request $request, Response $response) {

        $requestData = $request->getParsedBody();
        $db = new Functions();
        $result = $db->fetchSalesStatistics();
    $response->getBody()->write((json_encode($result)));
});



function isTheseParametersAvailable($required_fields)
{
    $error = false;
    $error_fields = "";
    $request_params = $_REQUEST;

    foreach ($required_fields as $field) {
        if (!isset($request_params[$field]) || strlen(trim($request_params[$field])) <= 0) {
            $error = true;
            $error_fields .= $field . ', ';
        }
    }

    if ($error) {
        $response = array();
        $response["error"] = true;
        $response["message"] = 'Required field(s) ' . substr($error_fields, 0, -2) . ' is missing or empty';
        echo json_encode($response);
        return false;
    }
    return true;
}


$app->run();