<?php
require_once __DIR__ . '/../config/database.php';

class SupplierController {

    private $conn;

    public function __construct() {
        $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if ($this->conn->connect_error) {
            die("Error de conexión: " . $this->conn->connect_error);
        }
    }

    public function getSuppliers() {
        $result = $this->conn->query("SELECT * FROM suppliers");
        $suppliers = [];
        while ($row = $result->fetch_assoc()) {
            $suppliers[] = $row;
        }
        return $suppliers;
    }

    public function createSupplier($name, $contact_person, $phone, $email) {
        $stmt = $this->conn->prepare("INSERT INTO suppliers (name, contact_person, phone, email) VALUES (?, ?, ?, ?)");
        $stmt->bind_param("ssss", $name, $contact_person, $phone, $email);
        return $stmt->execute();
    }

    public function updateSupplier($id, $name, $contact_person, $phone, $email) {
        $stmt = $this->conn->prepare("UPDATE suppliers SET name = ?, contact_person = ?, phone = ?, email = ? WHERE id = ?");
        $stmt->bind_param("ssssi", $name, $contact_person, $phone, $email, $id);
        return $stmt->execute();
    }

    public function deleteSupplier($id) {
        $stmt = $this->conn->prepare("DELETE FROM suppliers WHERE id = ?");
        $stmt->bind_param("i", $id);
        return $stmt->execute();
    }
}
?>