@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.services.store') }}" method="post">
                @csrf
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Service name<span class="required">*</span></label>
                        <input type="text" class="form-control" name="service_name" placeholder="e.g. WhatsApp" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Service ID (code)<span class="required">*</span></label>
                        <input type="text" class="form-control" name="service_id" placeholder="e.g. wa" required>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Server<span class="required">*</span></label>
                        <select class="form-control" name="server_id" required>
                            <option value="">Select server</option>
                            @foreach($servers as $s)
                            <option value="{{ $s->id }}">{{ $s->server_name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Price<span class="required">*</span></label>
                        <input type="number" step="0.01" class="form-control" name="service_price" placeholder="0.00" required>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Status</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 service</button>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="get" action="{{ route('backend.services.index') }}" class="table-search-form">
                <div class="table-search-form__group">
                    <div class="table-search table-search--narrow">
                        <input type="text" name="search" class="search-input" placeholder="Search services..." 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.services.index') }}" class="btn btn-outline">Clear</a>
                    @endif
                </div>
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Service name</th>
                        <th>Service ID</th>
                        <th>Server</th>
                        <th>Price</th>
                        <th>Status</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($services as $svc)
                    <tr>
                        <td>{{ $services->firstItem() + $loop->index }}</td>
                        <td>{{ $svc->service_name }}</td>
                        <td>{{ $svc->service_id }}</td>
                        <td>{{ $servers->firstWhere('id', $svc->server_id)->server_name ?? $svc->server_id }}</td>
                        <td>{{ $svc->service_price }}</td>
                        <td>{{ $svc->status == '1' ? 'On' : 'Off' }}</td>
                        <td>
                            <div class="table-actions">
                                <button type="button" class="table-btn edit-service" data-id="{{ $svc->id }}" data-service-name="{{ $svc->service_name }}" data-service-id="{{ $svc->service_id }}" data-server-id="{{ $svc->server_id }}" data-service-price="{{ $svc->service_price }}" data-status="{{ $svc->status }}"><i class="las la-pen"></i></button>
                                <form action="{{ route('backend.services.destroy', $svc->id) }}" method="post" class="d-inline" onsubmit="return confirm('Delete this service?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="table-btn"><i class="las la-trash"></i></button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center">No services yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $services->links('vendor.pagination.default') }}
    </div>
</div>

<div class="modal" id="edit-service-modal">
    <div class="modal-content">
        <div class="modal-header">
            <h3 class="modal-title">Edit service</h3>
            <button class="close-modal"><i class="las la-times"></i></button>
        </div>
        <form action="{{ route('backend.services.update', ['id' => '__ID__']) }}" method="post">
            @csrf
            @method('PUT')
            <div class="modal-body">
                <div class="form-group mb-25">
                    <label class="form-label">Service name<span class="required">*</span></label>
                    <input type="text" class="form-control" name="service_name" placeholder="e.g. WhatsApp" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Service ID (code)<span class="required">*</span></label>
                    <input type="text" class="form-control" name="service_id" placeholder="e.g. wa" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Server<span class="required">*</span></label>
                    <select class="form-control" name="server_id" required>
                        <option value="">Select server</option>
                        @foreach($allServers ?? $servers as $s)
                        <option value="{{ $s->id }}">{{ $s->server_name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Price<span class="required">*</span></label>
                    <input type="number" step="0.01" class="form-control" name="service_price" placeholder="0.00" required>
                </div>
                <div class="form-group mb-25">
                    <label class="form-label">Status</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 service</button>
            </div>
        </form>
    </div>
</div>
@endsection

@push('script')
<script>
document.querySelectorAll('.edit-service').forEach(function(btn) {
    btn.addEventListener('click', function() {
        const modal = document.getElementById('edit-service-modal');
        modal.querySelector('input[name="service_name"]').value = this.getAttribute('data-service-name') || '';
        modal.querySelector('input[name="service_id"]').value = this.getAttribute('data-service-id') || '';
        modal.querySelector('select[name="server_id"]').value = this.getAttribute('data-server-id') || '';
        modal.querySelector('input[name="service_price"]').value = this.getAttribute('data-service-price') || '';
        modal.querySelector('select[name="status"]').value = this.getAttribute('data-status') ?? '1';
        modal.querySelector('form').action = '{{ route("backend.services.update", ["id" => "__ID__"]) }}'.replace('__ID__', this.getAttribute('data-id'));
        modal.classList.add('active');
    });
});
</script>
@endpush