<?php

class Sede extends Conexion {

    public function Crear($datos)
    {
        try
        {
            $conectar = parent::conectar();
            parent::set_names();

            $sql = "INSERT INTO sedes (nombre, ubicacion) VALUES (?, ?);";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $datos["nombre"]);
            $sql->bindValue(2, $datos["ubicacion"]);

            $sql->execute();

            return array("status" => "success", "message" => "Sede creada correctamente");
        }
        catch(Exception $e)
        {
            return array("status" => "error", "message" => $e->getMessage());
        }
    }

    public function Update($data)
    {
        try
        {
            $conectar = parent::conectar();
            parent::set_names();

            $sql = "UPDATE sedes SET nombre = ?, ubicacion = ? WHERE sededbk = ?;";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $data["nombre"]);
            $sql->bindValue(2, $data["ubicacion"]);
            $sql->bindValue(3, $data["id"]);

            $sql->execute();

            return array("status" => "success", "message" => "Sede actualizada correctamente");
        }
        catch(Exception $e)
        {
            return array("status" => "error", "message" => $e->getMessage());
        }
    }

    public function Delete($id)
    {   
        try
        {
            $conectar = parent::conectar();
            parent::set_names();

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

            $sql->execute();

            return array("status" => "success", "message" => "Sede eliminada correctamente");
        }
        catch(Exception $e)
        {
            return array("status" => "error", "message" => $e->getMessage());
        }
    }

    public function ShowById ($id)
    {
        try
        {
            $conectar = parent::conectar();
            parent::set_names();

            $sql = "SELECT * FROM sedes WHERE sededbk = ?;";
            $sql = $conectar->prepare($sql);
            $sql->bindValue(1, $id);
            $sql->execute();

            return $sql->fetchAll(PDO::FETCH_ASSOC);
        }
        catch(Exception $e)
        {
            return array("status" => "error", "message" => $e->getMessage());
        }
    }

    public function Listar() 
    {
        try
        {
            $conectar = parent::conectar();
            parent::set_names();

            $sql = "SELECT * FROM sedes;";
            $sql = $conectar->prepare($sql);
            $sql->execute();

            return $sql->fetchAll(PDO::FETCH_ASSOC);
        }
        catch(Exception $e)
        {
            return array("status" => "error", "message" => $e->getMessage());
        }
    }
}