OLIVIA                      HUTANG LAIN
    Kepada Yth.
    {{ str_pad($debts->suppliers->supplier_name ?? 'N/A', 35) }}No. Invoice : {{ $debts->debt_other_number }}
    {{ str_pad($debts->suppliers->address ?? 'N/A', 35) }}Tanggal     : {{ \Carbon\Carbon::parse($debts->document_date)->format('d M Y') }}

    --------------------------------------------------------------------------------------
    |Account No.|         Account Name           |     Amount     |         Memo         |
    --------------------------------------------------------------------------------------
@php
    function splitAtWordBoundary0($text, $maxLength) {
        if (strlen($text) <= $maxLength) return [$text];
        $pos = strrpos(substr($text, 0, $maxLength + 1), ' ');
        if ($pos === false) $pos = $maxLength;
        return [substr($text, 0, $pos), substr($text, $pos + 1)];
    }
@endphp
@foreach($debts->details as $index => $detail)
@php
    $cols = [
        'account_number' => $detail->account_number . ' ',
        'account_name' => $detail->coa->account_name,
        'amount' => number_format($detail->nominal, 0),
        'memo' => $detail->note
    ];
    $maxLengths = [
        'account_number' => 11,
        'account_name' => 32,
        'amount' => 16,
        'memo' => 22
    ];
    $lines = [[]];
    $remaining = array_map(fn($col) => $col, $cols);

    // Split columns into lines
    while (array_filter($remaining)) {
        $currentLine = [];
        foreach ($cols as $key => $value) {
            $max = $maxLengths[$key];
            $text = $remaining[$key] ?? '';
            if (strlen($text) > 0) {
                $split = splitAtWordBoundary0($text, $max);
                $currentLine[$key] = $split[0];
                $remaining[$key] = $split[1] ?? '';
            } else {
                $currentLine[$key] = '';
            }
        }
        $lines[] = $currentLine;
    }
    array_shift($lines); // Remove empty first line
@endphp
@foreach($lines as $line)
    |{{ str_pad($line['account_number'], 11) }}|{{ str_pad($line['account_name'], 32) }}|{{ str_pad($line['amount'], 16, ' ', STR_PAD_LEFT) }}|{{ str_pad($line['memo'], 22, ' ', STR_PAD_LEFT) }}|
@endforeach
@endforeach
    --------------------------------------------------------------------------------------
@php
    // Helper function to split at word boundary <= maxLength
    function splitAtWordBoundary($text, $maxLength) {
        if (strlen($text) <= $maxLength) return [$text];
        $pos = strrpos(substr($text, 0, $maxLength + 1), ' ');
        if ($pos === false) $pos = $maxLength; // No space, cut at maxLength
        return [substr($text, 0, $pos), substr($text, $pos + 1)];
    }

    $totalPayment = 'Total Hutang: '.number_format($debts->total, 0);
    $maxLineLength = 84; // Total line length
    $maxHurufLength = $maxLineLength - strlen($totalPayment); // Max for $totalHuruf per line
    $lines = [];
    $remaining = $totalHuruf;

    // First line: Fit as much of $totalHuruf as possible before Total Payment
    $split = splitAtWordBoundary($remaining, $maxHurufLength);
    $lines[] = $split[0] . str_repeat(' ', $maxHurufLength - strlen($split[0])) .' '. $totalPayment;
    $remaining = $split[1] ?? '';

    // Additional lines: Use same maxHurufLength for consistency
    while (!empty($remaining) && count($lines) < 3) {
        $split = splitAtWordBoundary($remaining, $maxHurufLength);
        $lines[] = $split[0];
        $remaining = $split[1] ?? '';
    }
@endphp
@foreach($lines as $line)
    {{ $line }}
@endforeach

    Memo
    --------------------------------------------------------------------------------------
@php

        $memoLines = [];
        $remainingMemo = $debts->note ?? '';
        $maxMemoLength = 86;

        while (!empty($remainingMemo) && count($memoLines) < 3) {
            $split = splitAtWordBoundary($remainingMemo, $maxMemoLength);
            $memoLines[] = $split[0];
            $remainingMemo = $split[1] ?? '';
        }
@endphp
@foreach($memoLines as $memoLine)
    {{ $memoLine }}
@endforeach


    Disiapkan                   Dibayar oleh                Diterima oleh


    --------                    ------------                -------------
    Tgl.                        Tgl.                        Tgl.
Kembali