@extends('layouts.app')
@section('title', 'Reports & Analytics')

@section('content')
@php($maxDaily = max(1, (int) $daily->max('applications')))
<div class="space-y-5">
    <div class="flex flex-col gap-3 lg:flex-row lg:items-end lg:justify-between"><div><p class="text-[10px] font-bold uppercase tracking-[.18em] text-emerald-600">Decision intelligence</p><h1 class="mt-1 text-2xl font-bold text-[#0b1f38]">Reports & Analytics</h1><p class="mt-1 text-xs text-slate-500">Applications, services, staff, finance, HR and renewals from one trusted dataset.</p></div><form class="flex flex-wrap items-end gap-2"><label class="text-[9px] font-bold text-slate-500">From<input name="from" type="date" value="{{ $from->format('Y-m-d') }}" class="erp-input mt-1 block"></label><label class="text-[9px] font-bold text-slate-500">To<input name="to" type="date" value="{{ $to->format('Y-m-d') }}" class="erp-input mt-1 block"></label><button class="min-h-[42px] rounded-xl bg-[#0b1f38] px-4 text-xs font-bold text-white">Apply Range</button>@if(auth()->user()->hasPermission('reports.export'))<button name="export" value="csv" class="min-h-[42px] rounded-xl bg-emerald-600 px-4 text-xs font-bold text-white">Export CSV</button>@endif</form></div>

    <div class="grid gap-3 sm:grid-cols-2 xl:grid-cols-5">
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-blue-50 text-blue-600">A</span><p class="erp-kpi-label">Applications</p><p class="erp-kpi-value">{{ number_format($summary['applications']) }}</p><p class="erp-kpi-note">{{ $summary['completion_rate'] }}% completed</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-emerald-50 text-emerald-600">AED</span><p class="erp-kpi-label">Customer Collection</p><p class="erp-kpi-value text-xl">AED {{ number_format($summary['collection'], 2) }}</p><p class="erp-kpi-note">Successful payments</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-orange-50 text-orange-600">R</span><p class="erp-kpi-label">Service Revenue</p><p class="erp-kpi-value text-xl">AED {{ number_format($summary['service_revenue'], 2) }}</p><p class="erp-kpi-note">Excluding government fees</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-violet-50 text-violet-600">G</span><p class="erp-kpi-label">Government Fees</p><p class="erp-kpi-value text-xl">AED {{ number_format($summary['government_fees'], 2) }}</p><p class="erp-kpi-note">Pass-through amount</p></div>
        <div class="erp-kpi-card"><span class="erp-kpi-icon bg-rose-50 text-rose-600">O</span><p class="erp-kpi-label">Outstanding</p><p class="erp-kpi-value text-xl">AED {{ number_format($summary['outstanding'], 2) }}</p><p class="erp-kpi-note">Open invoice balances</p></div>
    </div>

    <div class="grid gap-4 xl:grid-cols-[1.6fr_1fr]">
        <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Application Volume</h2><p>{{ $from->format('d M Y') }} to {{ $to->format('d M Y') }}</p></div><span class="text-[10px] font-bold text-slate-400">{{ $summary['completed'] }} completed · {{ $summary['returned'] }} returned · {{ $summary['rejected'] }} rejected</span></div><div class="mt-5 flex h-[220px] items-end gap-1 overflow-x-auto border-b border-slate-200 pb-0">@foreach($daily as $day)<div class="group flex h-full min-w-[18px] flex-1 flex-col justify-end text-center" title="{{ $day['label'] }} · {{ $day['applications'] }} applications · AED {{ number_format($day['collection'],2) }}"><span class="mb-1 text-[7px] font-bold text-slate-400 opacity-0 group-hover:opacity-100">{{ $day['applications'] }}</span><div class="mx-auto w-[70%] rounded-t bg-gradient-to-t from-blue-700 to-cyan-400" style="height: {{ max(3, round(($day['applications']/$maxDaily)*100)) }}%"></div><span class="mt-2 hidden text-[7px] text-slate-400 2xl:block">{{ $day['label'] }}</span></div>@endforeach</div></section>
        <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Renewal Performance</h2><p>Opportunity-to-application conversion</p></div><strong class="text-lg text-violet-700">{{ $summary['renewal_conversion'] }}%</strong></div><div class="mt-6 grid place-items-center"><div class="grid h-40 w-40 place-items-center rounded-full" style="background: conic-gradient(#7c3aed {{ $summary['renewal_conversion'] }}%, #f1f5f9 0)"><div class="grid h-28 w-28 place-items-center rounded-full bg-white text-center"><div><strong class="block text-2xl text-[#0b1f38]">{{ $summary['renewal_converted'] }}</strong><span class="text-[9px] text-slate-400">of {{ $summary['renewals'] }} converted</span></div></div></div></div><div class="mt-6 grid grid-cols-2 gap-2 text-center"><div class="rounded-xl bg-slate-50 p-3"><strong class="block text-lg text-[#0b1f38]">{{ $summary['renewals'] }}</strong><span class="text-[9px] text-slate-400">Opportunities</span></div><div class="rounded-xl bg-slate-50 p-3"><strong class="block text-lg text-[#0b1f38]">AED {{ number_format($summary['vat'],2) }}</strong><span class="text-[9px] text-slate-400">VAT tracked</span></div></div></section>
    </div>

    <div class="grid gap-4 xl:grid-cols-2">
        <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Service Performance</h2><p>Revenue remains separated from government pass-through fees.</p></div></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Service</th><th>Applications</th><th>Completed</th><th>Service Revenue</th><th>Government Fees</th></tr></thead><tbody>@forelse($servicePerformance as $row)<tr><td><strong>{{ $row['name'] }}</strong></td><td>{{ $row['applications'] }}</td><td>{{ $row['completed'] }}</td><td class="font-bold text-emerald-700">AED {{ number_format($row['revenue'],2) }}</td><td class="font-bold text-violet-700">AED {{ number_format($row['government'],2) }}</td></tr>@empty<tr><td colspan="5"><div class="erp-empty"><span>S</span><strong>No service activity in this range</strong><p>Choose a wider date range or create applications.</p></div></td></tr>@endforelse</tbody></table></div></section>
        <section class="erp-panel"><div class="erp-panel-heading"><div><h2>Staff Performance</h2><p>Assigned workload, completion, returns and SLA risk.</p></div></div><div class="mt-4 overflow-x-auto"><table class="erp-table"><thead><tr><th>Employee</th><th>Assigned</th><th>Completed</th><th>Returned</th><th>SLA Risk</th></tr></thead><tbody>@forelse($staffPerformance as $row)<tr><td><strong>{{ $row['name'] }}</strong></td><td>{{ $row['assigned'] }}</td><td class="font-bold text-emerald-700">{{ $row['completed'] }}</td><td class="font-bold text-amber-700">{{ $row['returned'] }}</td><td class="font-bold text-rose-700">{{ $row['sla_risk'] }}</td></tr>@empty<tr><td colspan="5"><div class="erp-empty"><span>P</span><strong>No assigned activity</strong><p>Staff metrics appear after applications are assigned.</p></div></td></tr>@endforelse</tbody></table></div></section>
    </div>

    @if(auth()->user()->hasPermission('employees.view') || auth()->user()->hasPermission('payroll.salary.view'))
    <section class="erp-panel"><div class="erp-panel-heading"><div><h2>HR & Payroll Summary</h2><p>Role-aware employee, attendance, leave, payroll, document, gratuity and loan totals.</p></div><a href="{{ route('app.employees.index') }}">Employee directory</a></div><div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-4 xl:grid-cols-6"><div class="rounded-xl bg-slate-50 p-4"><small>Active employees</small><strong class="mt-1 block text-xl">{{ $hrSummary['active_employees'] }}</strong></div><div class="rounded-xl bg-emerald-50 p-4"><small>Present records</small><strong class="mt-1 block text-xl text-emerald-700">{{ $hrSummary['present'] }}</strong></div><div class="rounded-xl bg-rose-50 p-4"><small>Absent records</small><strong class="mt-1 block text-xl text-rose-700">{{ $hrSummary['absent'] }}</strong></div><div class="rounded-xl bg-amber-50 p-4"><small>Late records</small><strong class="mt-1 block text-xl text-amber-700">{{ $hrSummary['late'] }}</strong></div><div class="rounded-xl bg-blue-50 p-4"><small>Overtime</small><strong class="mt-1 block text-xl text-blue-700">{{ intdiv($hrSummary['overtime_minutes'],60) }}h {{ $hrSummary['overtime_minutes']%60 }}m</strong></div><div class="rounded-xl bg-amber-50 p-4"><small>Documents due</small><strong class="mt-1 block text-xl text-amber-700">{{ $hrSummary['documents_due'] }}</strong></div></div>
        @if($hrSummary['payroll_net'] !== null)<div class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-5"><div class="rounded-xl border border-slate-200 p-4"><small>Payroll gross</small><strong class="mt-1 block">AED {{ number_format($hrSummary['payroll_gross'],2) }}</strong></div><div class="rounded-xl border border-slate-200 p-4"><small>Payroll deductions</small><strong class="mt-1 block text-rose-700">AED {{ number_format($hrSummary['payroll_deductions'],2) }}</strong></div><div class="rounded-xl border border-emerald-200 p-4"><small>Payroll net</small><strong class="mt-1 block text-emerald-700">AED {{ number_format($hrSummary['payroll_net'],2) }}</strong></div><div class="rounded-xl border border-slate-200 p-4"><small>Active loans</small><strong class="mt-1 block">AED {{ number_format($hrSummary['loan_balance'],2) }}</strong></div><div class="rounded-xl border border-slate-200 p-4"><small>Approved gratuity</small><strong class="mt-1 block">AED {{ number_format($hrSummary['gratuity_approved'],2) }}</strong></div></div>@endif
    </section>
    @endif
</div>
@endsection
