@extends('layouts.app')

@section('title', 'Chart of Accounts')

@section('content')
    <x-page-head title="Chart of Accounts" subtitle="Master list of all financial accounts">
        <x-slot:actions>
            <x-export-menu module="chart-of-accounts" :formats="['xlsx','csv']" />
            <a href="{{ route('accounting.coa.create') }}" class="btn btn-primary">New Account</a>
        </x-slot:actions>
    </x-page-head>

    @if ($accountsByCategory->isEmpty())
        <x-panel>
            <x-empty-state title="No accounts yet" message="Create your chart of accounts to begin posting journal entries.">
                <x-slot:action>
                    <a href="{{ route('accounting.coa.create') }}" class="btn btn-primary">Create first account</a>
                </x-slot:action>
            </x-empty-state>
        </x-panel>
    @else
        @foreach ($accountsByCategory as $category => $rows)
            <x-panel title="{{ $category }}" style="margin-bottom:16px;">
                <div class="table-wrap">
                    <table>
                        <thead>
                            <tr>
                                <th>Code</th>
                                <th>Name</th>
                                <th>Normal Balance</th>
                                <th class="text-right">Live Balance</th>
                                <th>Status</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($rows as $row)
                                @php($account = $row['account'])
                                <tr>
                                    <td><strong>{{ $account->code }}</strong></td>
                                    <td>{{ $account->name }}</td>
                                    <td>{{ ucfirst($account->normal_balance_side) }}</td>
                                    <td class="text-right">
                                        <a href="{{ route('accounting.gl.index', ['account_id' => $account->id]) }}">
                                            {{ number_format($row['balance'], 2) }}
                                        </a>
                                    </td>
                                    <td>
                                        <x-badge :variant="$account->status->value === 'active' ? 'green' : 'gray'">
                                            {{ $account->status->label() }}
                                        </x-badge>
                                    </td>
                                    <td class="text-right">
                                        <a href="{{ route('accounting.coa.edit', $account) }}" class="btn btn-ghost btn-sm">Edit</a>
                                        <form method="POST" action="{{ route('accounting.coa.destroy', $account) }}" style="display:inline;" onsubmit="return confirm('Delete this account?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                                        </form>
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </x-panel>
        @endforeach
    @endif
@endsection
