<?php
require_once "../config/db.php";

// Only POST allowed
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    die("Invalid request");
}

// Get data
$name    = trim($_POST['name'] ?? '');
$email   = trim($_POST['email'] ?? '');
$phone   = trim($_POST['phone'] ?? '');
$address = trim($_POST['address'] ?? '');

// Validation
if (empty($name) || empty($email) || empty($phone) || empty($address)) {
    die("All fields are required");
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    die("Invalid email format");
}

// Insert
$stmt = $conn->prepare("
    INSERT INTO customers (name, email, phone, address) 
    VALUES (?, ?, ?, ?)
");

if (!$stmt) {
    die("Prepare failed: " . $conn->error);
}

$stmt->bind_param("ssss", $name, $email, $phone, $address);

if (!$stmt->execute()) {
    die("Insert failed: " . $stmt->error);
	
	header("Location: ../pages/add_customer.php?success=1");
exit;

}

header("Location: ../pages/add_customer.php?success=1");
exit;
