<x-app-layout>
    <div class="portal-container">
        @if (session('status'))
            <div class="alert alert-success">{{ session('status') }}</div>
        @endif

        @php
            $displayMoney = function ($domain) {
                $amount = number_format((float) $domain->amount, 2);
                return strtoupper((string) $domain->currency) === 'USD'
                    ? '$'.$amount.' USD'
                    : strtoupper((string) $domain->currency).' '.$amount;
            };
        @endphp

        <div class="page-head compact-page-head">
            <div>
                <span class="eyebrow">Domains</span>
                <h1 class="page-title">My Domains</h1>
                <p class="page-subtitle">View your registered and transferred domains.</p>
            </div>
            <form method="POST" action="{{ route('client.domains.refresh') }}">
                @csrf
                <button class="secondary-btn" type="submit">Refresh</button>
            </form>
        </div>

        <div class="card" style="overflow-x:auto;">
            <table class="client-clean-table">
                <thead>
                    <tr>
                        <th>Domain</th>
                        <th>Registration Period</th>
                        <th>Amount</th>
                        <th>Expiry Date</th>
                        <th>Status</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($domains as $domain)
                        @php
                            $status = strtolower(str_replace(' ', '_', (string) $domain->status));
                            $statusLabel = ucwords(str_replace('_', ' ', $status ?: 'pending'));
                        @endphp
                        <tr>
                            <td><a class="domain-table-link" href="{{ route('client.domains.show', $domain) }}"><strong>{{ $domain->domain_name }}</strong></a></td>
                            <td>{{ $domain->registration_period ? $domain->registration_period.' year(s)' : '-' }}</td>
                            <td><strong>{{ $displayMoney($domain) }}</strong></td>
                            <td>{{ optional($domain->expiry_date)->format('l, F jS, Y') ?: '-' }}</td>
                            <td><span class="status-pill {{ $status }}">{{ $statusLabel }}</span></td>
                            <td><a class="primary-btn domain-manage-btn" href="{{ route('client.domains.show', $domain) }}">Manage</a></td>
                        </tr>
                    @empty
                        <tr><td colspan="6">No domains yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
            <div style="margin-top:18px;">{{ $domains->links() }}</div>
        </div>
    </div>
</x-app-layout>
