@extends('backend.layout.app')

@section('content')
<div class="container">
    <div class="container-header">
        <h1>{{ $title }}</h1>
    </div>

    <div class="card mb-25">
        <div class="card-body">
            <form action="{{ route('backend.api-details.store') }}" method="post">
                @csrf
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">API name<span class="required">*</span></label>
                        <input type="text" class="form-control" name="api_name" placeholder="e.g. smsbower" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">API URL<span class="required">*</span></label>
                        <input type="url" class="form-control" name="api_url" placeholder="https://..." required>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">API key<span class="required">*</span></label>
                        <input type="text" class="form-control" name="api_key" placeholder="API key" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Provider type<span class="required">*</span></label>
                        <select name="provider_type" class="form-control" required>
                            <option value="handler" selected>PHP handler_api.php</option>
                            <option value="laravel">Laravel /api/v1</option>
                        </select>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Add API</button>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="GET" action="{{ route('backend.api-details.index') }}" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search by name or URL..." value="{{ $search ?? '' }}">
                    <i class="las la-search"></i>
                </div>
                <button type="submit" class="btn btn-secondary search-btn">Search</button>
                @if(!empty($search))
                    <a href="{{ route('backend.api-details.index') }}" class="btn btn-outline">Clear</a>
                @endif
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Balance</th>
                        <th>URL</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($apis as $api)
                    <tr>
                        <td>{{ $apis->firstItem() + $loop->index }}</td>
                        <td>{{ $api->api_name }}</td>
                        <td>{{ $balances[$api->id] ?? '—' }}</td>
                        <td>{{ Str::limit($api->api_url, 40) }}</td>
                        <td>
                            <div class="table-actions">
                                <button
                                    type="button"
                                    class="table-btn edit-api-detail"
                                    data-id="{{ $api->id }}"
                                    data-api-name="{{ $api->api_name }}"
                                    data-api-url="{{ $api->api_url }}"
                                    data-api-key="{{ $api->api_key }}"
                                    data-provider-type="{{ $api->provider_type ?? 'handler' }}"
                                >
                                    <i class="las la-pen"></i>
                                </button>
                                <form action="{{ route('backend.api-details.destroy', $api->id) }}" method="post" class="d-inline" onsubmit="return confirm('Delete this API?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="table-btn"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="5" class="text-center">No API details yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $apis->links('vendor.pagination.default') }}
    </div>
</div>

<div class="modal" id="edit-api-detail-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Edit API</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="{{ route('backend.api-details.update', ['id' => '__ID__']) }}" method="post">
            @csrf
            @method('PUT')
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">API name<span class="required">*</span></label>
                    <input type="text" class="form-control" name="api_name" placeholder="e.g. smsbower" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">API URL<span class="required">*</span></label>
                    <input type="url" class="form-control" name="api_url" placeholder="https://..." required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">API key<span class="required">*</span></label>
                    <input type="text" class="form-control" name="api_key" placeholder="API key" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Provider type<span class="required">*</span></label>
                    <select name="provider_type" class="form-control" required>
                        <option value="handler">PHP handler_api.php</option>
                        <option value="laravel">Laravel /api/v1</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="submit">Update API</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('script')
<script>
document.querySelectorAll('.edit-api-detail').forEach(function(btn) {
    btn.addEventListener('click', function() {
        const modal = document.getElementById('edit-api-detail-modal');
        modal.querySelector('input[name="api_name"]').value = this.getAttribute('data-api-name') || '';
        modal.querySelector('input[name="api_url"]').value = this.getAttribute('data-api-url') || '';
        modal.querySelector('input[name="api_key"]').value = this.getAttribute('data-api-key') || '';
        const typeSelect = modal.querySelector('select[name="provider_type"]');
        if (typeSelect) {
            typeSelect.value = this.getAttribute('data-provider-type') || 'handler';
        }
        modal.querySelector('form').action = '{{ route("backend.api-details.update", ["id" => "__ID__"]) }}'.replace('__ID__', this.getAttribute('data-id'));
        modal.classList.add('active');
    });
});
</script>
@endpush