<?php
require_once __DIR__ . '/../../app/includes/auth.php';
require_auth(); require_role_access('customer:create'); verify_csrf();
$name = trim((string)post('customer_name'));
if ($name === '') { flash('error', 'Customer name is required.'); redirect('/customers/create.php'); }
$email = trim((string)post('email'));
$phone = trim((string)post('phone'));
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) { flash('error', 'Please enter a valid customer email.'); redirect('/customers/create.php'); }
if (strlen($phone) > 60) { flash('error', 'Phone number is too long.'); redirect('/customers/create.php'); }
$stmt = db()->prepare('INSERT INTO customers (customer_name, phone, email, address, location, customer_type, created_by, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)');
$stmt->execute([$name, $phone, $email, post('address'), post('location'), post('customer_type'), current_user()['id'], now(), now()]);
log_activity('customer.create', 'Created customer ' . $name);
flash('success', 'Customer created successfully.');
redirect('/customers/index.php');
