<?php 
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Credentials: false');
header('Access-Control-Max-Age: 86400'); 
header('Content-type: application/json');

if(isset($_POST["action"])) {
    if(isset($_POST["secretKey"]) && $_POST["secretKey"] === "W3wBGKLLgusUoDUJS5RHPFn8QksrCV7G") {
        switch($_POST["action"]) {
            
            case "addcat":
                include "request/addcat.php";
                execute($_POST["catname"], $_POST["catcolor"]);
                break;
            case "addentry":
                include "request/addEntry.php";
                execute($_POST["prodname"], $_POST["prodamount"], $_POST["produnit"], $_POST["prodcat"], $_POST["prodnote"]);
                break;
            case "getcat":
                include "request/getcat.php";
                execute();
                break;
            case "getentry":
                include "request/getentry.php";
                execute();
                break;
            case "getLast":
                include "request/getLastEntry.php";
                execute($_POST["entryName"]);
                break;
            case "updateEntry":
                include "request/updateEntry.php";
                if(isset($_POST["updateunit"])) {
                    $updateunit = $_POST["updateunit"];
                }else {
                    $updateunit = "";
                }
                execute($_POST["updateaction"], $_POST["updateval"], $updateunit, $_POST["entryid"]);
                break;
            case "delentry":
                include "request/deleteEntry.php";
                execute($_POST["entryid"], $_POST["type"]);
                break;
            default:
                $resp = array('status' => 'err', "err" => "no action specified");
                echo json_encode($resp);
                break;
        }
    }else {
        $resp = array('status' => 'err', "err" => "invalid secret key");
        echo json_encode($resp);
    }
}else {
    $resp = array('status' => 'err', "err" => "no action specified");
    echo json_encode($resp);
}
?>