<?php
session_start();
require_once 'db_connect.php';

header('Content-Type: application/json');

try {
    $last_invoice = $conn->query("SELECT MAX(id) as last_id FROM sales_invoices")->fetch(PDO::FETCH_ASSOC);
    $invoice_number = ($last_invoice['last_id'] ? $last_invoice['last_id'] + 1 : 1);
    echo json_encode(['invoice_number' => $invoice_number]);
} catch (PDOException $e) {
    error_log("Error fetching next invoice number: " . $e->getMessage());
    echo json_encode(['error' => 'فشل جلب رقم الفاتورة التالي']);
}
?>