<?php

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

$jornada = new Jornada();

function verifyIsEmptyArray($array)
{
    $result = false;
    foreach ($array as $value) {
        if (empty($value)) {
            $result = true;
            break;
        }
    }
    return $result;
}

switch ($_GET["op"]) {

    case "select":
        $jornadas = $jornada->GetJornadas();

        $data = array();

        foreach ($jornadas as $row) {
            $data[] = array("id" => $row["jornadadbk"], "text" => $row["nombre"] . " - " . $row["descripcion"]);
        }

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

    case "getById":
        $jornadas = $jornada->GetJornadaById($_POST["id"]);

        if (empty($jornadas)) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha encontrado la jornada"));
        } else {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "data" => array(
                "jornadadbk" => $jornadas[0]["jornadadbk"],
                "nombre" => $jornadas[0]["nombre"],
                "descripcion" => $jornadas[0]["descripcion"]
            )));
        }
        break;

    case "get":
        $jornadas = $jornada->GetJornadas();

        $data = array();

        foreach ($jornadas as $row) {
            $sub_array = array();
            $sub_array["nombre"] = $row["nombre"];
            $sub_array["descripcion"] = $row["descripcion"];
            $sub_array["actions"] = '<button type="button" onClick="editar('.$row["jornadadbk"].');"  id="'.$row["jornadadbk"].'" class="btn btn-primary btn-sm update"><i class="fas fa-edit"></i></button> <button type="button" onClick="eliminar('.$row["jornadadbk"].');"  id="'.$row["jornadadbk"].'" class="btn btn-danger btn-sm delete"><i class="fas fa-trash-alt"></i></button>';
            $data[] = $sub_array;
        }

        $results = array(
            "sEcho" => 1,
            "iTotalRecords" => count($data),
            "iTotalDisplayRecords" => count($data),
            "data" => $data);

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

    case "getJornadas":
        if(empty($_GET["id"]))
        {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha encontrado la jornada", "id" => $_GET["id"] ));
        }
        else
        {
            $jornadas = $jornada->Get_Jornadas($_GET["id"]);

            $data = array();

            foreach ($jornadas as $row) {
                $data[] = array(
                    "jdetalledbk" => $row["jdetalledbk"],
                    "jornadadbk" => $row["jornadadbk"],
                    "dia" => $row["dia"],
                    "hora_inicio" => $row["hora_inicio"],
                    "hora_fin" => $row["hora_fin"],
                );
            }

            header('Content-Type: application/json');
            echo json_encode(array("status" => "success", "data" => $data));
        }
        break;

    case "create":
        if(empty($_POST["jornadadbk"]))
        {   
            $datos = array("name" => $_POST["name"], "description" => $_POST["description"]);

            $jornadas = $jornada->CreateJornada($datos);
            
            header('Content-Type: application/json');
            echo json_encode($jornadas);
        }
        else
        {
            $datos = array("jornadadbk" => $_POST["jornadadbk"], "dia" => $_POST["dia"], "hora_inicio" => $_POST["hora_inicial"], "hora_fin" => $_POST["hora_final"], "descripcion" => $_POST["description"]);

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

    case "deleteJornada":
        $jornadas = $jornada->DeleteJornadaDetalle($_GET["id"]);

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

    case "updateJornadas":
        if(verifyIsEmptyArray($_POST["dia"]) || verifyIsEmptyArray($_POST["hora_inicial"]) || verifyIsEmptyArray($_POST["hora_final"]))
        {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido configurar la jornada, se requieren todos los campos."));
        }
        else
        {
            $data = array(
                "jornadadbk" => $_POST["jornadadbk"],
                "dia" => $_POST["dia"],
                "hora_inicial" => $_POST["hora_inicial"],
                "hora_final" => $_POST["hora_final"],
                "id_jornada" => $_POST["id_jornada"]
            );

            $response = $jornada->UpdateDetalleJornada($data);

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

    case "configure":
        if (verifyIsEmptyArray($_POST["dia"]) || verifyIsEmptyArray($_POST["hora_inicial"]) || verifyIsEmptyArray($_POST["hora_final"])) {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido configurar la jornada, se requieren todos los campos."));
        }   
        else
        {
            $data = array(
                "jornadadbk" => $_POST["jornadadbk"],
                "dia" => $_POST["dia"],
                "hora_inicial" => $_POST["hora_inicial"],
                "hora_final" => $_POST["hora_final"]
            );

            $response = $jornada->CreateJornadaDetalle($data);

            header('Content-Type: application/json');
            echo json_encode($response);
        }
        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 "updateJornada":
        if(empty($_POST["jornadadbk"]) || empty($_POST["name"]) || empty($_POST["description"]))
        {
            header('Content-Type: application/json');
            echo json_encode(array("status" => "error", "message" => "No se ha podido actualizar la jornada, se requieren todos los campos."));
        }
        else
        {
            
            $datos = array("jornadadbk" => $_POST["jornadadbk"], "name" => $_POST["name"], "description" => $_POST["description"]);

            $jornadas = $jornada->UpdateJornada($datos);

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

    case "delete":
        $jornadas = $jornada->DeleteJornada($_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;
}
