<?php

class Jornada extends Conexion
{
    function GetJornadaById($id)
    {
        $conectar = parent::conectar();

        $sql = "SELECT * FROM jornada WHERE jornadadbk = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $id);

        $sql->execute();

        return $resultado = $sql->fetchAll();
    }

    function GetJornadas()
    {
        $conectar = parent::conectar();

        $sql = "SELECT * FROM jornada";

        $sql = $conectar->prepare($sql);

        $sql->execute();

        return $resultado = $sql->fetchAll();
    }
    
    function CreateJornada($datos)
    {   
        $conectar = parent::conectar();

        $sql = "SELECT * FROM jornada WHERE nombre = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $datos["name"]);

        $sql->execute();

        $resultado = $sql->fetchAll();

        if (count($resultado) > 0) return array("status" => "error", "message" => "Ya existe una jornada con ese nombre");

        $sql = "INSERT INTO jornada (nombre, descripcion) VALUES (?,?)";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $datos["name"]);
        $sql->bindValue(2, $datos["description"]);

        $sql->execute();

        return array("status" => "success", "message" => "Jornada creada correctamente", "id" => $conectar->lastInsertId());
    }

    public function CreateJornadaDetalle($datos) 
    {
        $conectar = parent::conectar();

        foreach ($datos["dia"] as $key => $value) {
            $sql = "INSERT INTO jornada_detalle (jornadadbk, dia, hora_inicio, hora_fin) VALUES (?,?,?,?)";

            $sql = $conectar->prepare($sql);

            $sql->bindValue(1, $datos["jornadadbk"]);
            $sql->bindValue(2, $datos["dia"][$key]);
            $sql->bindValue(3, $datos["hora_inicial"][$key]);
            $sql->bindValue(4, $datos["hora_final"][$key]);

            $sql->execute();
        }

        return array("status" => "success", "message" => "Jornada creada correctamente");
    }

    public function UpdateDetalleJornada($data)
    {
        $conectar = parent::conectar();

        foreach ($data["id_jornada"] as $key => $value) 
        {
            if ($value != "") 
            {
                $sql = "UPDATE jornada_detalle SET dia = ?, hora_inicio = ?, hora_fin = ? WHERE jdetalledbk = ?";

                $sql = $conectar->prepare($sql);

                $sql->bindValue(1, $data["dia"][$key]);
                $sql->bindValue(2, $data["hora_inicial"][$key]);
                $sql->bindValue(3, $data["hora_final"][$key]);
                $sql->bindValue(4, $data["id_jornada"][$key]);

                $sql->execute();
            } 
            else 
            {
                $sql = "INSERT INTO jornada_detalle (jornadadbk, dia, hora_inicio, hora_fin) VALUES (?,?,?,?)";

                $sql = $conectar->prepare($sql);

                $sql->bindValue(1, $data["jornadadbk"]);
                $sql->bindValue(2, $data["dia"][$key]);
                $sql->bindValue(3, $data["hora_inicial"][$key]);
                $sql->bindValue(4, $data["hora_final"][$key]);

                $sql->execute();
            }
        }

        return array("status" => "success", "message" => "Jornada actualizada correctamente");
    }

    public function Get_Jornadas($id)
    {
        $conectar = parent::conectar();

        $sql = "SELECT * FROM jornada_detalle WHERE jornadadbk = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $id);

        $sql->execute();

        return $resultado = $sql->fetchAll();
    }

    function UpdateJornada($datos)
    {
        $conectar = parent::conectar();

        $sql = "UPDATE jornada SET nombre = ?, descripcion = ? WHERE jornadadbk = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $datos["name"]);
        $sql->bindValue(2, $datos["description"]);
        $sql->bindValue(3, $datos["jornadadbk"]);

        $sql->execute();

        if($sql->rowCount() > 0) return array("status" => "success", "message" => "Jornada actualizada correctamente");
        else return array("status" => "error", "message" => "No se pudo actualizar la jornada");
    }

    public function DeleteJornadaDetalle($id)
    {
        $conectar = parent::conectar();

        $sql = "DELETE FROM jornada_detalle WHERE jdetalledbk = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $id);

        $sql->execute();

        if($sql->rowCount() > 0) return array("status" => "success", "message" => "Jornada eliminada correctamente");
        else return array("status" => "error", "message" => "No se pudo eliminar la jornada");
    }


    function DeleteJornada($id)
    {
        $conectar = parent::conectar();

        $sql = "DELETE FROM jornada_detalle WHERE jornadadbk = ?";
        $sql = $conectar->prepare($sql);
        $sql->bindValue(1, $id);

        $sql->execute();

        $sql = "DELETE FROM jornada WHERE jornadadbk = ?";

        $sql = $conectar->prepare($sql);

        $sql->bindValue(1, $id);

        $sql->execute();

        return $resultado = $sql->fetchAll();
    }
}