@extends('layouts.app')

@section('title', $dealer->company_name)

@section('content')
<div class="grid md:grid-cols-3 gap-6">
    <div class="md:col-span-2 space-y-6">
        <div class="bg-white rounded shadow p-6">
            <div class="flex justify-between items-start">
                <div>
                    <h2 class="text-lg font-bold">{{ $dealer->company_name }}</h2>
                    <p class="text-sm text-slate-500">{{ $dealer->code }} — {{ $dealer->type->label() }}</p>
                </div>
                @can('update', $dealer)
                    <div class="flex gap-2">
                        <a href="{{ route('panel.dealers.edit', $dealer) }}" class="text-sm text-blue-600 hover:underline">Düzenle</a>
                        <form method="POST" action="{{ route('panel.dealers.toggle-status', $dealer) }}">
                            @csrf
                            <button class="text-sm text-amber-600 hover:underline">
                                {{ $dealer->status === 'active' ? 'Pasife Al' : 'Aktife Al' }}
                            </button>
                        </form>
                    </div>
                @endcan
            </div>
            <dl class="grid grid-cols-2 gap-3 mt-4 text-sm">
                <div><dt class="text-slate-400">Telefon</dt><dd>{{ $dealer->phone }}</dd></div>
                <div><dt class="text-slate-400">E-posta</dt><dd>{{ $dealer->email }}</dd></div>
                <div><dt class="text-slate-400">İl/İlçe</dt><dd>{{ $dealer->il }} / {{ $dealer->ilce }}</dd></div>
                <div><dt class="text-slate-400">Durum</dt><dd>{{ $dealer->status === 'active' ? 'Aktif' : 'Pasif' }}</dd></div>
            </dl>
        </div>

        <div class="bg-white rounded shadow p-6">
            <h3 class="font-semibold mb-3">Son Abonelikler</h3>
            <table class="w-full text-sm">
                <thead class="text-left text-slate-400"><tr><th class="pb-2">Ref No</th><th>Müşteri</th><th>Paket</th><th>Durum</th></tr></thead>
                <tbody>
                @forelse ($dealer->subscriptions as $s)
                    <tr class="border-t">
                        <td class="py-2">{{ $s->reference_no }}</td>
                        <td>{{ $s->customer_name }}</td>
                        <td>{{ $s->package->name }}</td>
                        <td>{{ $s->status->label() }}</td>
                    </tr>
                @empty
                    <tr><td colspan="4" class="text-center text-slate-400 py-4">Kayıt yok.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="space-y-6">
        @can('manageCommission', $dealer)
        <div class="bg-white rounded shadow p-6">
            <h3 class="font-semibold mb-3">Cari Hesap</h3>
            <dl class="space-y-2 text-sm">
                <div class="flex justify-between"><dt>Toplam Hakediş</dt><dd class="font-medium">{{ number_format($dealer->total_earnings, 2, ',', '.') }} TL</dd></div>
                <div class="flex justify-between"><dt>Ödenen Tutar</dt><dd class="font-medium">{{ number_format($dealer->total_paid, 2, ',', '.') }} TL</dd></div>
                <div class="flex justify-between border-t pt-2"><dt>Kalan Bakiye</dt><dd class="font-bold">{{ number_format($dealer->remaining_balance, 2, ',', '.') }} TL</dd></div>
            </dl>
        </div>

        <div class="bg-white rounded shadow p-6">
            <h3 class="font-semibold mb-3">Komisyon Modeli</h3>
            @php $active = $dealer->commissions->firstWhere('is_active', true); @endphp
            @if ($active)
                <p class="text-sm mb-3">
                    <span class="font-medium">{{ $active->type->label() }}</span>
                    @if(in_array($active->type->value, ['percentage','hybrid'])) — %{{ $active->percentage }} @endif
                    @if(in_array($active->type->value, ['fixed','hybrid'])) + {{ number_format($active->fixed_amount,2,',','.') }} TL @endif
                </p>
            @else
                <p class="text-sm text-amber-600 mb-3">Tanımlı aktif komisyon yok.</p>
            @endif

            <form method="POST" action="{{ route('panel.dealers.commission.store', $dealer) }}" class="space-y-2 text-sm">
                @csrf
                <select name="type" class="w-full border rounded px-2 py-1.5">
                    <option value="percentage">Yüzdelik</option>
                    <option value="fixed">Sabit</option>
                    <option value="hybrid">Karma</option>
                </select>
                <input type="number" step="0.01" name="percentage" placeholder="Yüzde (%)" class="w-full border rounded px-2 py-1.5">
                <input type="number" step="0.01" name="fixed_amount" placeholder="Sabit Tutar (TL)" class="w-full border rounded px-2 py-1.5">
                <input type="date" name="effective_date" required class="w-full border rounded px-2 py-1.5">
                <button class="w-full bg-slate-800 text-white rounded py-1.5">Komisyonu Güncelle</button>
            </form>
        </div>
        @endcan
    </div>
</div>
@endsection
