@extends('layouts.app')
@section('title', 'Raporlar')
@section('content')
<form method="GET" class="bg-white rounded shadow p-4 mb-6 flex flex-wrap gap-2 items-end">
    <div>
        <label class="block text-xs text-slate-500 mb-1">Başlangıç</label>
        <input type="date" name="date_from" value="{{ $filters['date_from'] ?? '' }}" class="border rounded px-3 py-1.5 text-sm">
    </div>
    <div>
        <label class="block text-xs text-slate-500 mb-1">Bitiş</label>
        <input type="date" name="date_to" value="{{ $filters['date_to'] ?? '' }}" class="border rounded px-3 py-1.5 text-sm">
    </div>
    <div>
        <label class="block text-xs text-slate-500 mb-1">Bayi</label>
        <select name="dealer_id" class="border rounded px-3 py-1.5 text-sm">
            <option value="">Tümü</option>
            @foreach ($dealers as $d)
                <option value="{{ $d->id }}" @selected(($filters['dealer_id'] ?? null)==$d->id)>{{ $d->company_name }}</option>
            @endforeach
        </select>
    </div>
    <div>
        <label class="block text-xs text-slate-500 mb-1">Firma</label>
        <select name="company_id" class="border rounded px-3 py-1.5 text-sm">
            <option value="">Tümü</option>
            @foreach ($companies as $c)
                <option value="{{ $c->id }}" @selected(($filters['company_id'] ?? null)==$c->id)>{{ $c->name }}</option>
            @endforeach
        </select>
    </div>
    <button class="bg-slate-800 text-white rounded px-4 py-1.5 text-sm">Filtrele</button>
    <a href="{{ route('panel.reports.export.excel', request()->query()) }}" class="bg-green-600 text-white rounded px-4 py-1.5 text-sm">Excel</a>
    <a href="{{ route('panel.reports.export.csv', request()->query()) }}" class="bg-emerald-600 text-white rounded px-4 py-1.5 text-sm">CSV</a>
    <a href="{{ route('panel.reports.export.pdf', request()->query()) }}" class="bg-red-600 text-white rounded px-4 py-1.5 text-sm">PDF</a>
</form>

<div class="grid md:grid-cols-3 gap-6">
    <div class="bg-white rounded shadow p-4">
        <h3 class="font-semibold mb-2">Satış Raporu</h3>
        <p class="text-sm">Toplam Tamamlanan Satış: <strong>{{ $salesReport['total'] }}</strong></p>
        <p class="text-xs text-slate-400 mt-2">Aylık kırılım için Excel/PDF export kullanın.</p>
    </div>
    <div class="bg-white rounded shadow p-4">
        <h3 class="font-semibold mb-2">Finans Raporu</h3>
        <p class="text-sm">Toplam Hakediş: <strong>{{ number_format($financeReport['total_earnings'],2,',','.') }} TL</strong></p>
        <p class="text-sm">Toplam Ödeme: <strong>{{ number_format($financeReport['total_payments'],2,',','.') }} TL</strong></p>
        <p class="text-sm">Kalan Borç: <strong>{{ number_format($financeReport['remaining_debt'],2,',','.') }} TL</strong></p>
    </div>
    <div class="bg-white rounded shadow p-4">
        <h3 class="font-semibold mb-2">Firma Performansı</h3>
        <ul class="text-sm space-y-1">
            @foreach ($companyPerformance->take(5) as $c)
                <li class="flex justify-between"><span>{{ $c->name }}</span><span>{{ $c->total_sales }}</span></li>
            @endforeach
        </ul>
    </div>
</div>

<div class="bg-white rounded shadow p-4 mt-6">
    <h3 class="font-semibold mb-3">Bayi Performans Raporu</h3>
    <table class="w-full text-sm">
        <thead class="text-left text-slate-400"><tr><th class="pb-2">Bayi</th><th>Toplam Satış</th><th>Toplam Hakediş</th><th>Ortalama Satış</th></tr></thead>
        <tbody>
        @foreach ($dealerPerformance as $row)
            <tr class="border-t">
                <td class="py-2">{{ $row['dealer'] }}</td>
                <td>{{ $row['total_sales'] }}</td>
                <td>{{ number_format($row['total_earnings'],2,',','.') }} TL</td>
                <td>{{ number_format($row['average_sale'],2,',','.') }} TL</td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>
@endsection
