prepare('INSERT INTO customers (name, phone, phone_normalized, email, address) VALUES (?, ?, ?, ?, ?)'); $stmt->execute([$_POST['name'], $phoneForStorage, $phoneForStorage, $_POST['email'] ?? '', $_POST['address'] ?? '']); set_flash('success', tr('تمت إضافة العميل بنجاح', 'Customer added successfully')); } catch (Throwable $e) { if (is_customer_phone_unique_violation($e)) { set_flash('danger', tr('رقم الهاتف هذا مسجل لعميل آخر بالفعل.', 'This phone number is already assigned to another customer.')); } else { throw $e; } } redirect_to('customers.php'); } elseif ($action === 'edit') { $phoneInput = trim((string) ($_POST['phone'] ?? '')); $phone = $phoneInput === '' ? '' : normalize_oman_phone($phoneInput); $phoneForStorage = $phone === '' ? null : $phone; if ($phoneInput !== '' && $phone === '') { set_flash('danger', tr('رقم الهاتف يجب أن يكون عمانياً من 8 خانات.', 'Phone must be an 8-digit Oman number.')); redirect_to('customers.php'); } $customerId = (int) ($_POST['id'] ?? 0); if ($phone !== '' && customer_phone_exists($phone, $customerId)) { set_flash('danger', tr('رقم الهاتف هذا مسجل لعميل آخر بالفعل.', 'This phone number is already assigned to another customer.')); redirect_to('customers.php'); } try { $stmt = $pdo->prepare('UPDATE customers SET name = ?, phone = ?, phone_normalized = ?, email = ?, address = ? WHERE id = ?'); $stmt->execute([$_POST['name'], $phoneForStorage, $phoneForStorage, $_POST['email'] ?? '', $_POST['address'] ?? '', $_POST['id']]); set_flash('success', tr('تم التحديث بنجاح', 'Updated successfully')); } catch (Throwable $e) { if (is_customer_phone_unique_violation($e)) { set_flash('danger', tr('رقم الهاتف هذا مسجل لعميل آخر بالفعل.', 'This phone number is already assigned to another customer.')); } else { throw $e; } } redirect_to('customers.php'); } elseif ($action === 'delete') { $stmt = $pdo->prepare('DELETE FROM customers WHERE id = ?'); $stmt->execute([$_POST['id']]); set_flash('success', tr('تم الحذف بنجاح', 'Deleted successfully')); redirect_to('customers.php'); } } // Pagination & Search $page = max(1, (int)($_GET['p'] ?? 1)); $limit = 10; $offset = ($page - 1) * $limit; $search = $_GET['q'] ?? ''; $where = '1=1'; $params = []; if ($search) { $where .= " AND (name LIKE ? OR phone LIKE ? OR CONCAT('968', phone) LIKE ? OR email LIKE ?)"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; $params[] = "%$search%"; } $totalStmt = $pdo->prepare("SELECT COUNT(*) FROM customers WHERE $where"); $totalStmt->execute($params); $total = $totalStmt->fetchColumn(); $totalPages = ceil($total / $limit); $queryStmt = $pdo->prepare("SELECT * FROM customers WHERE $where ORDER BY id DESC LIMIT $limit OFFSET $offset"); $queryStmt->execute($params); $items = $queryStmt->fetchAll(); require __DIR__ . '/includes/header.php'; ?>

ID
1): ?>