@foreach($items as $account)
@php
    $hasChildren = count($account->children_tree) > 0;
    $indent      = $level * 18;
    $typeData    = ['asset'=>['#d1e7dd','#0a3622'],'liability'=>['#f8d7da','#842029'],'equity'=>['#cfe2ff','#084298'],'income'=>['#d1e7dd','#155724'],'expense'=>['#fff3cd','#664d03']];
    [$tbg,$tfg]  = $typeData[$account->type] ?? ['#e9ecef','#495057'];
@endphp
<tr class="tree-row"
    data-id="{{ $account->id }}"
    data-parent="{{ $account->parent_id ?? 'null' }}"
    data-level="{{ $level }}"
    style="{{ $level > 0 ? 'display:none' : '' }}">

    <td style="padding-left:{{ $indent + 12 }}px">
        <div class="d-flex align-items-center gap-1">
            @if($hasChildren)
                <button class="tree-toggle btn btn-link p-0 border-0 lh-1 flex-shrink-0"
                        data-id="{{ $account->id }}"
                        style="width:20px;color:#6c757d">
                    <i class="bi bi-chevron-right toggle-icon" style="font-size:11px"></i>
                </button>
            @else
                <span style="display:inline-block;width:20px"></span>
            @endif

            <i class="bi bi-{{ $account->is_group ? 'folder2 text-warning' : 'file-text text-primary' }} flex-shrink-0"
               style="font-size:14px"></i>

            <div class="ms-1">
                <div class="{{ $account->is_group ? 'fw-semibold' : '' }}" style="font-size:13.5px;line-height:1.3">
                    {{ $account->name }}
                    @if($account->is_bank)
                        <span class="badge ms-1" style="background:#cfe2ff;color:#084298;font-size:10px">B</span>
                    @endif
                    @if($account->is_cash)
                        <span class="badge ms-1" style="background:#d1e7dd;color:#0a3622;font-size:10px">C</span>
                    @endif
                    @if($account->is_system)
                        <span class="badge ms-1 bg-secondary" style="font-size:10px">SYS</span>
                    @endif
                </div>
                {{-- Show code on mobile since code column is hidden --}}
                @if($account->code)
                <div class="d-md-none text-muted" style="font-size:11px">{{ $account->code }}</div>
                @endif
            </div>
        </div>
    </td>

    <td class="d-none d-md-table-cell">
        <code style="font-size:12px;color:#495057">{{ $account->code ?? '—' }}</code>
    </td>

    <td class="d-none d-sm-table-cell">
        <span class="badge" style="background:{{ $tbg }};color:{{ $tfg }};font-size:11px">
            {{ ucfirst($account->type) }}
        </span>
    </td>

    <td class="d-none d-lg-table-cell text-muted" style="font-size:12px">
        {{ $account->sub_type ? ucwords(str_replace('_',' ',$account->sub_type)) : '—' }}
    </td>

    <td>
        @if($account->is_active)
            <span class="badge" style="background:#d1e7dd;color:#0a3622;font-size:11px">Active</span>
        @else
            <span class="badge" style="background:#f8d7da;color:#842029;font-size:11px">Inactive</span>
        @endif
    </td>

    <td class="text-end pe-2">
        <div class="d-flex justify-content-end gap-1 flex-wrap">
            @if(!$account->is_system && auth()->user()->hasPermission('accounts','edit'))
            <a href="{{ route('coa.edit', $account) }}"
               class="btn btn-outline-primary btn-sm" title="Edit">
                <i class="bi bi-pencil"></i>
            </a>
            @endif

            @if(auth()->user()->hasPermission('accounts','create'))
            <a href="{{ route('coa.create', ['parent_id' => $account->id]) }}"
               class="btn btn-outline-success btn-sm" title="Add Sub-Account">
                <i class="bi bi-plus"></i>
            </a>
            @endif

            @if(!$account->is_system && !$hasChildren && auth()->user()->hasPermission('accounts','delete'))
            <form method="POST" action="{{ route('coa.destroy', $account) }}" id="del-{{ $account->id }}">
                @csrf @method('DELETE')
                <button type="button" class="btn btn-outline-danger btn-sm"
                    data-confirm data-form="#del-{{ $account->id }}"
                    data-confirm-title="Delete Account?"
                    data-confirm-text="Delete '{{ $account->name }}'? This cannot be undone."
                    data-confirm-icon="warning" data-confirm-color="#E74C3C" data-confirm-btn="Delete"
                    title="Delete">
                    <i class="bi bi-trash"></i>
                </button>
            </form>
            @endif
        </div>
    </td>
</tr>
@if($hasChildren)
    @include('accounting.coa._tree', ['items' => $account->children_tree, 'level' => $level + 1])
@endif
@endforeach
