@extends('layouts.master')
@section('title', 'Sub-Accounts')

@section('content')
<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-2">
        <div>
            <h5 class="mb-0">Sub-Accounts Level {{ $level }}</h5>
            @if(!empty($breadcrumb))
                <div class="text-muted small">
                    @foreach($breadcrumb as $c)
                        <span>{{ $c['label'] }}</span>@if(!$loop->last) <span class="mx-2">/</span> @endif
                    @endforeach
                </div>
            @endif
        </div>
        <a href="{{ route('ledger.create') }}" class="btn btn-primary mb-1">Add</a>
    </div>

    <div class="card-header bg-primary text-white py-3">
        <h5 class="mb-0"><i class="fas fa-list me-2"></i>Sub-Accounts</h5>
    </div>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    <table class="table table-bordered table-hover">
        <thead class="table-light">
            <tr>
                <th>#</th>
                <th>Account Code</th>
                <th>Sub Name</th>
                <th>Children</th>
                <th width="180px">Actions</th>
            </tr>
        </thead>
        <tbody>
            @forelse($children as $row)
                <tr>
                    <td>{{ ($children->currentPage() - 1) * $children->perPage() + $loop->iteration }}</td>
                    <td>{{ $row->account_code }}</td>
                    <td>{{ $row->sub_name }}</td>
                    <td>
                        @if($level < 5)
                            <a class="btn btn-secondary btn-sm"
                               href="{{ route('chart-of-accounts.sub-accounts.level.index', ['level' => $level + 1, 'parentId' => $row->id]) }}">
                                View
                            </a>
                        @else
                            <span class="text-muted">—</span>
                        @endif
                    </td>
                    <td>
                        <a href="{{ route('ledger.create') }}" class="btn btn-secondary btn-sm">Edit</a>
                        <form action="{{ route('chart-of-accounts.sub-accounts.level.destroy', ['level' => $level, 'parentId' => $parentId, 'childId' => $row->id]) }}"
                              method="POST" style="display:inline;">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')">Delete</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr><td colspan="5">No records found.</td></tr>
            @endforelse
        </tbody>
    </table>

    {{ $children->links() }}
</div>
@endsection

