@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.otp-servers.store') }}" method="post">
                @csrf
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Server name<span class="required">*</span></label>
                        <input type="text" class="form-control" name="server_name" placeholder="e.g. United States" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Server code<span class="required">*</span></label>
                        <input type="text" class="form-control" name="server_code" placeholder="e.g. 12 or gb" required>
                        <small class="text-muted">Your internal code/name. Does not have to match provider.</small>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Provider server ID<span class="required">*</span></label>
                        <input type="text" class="form-control" name="provider_server_id" placeholder="Exact provider country/server id, e.g. 0 or 12 or gb" required>
                        <small class="text-muted">Must match your API provider's country/server identifier exactly. This is what will be sent as the country parameter.</small>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">API<span class="required">*</span></label>
                        <select class="form-control" name="api_id" required>
                            <option value="">Select API</option>
                            @foreach($apiDetails as $api)
                            <option value="{{ $api->id }}">{{ $api->api_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Status<span class="required">*</span></label>
                        <select class="form-control" name="status">
                            <option value="1">On</option>
                            <option value="0">Off</option>
                        </select>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Add server</button>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="GET" action="{{ route('backend.otp-servers.index') }}" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search by server name or code..." 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.otp-servers.index') }}" class="btn btn-outline">Clear</a>
                @endif
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Server name</th>
                        <th>Server code</th>
                        <th>Provider server ID</th>
                        <th>API</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($servers as $s)
                    <tr>
                        <td>{{ $servers->firstItem() + $loop->index }}</td>
                        <td>{{ $s->server_name }}</td>
                        <td>{{ $s->server_code }}</td>
                        <td>{{ $s->provider_server_id }}</td>
                        <td>{{ $s->apiDetail->api_name ?? '-' }}</td>
                        <td>{{ $s->status == '1' ? 'On' : 'Off' }}</td>
                        <td>
                            <div class="table-actions">
                                <button type="button" class="table-btn edit-otp-server" data-id="{{ $s->id }}" data-server-name="{{ $s->server_name }}" data-server-code="{{ $s->server_code }}" data-provider-server-id="{{ $s->provider_server_id }}" data-api-id="{{ $s->api_id }}" data-status="{{ $s->status }}"><i class="las la-pen"></i></button>
                                <form action="{{ route('backend.otp-servers.destroy', $s->id) }}" method="post" class="d-inline" onsubmit="return confirm('Delete this server?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="table-btn"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="6" class="text-center">No servers yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $servers->links('vendor.pagination.default') }}
    </div>
</div>

<div class="modal" id="edit-otp-server-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Edit server</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="{{ route('backend.otp-servers.update', ['id' => '__ID__']) }}" method="post">
            @csrf
            @method('PUT')
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">Server name<span class="required">*</span></label>
                    <input type="text" class="form-control" name="server_name" placeholder="e.g. United States" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Server code<span class="required">*</span></label>
                    <input type="text" class="form-control" name="server_code" placeholder="e.g. 12 or gb" required>
                    <small class="text-muted">Your internal code/name. Does not have to match provider.</small>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Provider server ID<span class="required">*</span></label>
                    <input type="text" class="form-control" name="provider_server_id" placeholder="Exact provider country/server id, e.g. 0 or 12 or gb" required>
                    <small class="text-muted">Must match your API provider's country/server identifier exactly. This is what will be sent as the country parameter.</small>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">API<span class="required">*</span></label>
                    <select class="form-control" name="api_id" required>
                        <option value="">Select API</option>
                        @foreach($apiDetails as $api)
                        <option value="{{ $api->id }}">{{ $api->api_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Status<span class="required">*</span></label>
                    <select class="form-control" name="status">
                        <option value="1">On</option>
                        <option value="0">Off</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-primary" type="submit">Update server</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('script')
<script>
document.querySelectorAll('.edit-otp-server').forEach(function(btn) {
    btn.addEventListener('click', function() {
        const modal = document.getElementById('edit-otp-server-modal');
        modal.querySelector('input[name="server_name"]').value = this.getAttribute('data-server-name') || '';
        modal.querySelector('input[name="server_code"]').value = this.getAttribute('data-server-code') || '';
        modal.querySelector('input[name="provider_server_id"]').value = this.getAttribute('data-provider-server-id') || '';
        modal.querySelector('select[name="api_id"]').value = this.getAttribute('data-api-id') || '';
        modal.querySelector('select[name="status"]').value = this.getAttribute('data-status') ?? '1';
        modal.querySelector('form').action = '{{ route("backend.otp-servers.update", ["id" => "__ID__"]) }}'.replace('__ID__', this.getAttribute('data-id'));
        modal.classList.add('active');
    });
});
</script>
@endpush