@extends('layouts.app')
@section('title', 'Rekap Gaji Kurir')
@section('content')

<div class="max-w-5xl mx-auto" style="font-family: 'Plus Jakarta Sans', sans-serif;">

    {{-- Header --}}
    <div class="mb-8">
        <h1 class="text-3xl font-black text-slate-800 italic tracking-tight">Gaji Kurir 💰</h1>
        <p class="text-pink-400 font-bold text-sm mt-1">Rekap penghasilan kurir per bulan</p>
    </div>

    {{-- Filter Bulan --}}
    <form method="GET" class="bg-white rounded-[28px] border border-pink-50 shadow-sm p-5 mb-8 flex flex-wrap gap-4 items-end">
        <div>
            <label class="block text-[10px] font-black text-slate-500 uppercase tracking-widest mb-1">Bulan</label>
            <select name="month" class="px-4 py-2.5 rounded-xl bg-slate-50 border-2 border-transparent focus:border-pink-300 outline-none text-sm font-bold text-slate-700">
                @for($m = 1; $m <= 12; $m++)
                    <option value="{{ $m }}" {{ $m == $month ? 'selected' : '' }}>
                        {{ \Carbon\Carbon::create()->month($m)->translatedFormat('F') }}
                    </option>
                @endfor
            </select>
        </div>
        <div>
            <label class="block text-[10px] font-black text-slate-500 uppercase tracking-widest mb-1">Tahun</label>
            <select name="year" class="px-4 py-2.5 rounded-xl bg-slate-50 border-2 border-transparent focus:border-pink-300 outline-none text-sm font-bold text-slate-700">
                @for($y = date('Y'); $y >= date('Y') - 3; $y--)
                    <option value="{{ $y }}" {{ $y == $year ? 'selected' : '' }}>{{ $y }}</option>
                @endfor
            </select>
        </div>
        <button type="submit" class="px-6 py-2.5 bg-pink-500 hover:bg-pink-600 text-white font-black text-xs uppercase tracking-widest rounded-xl transition shadow-lg shadow-pink-100">
            Tampilkan
        </button>
    </form>

    {{-- Tabel Gaji --}}
    @if($salaries->isEmpty())
        <div class="bg-white rounded-[32px] border-2 border-dashed border-pink-100 py-20 text-center">
            <p class="text-5xl mb-4">📭</p>
            <p class="text-slate-400 font-black italic text-lg">Belum ada data gaji bulan ini.</p>
            <p class="text-slate-300 text-sm mt-1">Gaji otomatis masuk saat kurir menyelesaikan pengiriman.</p>
        </div>
    @else
        <div class="space-y-4">
            @foreach($salaries as $s)
            <div class="bg-white rounded-[28px] border border-pink-50 shadow-sm p-6 flex flex-wrap items-center justify-between gap-4">

                {{-- Info Kurir --}}
                <div class="flex items-center gap-4">
                    <img src="{{ $s->courier->avatar ? asset('storage/' . $s->courier->avatar) : 'https://ui-avatars.com/api/?name=' . urlencode($s->courier->name) . '&background=fce7f3&color=db2777' }}"
                         class="w-12 h-12 rounded-2xl object-cover border-2 border-pink-100">
                    <div>
                        <p class="font-black text-slate-800">{{ $s->courier->name }}</p>
                        <p class="text-xs text-slate-400 font-bold">{{ $s->month_name }} {{ $s->year }}</p>
                    </div>
                </div>

                {{-- Rincian Gaji --}}
                <div class="flex flex-wrap gap-6 text-center">
                    <div>
                        <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Pengiriman</p>
                        <p class="text-xl font-black text-slate-800">{{ $s->total_deliveries }}x</p>
                    </div>
                    <div>
                        <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Gaji Pokok</p>
                        <p class="text-base font-black text-slate-700">Rp {{ number_format($s->base_salary, 0, ',', '.') }}</p>
                    </div>
                    <div>
                        <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Bonus Antar</p>
                        <p class="text-base font-black text-pink-500">+ Rp {{ number_format($s->bonus, 0, ',', '.') }}</p>
                    </div>
                    <div>
                        <p class="text-[10px] font-black text-slate-400 uppercase tracking-widest">Total</p>
                        <p class="text-xl font-black text-emerald-600">Rp {{ number_format($s->total_salary, 0, ',', '.') }}</p>
                    </div>
                </div>

                {{-- Status & Aksi --}}
                <div class="flex items-center gap-3">
                    @if($s->status === 'paid')
                        <div class="text-right">
                            <span class="px-4 py-1.5 bg-emerald-100 text-emerald-600 text-[10px] font-black uppercase tracking-widest rounded-full">✅ Sudah Dibayar</span>
                            <p class="text-[10px] text-slate-400 mt-1">{{ $s->paid_at->format('d M Y') }}</p>
                        </div>
                    @else
                        <form action="{{ route('admin.courier-salary.pay', $s->id) }}" method="POST">
                            @csrf
                            @method('PATCH')
                            <button type="submit"
                                class="px-6 py-2.5 bg-pink-500 hover:bg-pink-600 text-white font-black text-xs uppercase tracking-widest rounded-xl transition shadow-lg shadow-pink-100">
                                💸 Bayar Gaji
                            </button>
                        </form>
                    @endif
                </div>

            </div>
            @endforeach
        </div>
    @endif

</div>

<script>lucide.createIcons();</script>
@endsection