@extends('layouts.app')

@section('title', __('clinic.patients'))
@section('heading', __('clinic.patients'))

@section('content')
    <div class="card mb-1">
        <form method="GET" action="{{ route('patients.index') }}" class="form-grid" style="--grid-cols: 5; gap: 0.75rem; align-items: flex-end;">
            <div class="form-group mb-0">
                <label for="q">{{ __('clinic.search') }}</label>
                <div class="search-input-wrap" style="position: relative;">
                    <input id="q" class="form-control" name="q" value="{{ request('q') }}" placeholder="{{ __('clinic.search_placeholder') }}">
                </div>
            </div>

            <div class="form-group mb-0">
                <label for="gender">{{ __('clinic.filter_by_gender') }}</label>
                <select id="gender" class="form-control" name="gender">
                    <option value="">{{ __('clinic.view_all') }}</option>
                    <option value="male" {{ request('gender') === 'male' ? 'selected' : '' }}>{{ __('clinic.male') }}</option>
                    <option value="female" {{ request('gender') === 'female' ? 'selected' : '' }}>{{ __('clinic.female') }}</option>
                    <option value="other" {{ request('gender') === 'other' ? 'selected' : '' }}>{{ __('clinic.other') }}</option>
                </select>
            </div>

            <div class="form-group mb-0">
                <label for="status">{{ __('clinic.filter_by_status') }}</label>
                <select id="status" class="form-control" name="status">
                    <option value="">{{ __('clinic.view_all') }}</option>
                    <option value="active" {{ request('status') === 'active' ? 'selected' : '' }}>{{ __('clinic.active') }}</option>
                    <option value="archived" {{ request('status') === 'archived' ? 'selected' : '' }}>{{ __('clinic.archived') }}</option>
                </select>
            </div>

            <div class="form-group mb-0">
                <label for="doctor_id">{{ __('clinic.filter_by_doctor') }}</label>
                <select id="doctor_id" class="form-control" name="doctor_id">
                    <option value="">{{ __('clinic.view_all') }}</option>
                    @foreach ($doctors as $doc)
                        <option value="{{ $doc->id }}" {{ request('doctor_id') == $doc->id ? 'selected' : '' }}>{{ $doc->name }}</option>
                    @endforeach
                </select>
            </div>

            <div class="flex gap">
                <button type="submit" class="btn btn-primary btn-sm" style="flex: 1;">{{ __('clinic.filter') }}</button>
                <a href="{{ route('patients.index') }}" class="btn btn-ghost btn-sm" style="display: flex; align-items: center; justify-content: center;">✕</a>
            </div>
        </form>
    </div>

    <div class="flex-between mb-1" style="display: flex; justify-content: space-between; align-items: center;">
        <div>
            @if(request('q') || request('gender') || request('status') || request('doctor_id'))
                <span class="pill pill-muted">{{ __('clinic.search') }}</span>
            @endif
        </div>
        <a href="{{ route('patients.create') }}" class="btn btn-primary btn-sm">{{ __('clinic.add_new_patient') }}</a>
    </div>

    <div class="table-wrap">
        <table class="data-table">
            <thead>
                <tr>
                    <th>{{ __('clinic.patient_code') }}</th>
                    <th>{{ __('clinic.full_name') }}</th>
                    <th>{{ __('clinic.phone') }}</th>
                    <th>{{ __('clinic.gender') }}</th>
                    <th>{{ __('clinic.age') }}</th>
                    <th>{{ __('clinic.status') }}</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @forelse ($patients as $p)
                    <tr>
                        <td><strong><a href="{{ route('patients.show', $p) }}">{{ $p->patient_code }}</a></strong></td>
                        <td><a href="{{ route('patients.show', $p) }}">{{ $p->full_name }}</a></td>
                        <td>{{ $p->phone }}</td>
                        <td>{{ __('clinic.' . $p->gender) }}</td>
                        <td>{{ $p->age ?? '—' }}</td>
                        <td>
                            <span class="pill {{ $p->status === 'active' ? 'pill-success' : 'pill-muted' }}">
                                {{ __('clinic.' . $p->status) }}
                            </span>
                        </td>
                        <td>
                            <div class="flex gap">
                                <a href="{{ route('patients.show', $p) }}" class="btn btn-ghost btn-sm">{{ __('clinic.view_all') }}</a>
                                <a href="{{ route('patients.edit', $p) }}" class="btn btn-ghost btn-sm">⚙</a>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="7" style="text-align: center;">{{ __('clinic.no_records') }}</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>

    @include('partials.pagination', ['paginator' => $patients])
@endsection
