<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header("Location: ../auth/login.php");
    exit();
}

require_once __DIR__ . '/../../controllers/BranchController.php';
$branchController = new BranchController();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['create'])) {
        $branchController->createBranch($_POST['name'], $_POST['db_host'], $_POST['db_name'], $_POST['db_user'], $_POST['db_pass']);
    }
    header("Location: branches.php");
    exit();
}

$branches = $branchController->getBranches();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Gestión de Sucursales</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1 class="mt-3">Gestión de Sucursales</h1>
        
        <h2 class="mt-4">Crear Sucursal</h2>
        <form method="POST">
            <input type="hidden" name="create">
            <div class="mb-3">
                <label for="name" class="form-label">Nombre</label>
                <input type="text" class="form-control" id="name" name="name" required>
            </div>
            <div class="mb-3">
                <label for="db_host" class="form-label">Host</label>
                <input type="text" class="form-control" id="db_host" name="db_host" required>
            </div>
            <div class="mb-3">
                <label for="db_name" class="form-label">Nombre de la BD</label>
                <input type="text" class="form-control" id="db_name" name="db_name" required>
            </div>
            <div class="mb-3">
                <label for="db_user" class="form-label">Usuario de la BD</label>
                <input type="text" class="form-control" id="db_user" name="db_user" required>
            </div>
            <div class="mb-3">
                <label for="db_pass" class="form-label">Contraseña de la BD</label>
                <input type="password" class="form-control" id="db_pass" name="db_pass">
            </div>
            <button type="submit" class="btn btn-primary">Crear</button>
        </form>

        <h2 class="mt-4">Sucursales</h2>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre</th>
                    <th>Host</th>
                    <th>Base de Datos</th>
                    <th>Usuario</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($branches as $branch): ?>
                    <tr>
                        <td><?php echo $branch['id']; ?></td>
                        <td><?php echo $branch['name']; ?></td>
                        <td><?php echo $branch['db_host']; ?></td>
                        <td><?php echo $branch['db_name']; ?></td>
                        <td><?php echo $branch['db_user']; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <a href="dashboard.php" class="btn btn-primary">Volver al Dashboard</a>
    </div>
</body>
</html>