<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/database.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/auth.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/erp/config/functions.php';
header('Content-Type: application/json');
requireLogin();
requireRole('director','accountant','manager');

$pdo  = getDBConnection();
$user = currentUser();

if (!verifyCSRF($_POST['csrf_token'] ?? '')) {
    echo json_encode(['ok'=>false,'msg'=>'CSRF invalid']); exit;
}

$customerId  = (int)($_POST['customer_id']  ?? 0);
$invoiceDate = trim($_POST['invoice_date']  ?? date('Y-m-d'));
$dueDate     = trim($_POST['due_date']      ?? '') ?: null;
$vatRate     = (float)($_POST['vat_rate']   ?? 0);
$note        = trim($_POST['note']          ?? '') ?: null;
$deliveryId  = (int)($_POST['delivery_id']  ?? 0) ?: null;
$items       = $_POST['items'] ?? [];

if (!$customerId || empty($items)) {
    echo json_encode(['ok'=>false,'msg'=>'Thiếu khách hàng hoặc sản phẩm']); exit;
}

$validItems = [];
foreach ($items as $it) {
    $pcId  = (int)($it['product_code_id'] ?? 0);
    $qty   = (float)($it['quantity']      ?? 0);
    $price = (float)($it['unit_price']    ?? 0);
    if ($pcId && $qty > 0) {
        $validItems[] = [
            'product_code_id' => $pcId,
            'description'     => trim($it['description'] ?? ''),
            'unit'            => trim($it['unit'] ?? ''),
            'quantity'        => $qty,
            'unit_price'      => $price,
            'total_price'     => round($qty * $price),
        ];
    }
}
if (empty($validItems)) {
    echo json_encode(['ok'=>false,'msg'=>'Không có dòng hợp lệ']); exit;
}

try {
    $pdo->beginTransaction();

    // Sinh số HĐ INV-YYYYMMDD-XXX
    $pdo->prepare("
        INSERT INTO document_sequences (doc_type, doc_date, last_seq) VALUES ('INV',?,1)
        ON DUPLICATE KEY UPDATE last_seq = last_seq + 1
    ")->execute([$invoiceDate]);
    $seq = $pdo->query("
        SELECT last_seq FROM document_sequences WHERE doc_type='INV' AND doc_date='$invoiceDate'
    ")->fetchColumn();
    $invoiceNo = 'INV-' . date('Ymd', strtotime($invoiceDate)) . '-' . str_pad($seq, 3, '0', STR_PAD_LEFT);

    $subtotal    = array_sum(array_column($validItems, 'total_price'));
    $vatAmount   = round($subtotal * $vatRate / 100);
    $totalAmount = $subtotal + $vatAmount;

    // Insert invoice header
    $pdo->prepare("
        INSERT INTO invoices
            (invoice_no, invoice_date, due_date, customer_id,
             subtotal, vat_rate, vat_amount, total_amount,
             delivery_id, note, status, created_by)
        VALUES (?,?,?,?,?,?,?,?,?,?,'unpaid',?)
    ")->execute([
        $invoiceNo, $invoiceDate, $dueDate, $customerId,
        $subtotal, $vatRate, $vatAmount, $totalAmount,
        $deliveryId, $note, $user['id']
    ]);
    $invoiceId = $pdo->lastInsertId();

    // Insert items
    $stmtItem = $pdo->prepare("
        INSERT INTO invoice_items
            (invoice_id, product_code_id, description, unit, quantity, unit_price, total_price)
        VALUES (?,?,?,?,?,?,?)
    ");
    foreach ($validItems as $it) {
        $stmtItem->execute([
            $invoiceId, $it['product_code_id'], $it['description'],
            $it['unit'], $it['quantity'], $it['unit_price'], $it['total_price']
        ]);
    }

    // Cập nhật status biên bản → invoiced
    if ($deliveryId) {
        $pdo->prepare("UPDATE deliveries SET status='invoiced' WHERE id=?")->execute([$deliveryId]);
    }

    $pdo->commit();
    echo json_encode(['ok'=>true,'msg'=>'Đã tạo hoá đơn','invoice_no'=>$invoiceNo,'id'=>$invoiceId]);

} catch (Throwable $e) {
    $pdo->rollBack();
    error_log($e->getMessage());
    echo json_encode(['ok'=>false,'msg'=>'Lỗi hệ thống: '.$e->getMessage()]);
}