<?php

function route($route, $args)
{
    loadAccountData();
    $nbParts = count($route);

    if (!$nbParts)
        require(__DIR__."/../templates/index.php");
    else if ($nbParts == 2 && $route[0] == 'event' && $route[1] == "list" && isset($_GET["period"])) {
        $period = Period::FromHashed($_GET["period"]);
        if (!$period) {
            header("404 Not Found");
            exit;
        }
        echo '[';
        $written = false;
        $events = Event::LoadForPeriod($period);
        foreach ($events as $i) {
            echo ($written ? ',' : '') .json_encode($i->toArray());
            $written = true;
        }
        echo ']';
        exit;
    } else if ($nbParts == 2 && $route[0] == 'event' && $route[1] == "add") {
        if (isset($_POST["label"])) {
            // create
            $ctx = ',' .implode(',', array_unique($_POST["context"])) .',';
            $event = new \Event(array(
                "id"           => null,
                "label"        => $_POST["label"],
                "payment_date" => $_POST["paymentDate"] .' 00:00:00',
                "context"      => $ctx,
                "beneficiary"  => $_POST["beneficiary"],
                "amount"       => floatval($_POST["amount"])
            ));
            $event->save();
            header("Location: ../");
            exit;
        }
        $template = null;
        if (isset($_GET["from"]))
            $template = Event::FromHashed($_GET["from"]);
        require(__DIR__."/../templates/eventAdd.php");
    } else if ($nbParts == 2 && $route[0] == 'event' && $route[1] == "edit") {
        $template = Event::FromHashed($_GET["id"]);
        if (isset($_POST["label"])) {
            // update
            $ctx = ',' .implode(',', array_unique($_POST["context"])) .',';
            $event = new \Event(array(
                "id"           => $template->getDbId(),
                "label"        => $_POST["label"],
                "payment_date" => $_POST["paymentDate"] .' 00:00:00',
                "context"      => $ctx,
                "beneficiary"  => $_POST["beneficiary"],
                "amount"       => floatval($_POST["amount"])
            ));
            $event->save();
            header("Location: ../");
            exit;
        }
        if (isset($_GET["from"]))
            $template = Event::FromHashed($_GET["from"]);
        require(__DIR__."/../templates/eventAdd.php");
    } else if ($nbParts == 2 && $route[0] == 'event' && $route[1] == "delete") {
        $event = Event::FromHashed($_GET["id"]);
        if (!$event) {
            require(__DIR__."/../templates/403.php");
        } else if (isset($_POST["confirm"]) && $_POST["confirm"] == $_GET["id"]) {
            $event->delete();
            header("Location: ../");
            exit;
        } else {
            require(__DIR__."/../templates/eventDel.php");
        }
    } else if ($route[0] == "periods") {
        if ($nbParts == 1) {
            require(__DIR__."/../templates/periods.php");
            exit;
        }
    } else if ($nbParts == 1 && $route[0] == 'logout') {
        session_destroy();
        header("Location: ./");
        exit;
    } else {
        require(__DIR__."/../templates/403.php");
    }
}

