<?php

/**
 * InfoApp
 * @author Jarcelo
 **/



date_default_timezone_set('UTC');
date_default_timezone_set("America/La_Paz");

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Content-Type, x-requested-with");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE");
header("Allow: POST, GET, OPTIONS, DELETE");

// ini_set('display_errors', 1);
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);

ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
header('Content-Type: application/json');



$func_post = "";
$func_get = "";

if (isset($_POST['function'])) {
    $func_post = $_POST["function"];
}
if (isset($_GET['function'])) {
    $func_get = $_GET["function"];
}





// del_tipo_nivel
if ($func_get == "del_services_space") {
    $id = $_GET['id'];

    if (!isset($id)) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "¡Error: Falta el ID!",
            "alert_type" => "error"
        );
        echo json_encode($array);
        return;
    }


    $sql = "DELETE from service_spaces where id=$id";

    try {
        $result = ExecutorPg::doit($sql);
        $_SESSION['alert'] = '¡Eliminado!';
        $array = array(
            "error"  => "false",
            "data"  => "Result: " . $result,
            "alert" => "¡Eliminado con éxito!",
            "alert_type" => "dashboard"
        );
    } catch (Exception $e) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "¡Error: " . $e->getMessage() . "!",
            "alert_type" => "error"
        );
    }
    echo json_encode($array);
    return;
}



// add_services_space
if ($func_post == "add_services_space") {

    if (!isset($_POST['space_name'])) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "Error: Los datos enviados no son válidos",
            "alert_type" => "error"
        );
        echo json_encode($array);
        return;
    }

    $space_name = $_POST['space_name'];
    $observations = $_POST['observations'];
    $sql = "INSERT into service_spaces (space_name, observations) VALUES ('$space_name', '$observations')";

    try {
        $result = ExecutorPg::doit($sql);
        $_SESSION['alert'] = '¡Creado exitosamente!';
        $array = array(
            "error"  => "false",
            "data"  => "ID: " . $result,
            "alert" => "Registro creado con éxito.",
            "alert_type" => "dashboard"
        );
    } catch (Exception $e) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "¡Error: " . $e->getMessage() . "!",
            "alert_type" => "error"
        );
    }
    echo json_encode($array);
    return;
}



// delet internet_type
if ($func_get == "del_internet_type") {
    $id = $_GET['id'];

    if (!isset($id)) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "¡Error: Falta el ID!",
            "alert_type" => "error"
        );
        echo json_encode($array);
        return;
    }

    $param = InternetTypeData::getById($_GET["id"]);
    try {
        $result = $param->del();
        $_SESSION['alert'] = '¡Eliminado!';
        $array = array(
            "error"  => "false",
            "data"  => "Result: " . $result,
            "alert" => "¡Eliminado con éxito!",
            "alert_type" => "dashboard"
        );
    } catch (Exception $e) {
        $array = array(
            "error"  => "true",
            "data"  => "",
            "alert" => "¡Error: " . $e->getMessage() . "!",
            "alert_type" => "error"
        );
    }
    echo json_encode($array);
    return;
}
