false, 'error' => tr('الاسم مطلوب', 'Name is required')]); exit; } if ($phoneInput !== '' && $phone === '') { echo json_encode(['success' => false, 'error' => tr('رقم الهاتف يجب أن يكون عمانياً من 8 خانات.', 'Phone must be an 8-digit Oman number.')]); exit; } if ($phone !== '' && customer_phone_exists($phone)) { echo json_encode(['success' => false, 'error' => tr('رقم الهاتف هذا مسجل لعميل آخر بالفعل.', 'This phone number is already assigned to another customer.')]); exit; } try { $pdo = db(); $stmt = $pdo->prepare('INSERT INTO customers (name, phone, phone_normalized) VALUES (?, ?, ?)'); $stmt->execute([$name, $phoneForStorage, $phoneForStorage]); $id = $pdo->lastInsertId(); echo json_encode(['success' => true, 'customer' => ['id' => $id, 'name' => $name, 'phone' => $phone]]); } catch (Throwable $e) { if (is_customer_phone_unique_violation($e)) { echo json_encode(['success' => false, 'error' => tr('رقم الهاتف هذا مسجل لعميل آخر بالفعل.', 'This phone number is already assigned to another customer.')]); } else { echo json_encode(['success' => false, 'error' => tr('تعذر حفظ العميل حالياً.', 'Could not save the customer right now.')]); } } exit; }