<?php

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

$event = new Event();

switch($_GET["op"])
{
    case "insertarActualizar":
        $datos = array( "tabla" => "events",
                        "event_title" => $_POST["event_title"],
                        "event_start" => $_POST["event_start"],
                        "event_end" => $_POST["event_end"],
                        "event_color" => $_POST["event_color"]
                        );
                        
        break;

    case "create":
        $datos = array("title" => $_POST["title"], "color" => $_POST["color"]);

        $eventos = $event->Create($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 "eventos":
        
        $datos = $event->GetEvents();
        $output = array();
        
        foreach ($datos as $row) {

            $sub_array = array();

            $sub_array["eventodbk"] = $row["eventodbk"];
            $sub_array["color_evento"] = $row["color_evento"];
            $sub_array["titulo_evento"] = $row["titulo_evento"];

            $output[] = $sub_array;
        }

        header('Content-Type: application/json');
        echo json_encode($output);
        
        break;

    case "delete":

        $eventos = $event->Delete($_POST["id"]);

        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;

    case "update":

        $datos = array("title" => $_POST["title"], "id" => $_POST["id"]);

        $eventos = $event->Update($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;
}