@extends('layouts.app')

@section('title','Reports & Analytics')
@section('header','Reports & Analytics')

@section('content')
<div class="row">
    <div>
        <h1>Advanced Dashboard & Reports</h1>
        <p class="muted">Track lead acquisition, conversion and sales performance.</p>
    </div>
    <form method="get" class="filters">
        <input type="date" name="from" value="{{ $from->format('Y-m-d') }}">
        <input type="date" name="to" value="{{ $to->format('Y-m-d') }}">
        <button class="btn">Apply</button>
    </form>
</div>

<div class="cards">
    <div>Total Leads<strong>{{ $total }}</strong></div>
    <div>Qualified<strong>{{ $qualified }}</strong></div>
    <div>Won<strong>{{ $won }}</strong></div>
    <div>Conversion<strong>{{ $conversion }}%</strong></div>
</div>

<div class="grid2">
    <div class="panel">
        <h2>Lead Sources</h2>
        <table>
            <tr><th>Source</th><th>Leads</th></tr>
            @forelse($sources as $row)
                <tr><td>{{ ucfirst($row->source ?: 'unknown') }}</td><td>{{ $row->total }}</td></tr>
            @empty
                <tr><td colspan="2">No data for this period.</td></tr>
            @endforelse
        </table>
    </div>

    <div class="panel">
        <h2>Pipeline Status</h2>
        <table>
            <tr><th>Status</th><th>Leads</th></tr>
            @forelse($statuses as $row)
                <tr><td>{{ ucfirst($row->status) }}</td><td>{{ $row->total }}</td></tr>
            @empty
                <tr><td colspan="2">No data for this period.</td></tr>
            @endforelse
        </table>
    </div>
</div>

<div class="panel">
    <h2>Salesperson Performance</h2>
    <table>
        <tr><th>Salesperson</th><th>Leads</th><th>Won</th><th>Conversion</th></tr>
        @forelse($salespeople as $row)
            <tr>
                <td>{{ $row['name'] }}</td>
                <td>{{ $row['total'] }}</td>
                <td>{{ $row['won'] }}</td>
                <td>{{ $row['conversion'] }}%</td>
            </tr>
        @empty
            <tr><td colspan="4">No sales users found.</td></tr>
        @endforelse
    </table>
</div>

<div class="cards">
    <div>Lost Leads<strong>{{ $lost }}</strong></div>
    <div>Quotations<strong>{{ $quotationTotal }}</strong></div>
</div>
@endsection
