<?php

require_once("../config/conexion.php");
require_once("../models/Calendar.php");

$calendar = new Calendar();

switch ($_GET["op"]) {

    case "get":
        $eventos = $calendar->GetEvents();

        if (empty($eventos)) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "message" => "No hay eventos"));
        } else {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "message" => "Eventos encontrados", "data" => $eventos));
        }

        break;

    case "create":
        //Atributtes decha_evento, eventodbk
        $datos = array("fecha_evento" => $_POST["fecha_evento"], "eventodbk" => $_POST["eventodbk"]);

        $eventos = $calendar->CreateEvent($datos);

        if (empty($eventos)) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "message" => "Se ha creado el evento"));
        } else {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido crear el evento"));
        }

        break;

    case "update":
        
        $datos = array("fecha_evento" => $_POST["fecha_evento"], "calendardbk" => $_POST["calendardbk"]);

        $eventos = $calendar->UpdateEvent($datos);

        if (empty($eventos)) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "message" => "Se ha actualizado el evento"));
        } else {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido actualizar el evento"));
        }

        break;

    case "delete":

        $eventos = $calendar->DeleteEvent($_POST["calendardbk"]);

        if (empty($eventos)) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "message" => "Se ha eliminado el evento"));
        } else {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido eliminar el evento"));
        }
        
        break;
}
