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

require_once __DIR__ . '/../../controllers/SupplierController.php';
$supplierController = new SupplierController();

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['create'])) {
        $supplierController->createSupplier($_POST['name'], $_POST['contact_person'], $_POST['phone'], $_POST['email']);
    } elseif (isset($_POST['update'])) {
        $supplierController->updateSupplier($_POST['id'], $_POST['name'], $_POST['contact_person'], $_POST['phone'], $_POST['email']);
    } elseif (isset($_POST['delete'])) {
        $supplierController->deleteSupplier($_POST['id']);
    }
    header("Location: suppliers.php");
    exit();
}

$suppliers = $supplierController->getSuppliers();
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Gestión de Proveedores</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 Proveedores</h1>
        
        <h2 class="mt-4">Crear Proveedor</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="contact_person" class="form-label">Persona de Contacto</label>
                <input type="text" class="form-control" id="contact_person" name="contact_person">
            </div>
            <div class="mb-3">
                <label for="phone" class="form-label">Teléfono</label>
                <input type="text" class="form-control" id="phone" name="phone">
            </div>
            <div class="mb-3">
                <label for="email" class="form-label">Email</label>
                <input type="email" class="form-control" id="email" name="email">
            </div>
            <button type="submit" class="btn btn-primary">Crear</button>
        </form>

        <h2 class="mt-4">Proveedores</h2>
        <table class="table">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Nombre</th>
                    <th>Contacto</th>
                    <th>Teléfono</th>
                    <th>Email</th>
                    <th>Acciones</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($suppliers as $supplier): ?>
                    <tr>
                        <td><?php echo $supplier['id']; ?></td>
                        <td><?php echo $supplier['name']; ?></td>
                        <td><?php echo $supplier['contact_person']; ?></td>
                        <td><?php echo $supplier['phone']; ?></td>
                        <td><?php echo $supplier['email']; ?></td>
                        <td>
                            <form method="POST" class="d-inline">
                                <input type="hidden" name="delete">
                                <input type="hidden" name="id" value="<?php echo $supplier['id']; ?>">
                                <button type="submit" class="btn btn-danger btn-sm">Eliminar</button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <a href="dashboard.php" class="btn btn-primary">Volver al Dashboard</a>
    </div>
</body>
</html>