@extends('layouts.app')

@section('title', 'Abonelikler')

@section('content')
<div class="flex justify-between items-center mb-4">
    <form method="GET" class="flex gap-2 flex-wrap">
        <select name="status" class="border rounded px-3 py-1.5 text-sm">
            <option value="">Tüm Durumlar</option>
            @foreach (['draft'=>'Taslak','pending'=>'Beklemede','reviewing'=>'İnceleniyor','approved'=>'Onaylandı','completed'=>'Tamamlandı','cancelled'=>'İptal'] as $val => $label)
                <option value="{{ $val }}" @selected(request('status')===$val)>{{ $label }}</option>
            @endforeach
        </select>
        <input type="date" name="date_from" value="{{ request('date_from') }}" class="border rounded px-3 py-1.5 text-sm">
        <input type="date" name="date_to" value="{{ request('date_to') }}" class="border rounded px-3 py-1.5 text-sm">
        <button class="bg-slate-800 text-white rounded px-4 py-1.5 text-sm">Filtrele</button>
    </form>
    @can('create', App\Models\Subscription::class)
        <a href="{{ route('panel.subscriptions.create') }}" class="bg-blue-600 text-white rounded px-4 py-2 text-sm font-medium">+ Yeni Abonelik</a>
    @endcan
</div>

<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">Ref No</th>
                <th class="p-3">Bayi</th>
                <th class="p-3">Müşteri</th>
                <th class="p-3">Paket</th>
                <th class="p-3">Ay</th>
                <th class="p-3">Toplam Tutar</th>
                <th class="p-3">Durum</th>
                <th class="p-3">Tarih</th>
                <th class="p-3">İşlem</th>
            </tr>
        </thead>
        <tbody>
        @forelse ($subscriptions as $s)
            <tr class="border-t">
                <td class="p-3">{{ $s->reference_no }}</td>
                <td class="p-3">{{ $s->dealer->company_name }}</td>
                <td class="p-3">{{ $s->customer_name }}</td>
                <td class="p-3">{{ $s->package->name }}</td>
                <td class="p-3">{{ $s->contract_months }}</td>
                <td class="p-3 font-medium">{{ number_format($s->total_price, 2, ',', '.') }} TL</td>
                <td class="p-3">
                    <span class="px-2 py-0.5 rounded text-xs
                        @class([
                            'bg-slate-200 text-slate-600' => $s->status->value === 'draft',
                            'bg-amber-100 text-amber-700' => in_array($s->status->value, ['pending','reviewing']),
                            'bg-blue-100 text-blue-700' => $s->status->value === 'approved',
                            'bg-green-100 text-green-700' => $s->status->value === 'completed',
                            'bg-red-100 text-red-700' => $s->status->value === 'cancelled',
                        ])">
                        {{ $s->status->label() }}
                    </span>
                </td>
                <td class="p-3">{{ $s->created_at->format('d.m.Y') }}</td>
                <td class="p-3">@include('panel.subscriptions._actions', ['subscription' => $s])</td>
            </tr>
        @empty
            <tr><td colspan="9" class="p-6 text-center text-slate-400">Kayıt bulunamadı.</td></tr>
        @endforelse
        </tbody>
    </table>
</div>

<div class="mt-4">{{ $subscriptions->links() }}</div>
@endsection
