<?php
session_start();
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit;
}
require_once 'db/config.php';
$db = db();

// Fetch banner text for the form
$stmt_banner = $db->prepare('SELECT valor FROM configuracion WHERE clave = ?');
$stmt_banner->execute(['banner_text']);
$banner_text = $stmt_banner->fetchColumn();

// Fetch kanban columns
$kanban_columns = $db->query("SELECT id, nombre FROM kanban_columns ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);

include 'layout_header.php';
?>

<div class="container mt-5">
    <?php if (isset($_SESSION['success_message'])): ?>
        <div class="alert alert-success">
            <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
        </div>
    <?php endif; ?>
    <?php if (isset($_SESSION['error_message'])): ?>
        <div class="alert alert-danger">
            <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
        </div>
    <?php endif; ?>

    <h1 class="mb-4">Configuración General</h1>

    <!-- Banner Text Management -->
    <div class="card mb-5">
        <div class="card-header">
            <h2>Editar Banner del Kanban</h2>
        </div>
        <div class="card-body">
            <form action="save_banner_text.php" method="post">
                <div class="form-group">
                    <label for="banner_text">Texto del banner</label>
                    <textarea name="banner_text" id="banner_text" class="form-control" rows="2"><?php echo htmlspecialchars($banner_text ?? ''); ?></textarea>
                    <small class="form-text text-muted">Este texto aparecerá en un banner deslizante en la parte superior del tablero Kanban.</small>
                </div>
                <button type="submit" class="btn btn-primary">Guardar Banner</button>
            </form>
        </div>
    </div>

    <!-- Cobertura Banner Management -->
    <div class="card mb-5">
        <div class="card-header">
            <h2>Editar Imagen de Cobertura</h2>
        </div>
        <div class="card-body">
            <div class="text-center mb-3">
                <p>Imagen actual:</p>
                <?php
                $banner_image = 'assets/uploads/cobertura_banner.jpg';
                if (file_exists($banner_image)):
                ?>
                    <img src="<?php echo $banner_image; ?>?t=<?php echo time(); ?>" alt="Banner de Cobertura" class="img-fluid rounded" style="max-height: 200px;">
                <?php else: ?>
                    <p>No hay ninguna imagen de cobertura cargada.</p>
                <?php endif; ?>
            </div>
            <form action="save_cobertura_banner.php" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="banner_image">Subir o reemplazar imagen</label>
                    <input type="file" class="form-control-file" id="banner_image" name="banner_image" accept="image/jpeg,image/png,image/gif,image/webp" required>
                    <small class="form-text text-muted">La imagen se guardará como .jpg y se mostrará en la sección de Cobertura.</small>
                </div>
                <button type="submit" class="btn btn-primary">Guardar Imagen</button>
            </form>
        </div>
    </div>

    <!-- Cobertura Xpress Banner Management -->
    <div class="card mb-5">
        <div class="card-header">
            <h2>Editar Imagen de Cobertura Xpress</h2>
        </div>
        <div class="card-body">
            <div class="text-center mb-3">
                <p>Imagen actual:</p>
                <?php
                $banner_image_xpress = 'assets/uploads/cobertura_xpress_banner.jpg';
                if (file_exists($banner_image_xpress)):
                ?>
                    <img src="<?php echo $banner_image_xpress; ?>?t=<?php echo time(); ?>" alt="Banner de Cobertura Xpress" class="img-fluid rounded" style="max-height: 200px;">
                <?php else: ?>
                    <p>No hay ninguna imagen de Cobertura Xpress cargada.</p>
                <?php endif; ?>
            </div>
            <form action="save_cobertura_xpress_banner.php" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="banner_image_xpress">Subir o reemplazar imagen</label>
                    <input type="file" class="form-control-file" id="banner_image_xpress" name="banner_image_xpress" accept="image/jpeg,image/png,image/gif,image/webp" required>
                    <small class="form-text text-muted">Esta imagen se mostrará en la sección de Cobertura Xpress.</small>
                </div>
                <button type="submit" class="btn btn-primary">Guardar Imagen</button>
            </form>
        </div>
    </div>

    <!-- Column Management -->
    <div class="card">
        <div class="card-header">
            <h2>Configuración de Columnas Kanban</h2>
        </div>
        <div class="card-body">
            <table class="table table-bordered">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Nombre de la Columna</th>
                        <th>Acción</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($kanban_columns as $column): ?>
                        <tr>
                            <form action="edit_column.php" method="post">
                                <td><?php echo htmlspecialchars($column['id']); ?></td>
                                <td>
                                    <input type="text" name="nombre" class="form-control" value="<?php echo htmlspecialchars($column['nombre']); ?>">
                                    <input type="hidden" name="id" value="<?php echo $column['id']; ?>">
                                </td>
                                <td>
                                    <button type="submit" class="btn btn-primary btn-sm">Guardar</button>
                                    <a href="delete_column.php?id=<?php echo $column['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('¿Estás seguro?')">Eliminar</a>
                                </td>
                            </form>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <form action="add_column.php" method="post" class="form-inline mt-3">
                <div class="form-group">
                    <input type="text" name="nombre" class="form-control" placeholder="Nueva Columna" required>
                </div>
                <button type="submit" class="btn btn-success ml-2">Agregar Columna</button>
            </form>
        </div>
    </div>
</div>

<?php include 'layout_footer.php'; ?>
