<x-layouts.dashboard :title="'Client Profile: '.$client->name">
    <div class="profile-grid">
        <section class="card">
            <span class="badge">{{ $client->status }}</span><span class="badge">{{ $client->client_type === 'one_time' ? 'One-Time Order Client' : 'Monthly Plan Client' }}</span><h2>{{ $client->business_name ?: $client->name }}</h2>
            <p>{{ $client->email }} | {{ $client->phone }}</p>
            <p class="muted">Assigned designer: {{ $client->assignedDesigner->name ?? 'Not assigned' }}</p>
            <p>{{ $client->notes }}</p>
        </section>
        <section class="card">
            @if($client->client_type === 'one_time')
                <h3>Service Order Client</h3><p class="price">{{ $requests->count() }} orders</p>
                <p>Monthly plan is hidden for this client. Admin can still assign tasks and manage payments manually.</p>
            @else
                <h3>Selected Plan</h3><p class="price">{{ $subscription->plan->name ?? 'No plan' }}</p>
                <p>{{ optional($subscription?->starts_at)->format('Y-m-d') }} - {{ optional($subscription?->ends_at)->format('Y-m-d') }}</p>
                <p>Total monthly work: {{ $subscription->plan->work_limit ?? 0 }} | Completed: {{ $client->clientTasks->whereIn('status',['approved','completed','delivered'])->count() }} | Remaining: {{ $client->clientTasks->whereNotIn('status',['approved','completed','delivered'])->count() }}</p>
            @endif
        </section>
    </div>
    @if($client->client_type !== 'one_time')
    <section class="form-card">
        <header class="table-head">
            <div>
                <h3>Assign / Change Plan</h3>
                <p>Create a new active subscription for this client. The previous active plan is moved to inactive automatically.</p>
            </div>
        </header>
        <form method="post" action="{{ route('admin.client-subscriptions.store') }}">
            @csrf
            <input type="hidden" name="user_id" value="{{ $client->id }}">
            <input type="hidden" name="status" value="active">
            <input type="hidden" name="usage_count" value="0">
            <div class="form-grid">
                <div class="field">
                    <label>Plan</label>
                    <select name="plan_id" required>
                        <option value="">Select plan</option>
                        @foreach($plans as $plan)
                            <option value="{{ $plan->id }}">{{ $plan->name }} - {{ $plan->price > 0 ? number_format($plan->price) : 'Custom' }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="field"><label>Start Date</label><input type="date" name="starts_at" value="{{ today()->toDateString() }}"></div>
                <div class="field"><label>End Date</label><input type="date" name="ends_at" value="{{ today()->addDays(30)->toDateString() }}"></div>
            </div>
            <p><button class="btn">Activate Plan</button></p>
        </form>
    </section>
    @endif
    <section class="table-card">
        <header class="table-head"><div><h3>Subscription History</h3><p>Plan records for this client.</p></div><a class="ghost small" href="{{ route('admin.client-subscriptions.index') }}">Manage All</a></header>
        <table>
            <thead><tr><th>Plan</th><th>Start</th><th>End</th><th>Usage</th><th>Status</th></tr></thead>
            <tbody>@forelse($subscriptionHistory as $item)<tr><td>{{ $item->plan->name ?? '-' }}</td><td>{{ $item->starts_at?->format('Y-m-d') ?? '-' }}</td><td>{{ $item->ends_at?->format('Y-m-d') ?? '-' }}</td><td>{{ $item->usage_count }}</td><td><span class="badge status-{{ str($item->status)->slug() }}">{{ str($item->status)->headline() }}</span></td></tr>@empty<tr><td colspan="5">No subscription history.</td></tr>@endforelse</tbody>
        </table>
    </section>
    <div class="grid two">
        <div class="table-card"><h3>Requests</h3><table><tbody>@forelse($requests as $item)<tr><td>{{ $item->request_type }}</td><td>{{ $item->title }}</td><td><span class="badge">{{ $item->status }}</span></td></tr>@empty<tr><td>No requests.</td></tr>@endforelse</tbody></table></div>
        <div class="table-card"><h3>Payments & Files</h3><table><tbody>@forelse($payments as $item)<tr><td>{{ $item->plan->name ?? '-' }}</td><td>{{ number_format($item->amount) }}</td><td><span class="badge">{{ $item->status }}</span></td></tr>@empty<tr><td>No payments.</td></tr>@endforelse</tbody></table></div>
    </div>
</x-layouts.dashboard>
