@extends('layouts.app')
@section('title', 'Başvurular')
@section('content')
<form method="GET" class="flex flex-wrap gap-2 mb-4 items-center">
    <select name="status" class="border rounded px-3 py-1.5 text-sm">
        <option value="">Tüm Durumlar</option>
        @foreach (['new'=>'Yeni','assigned'=>'Atandı','contacted'=>'İletişime Geçildi','converted'=>'Aboneliğe Dönüştü','rejected'=>'Reddedildi'] as $val=>$label)
            <option value="{{ $val }}" @selected(request('status')===$val)>{{ $label }}</option>
        @endforeach
    </select>
    <select name="kaynak" class="border rounded px-3 py-1.5 text-sm">
        <option value="">Tüm Kaynaklar</option>
        <option value="bireysel" @selected(request('kaynak')==='bireysel')>Bireysel</option>
        <option value="bayi" @selected(request('kaynak')==='bayi')>Bayi</option>
    </select>
    <select name="dealer_id" class="border rounded px-3 py-1.5 text-sm">
        <option value="">Tüm Bayiler</option>
        @foreach ($dealers as $d)
            <option value="{{ $d->id }}" @selected(request('dealer_id')==$d->id)>{{ $d->company_name }}</option>
        @endforeach
    </select>
    <button class="bg-slate-800 text-white rounded px-4 py-1.5 text-sm">Filtrele</button>
</form>
<div class="bg-white rounded shadow overflow-x-auto">
    <table class="w-full text-sm">
        <thead class="bg-slate-100 text-left"><tr><th class="p-3">Ad</th><th class="p-3">Telefon</th><th class="p-3">Paket</th><th class="p-3">Durum</th><th class="p-3">Kaynak</th><th class="p-3">İşlem</th></tr></thead>
        <tbody>
        @forelse ($applications as $app)
            <tr class="border-t">
                <td class="p-3">{{ $app->name }}</td>
                <td class="p-3">{{ $app->phone }}</td>
                <td class="p-3">{{ $app->package->name }}</td>
                <td class="p-3">
                    @php
                        $statusLabels = ['new'=>'Yeni','assigned'=>'Atandı','contacted'=>'İletişime Geçildi','converted'=>'Aboneliğe Dönüştü','rejected'=>'Reddedildi'];
                    @endphp
                    @if (in_array($app->status, ['converted', 'rejected']))
                        <span class="text-xs font-medium {{ $app->status === 'converted' ? 'text-green-600' : 'text-red-500' }}">{{ $statusLabels[$app->status] }}</span>
                    @else
                        <form method="POST" action="{{ route('panel.applications.update-status', $app) }}" class="flex gap-1 items-center">
                            @csrf
                            @method('PATCH')
                            <select name="status" onchange="this.form.submit()" class="border rounded text-xs px-1 py-1">
                                <option value="new" @selected($app->status==='new')>Yeni</option>
                                <option value="contacted" @selected($app->status==='contacted')>İletişime Geçildi</option>
                                <option value="rejected" @selected($app->status==='rejected')>Reddedildi</option>
                            </select>
                        </form>
                    @endif
                </td>
                <td class="p-3">
                    @if ($app->assignedDealer)
                        <span class="inline-block bg-gradient-to-r from-indigo-500 via-purple-500 to-pink-500 text-white text-xs font-semibold px-3 py-1 rounded-full shadow-sm">{{ $app->assignedDealer->company_name }}</span>
                    @else
                        <span class="inline-block bg-slate-100 text-slate-500 text-xs font-medium px-3 py-1 rounded-full">Bireysel</span>
                    @endif
                </td>
                <td class="p-3">
                    @if ($app->status !== 'converted' && $app->status !== 'rejected')
                    <form method="POST" action="{{ route('panel.applications.convert', $app) }}" class="flex gap-1 items-center">
                        @csrf
                        @if (! $app->assigned_dealer_id)
                            <select name="dealer_id" required class="border rounded text-xs px-1 py-1">
                                <option value="">Bayi Seç</option>
                                @foreach ($dealers as $d)
                                    <option value="{{ $d->id }}">{{ $d->company_name }}</option>
                                @endforeach
                            </select>
                        @endif
                        <button class="text-green-600 text-xs hover:underline whitespace-nowrap">Aboneliğe Dönüştür</button>
                    </form>
                    @endif
                </td>
            </tr>
        @empty
            <tr><td colspan="6" class="p-6 text-center text-slate-400">Kayıt bulunamadı.</td></tr>
        @endforelse
        </tbody>
    </table>
</div>
<div class="mt-4">{{ $applications->links() }}</div>
@endsection
