<?php

declare(strict_types=1);

require_once '../../private/db.php';
require_once '../../private/auth.php';

require_login();

$message='';

if($_SERVER['REQUEST_METHOD']==='POST'){

$stmt=$pdo->prepare("
INSERT INTO customers(
first_name,
last_name,
email,
phone
)
VALUES(?,?,?,?)
");

$stmt->execute([
$_POST['first_name'],
$_POST['last_name'],
$_POST['email'],
$_POST['phone']
]);

$message='Customer added.';
}

$stmt=$pdo->query("SELECT * FROM customers ORDER BY id DESC");
$customers=$stmt->fetchAll();

?>
<!DOCTYPE html>
<html>
<head>
<title>Customers</title>
<style>
body{font-family:Arial;background:#111827;color:#fff;padding:20px;}
table{width:100%;border-collapse:collapse;background:#1f2937;}
th,td{padding:12px;border:1px solid #374151;}
input{padding:10px;width:100%;margin-bottom:10px;}
button{padding:12px;background:#2563eb;border:none;color:#fff;}
.form-box{background:#1f2937;padding:20px;border-radius:10px;margin-bottom:20px;}
</style>
</head>
<body>

<h1>Customers</h1>

<p><a href="dashboard.php" style="color:white;">Dashboard</a></p>

<?php if($message): ?>
<p><?= htmlspecialchars($message) ?></p>
<?php endif; ?>

<div class="form-box">

<form method="POST">

<input type="text" name="first_name" placeholder="First Name" required>

<input type="text" name="last_name" placeholder="Last Name" required>

<input type="email" name="email" placeholder="Email">

<input type="text" name="phone" placeholder="Phone">

<button type="submit">Add Customer</button>

</form>

</div>

<table>

<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
</tr>

<?php foreach($customers as $customer): ?>

<tr>
<td><?= (int)$customer['id'] ?></td>
<td><?= htmlspecialchars($customer['first_name'].' '.$customer['last_name']) ?></td>
<td><?= htmlspecialchars($customer['email']) ?></td>
<td><?= htmlspecialchars($customer['phone']) ?></td>
</tr>

<?php endforeach; ?>

</table>

</body>
</html>
