<x-admin.layout title="Licenses">
    <div class="grid two">
        <div class="card">
            <h2>Create License</h2>
            <form method="post" action="{{ route('admin.licenses.store') }}">
                @csrf
                <label>Product</label><select name="product_id" required>@foreach ($products as $product)<option value="{{ $product->id }}">{{ $product->name }}</option>@endforeach</select>
                <label>Client</label><select name="client_account_id" required>@foreach ($clients as $client)<option value="{{ $client->id }}">{{ $client->name }}</option>@endforeach</select>
                <label>Domains</label><input name="domains" placeholder="example.com">
                <label>Seats</label><input name="seats" type="number" min="1" value="1" required>
                <label>Expires At</label><input name="expires_at" type="datetime-local">
                <label>Status</label><select name="status"><option>active</option><option>inactive</option><option>suspended</option><option>expired</option></select>
                <button type="submit">Create</button>
            </form>
        </div>
        <div class="card">
            <h2>Licenses</h2>
            <div class="table-wrap">
                <table><thead><tr><th>Key</th><th>Product</th><th>Client</th><th>Rules</th><th>Actions</th></tr></thead><tbody>
                    @foreach ($licenses as $license)
                        <tr>
                            <td>
                                <code id="license-{{ $license->id }}">{{ $license->key }}</code>
                                <button class="button secondary copy-button" type="button" data-copy-target="license-{{ $license->id }}">Copy</button>
                            </td>
                            <td>{{ $license->product?->name }}</td>
                            <td>{{ $license->clientAccount?->name }}</td>
                            <td colspan="2">
                                <form method="post" action="{{ route('admin.licenses.update', $license) }}" class="admin-inline-form">
                                    @csrf
                                    @method('PATCH')
                                    <input name="domains" value="{{ implode(', ', $license->domains ?? []) }}" placeholder="domains">
                                    <input name="seats" type="number" min="1" value="{{ $license->seats }}" required>
                                    <input name="expires_at" type="datetime-local" value="{{ $license->expires_at?->format('Y-m-d\TH:i') }}">
                                    <select name="status">
                                        @foreach (['active', 'inactive', 'suspended', 'expired'] as $status)
                                            <option value="{{ $status }}" @selected($license->status === $status)>{{ $status }}</option>
                                        @endforeach
                                    </select>
                                    <button type="submit">Save</button>
                                </form>
                                <form method="post" action="{{ route('admin.licenses.destroy', $license) }}" onsubmit="return confirm('Delete license?')">
                                    @csrf
                                    @method('DELETE')
                                    <button class="button danger" type="submit">Delete</button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody></table>
            </div>
            {{ $licenses->links() }}
        </div>
    </div>
</x-admin.layout>
