@extends('layouts.app')

@section('title', 'Reports')
@section('heading', 'Reports')
@section('subheading')
    Leads and customers between {{ $from->toFormattedDateString() }} and {{ $to->toFormattedDateString() }}
@endsection

@section('content')
    <div class="card mb-1">
        <form method="GET" action="{{ route('reports.index') }}" class="flex gap" style="flex-wrap:wrap;align-items:flex-end">
            <div class="form-group mb-0">
                <label for="from">From</label>
                <input id="from" class="form-control" type="date" name="from" value="{{ $from->toDateString() }}">
            </div>
            <div class="form-group mb-0">
                <label for="to">To</label>
                <input id="to" class="form-control" type="date" name="to" value="{{ $to->toDateString() }}">
            </div>
            <button type="submit" class="btn btn-primary btn-sm">Apply</button>
        </form>
    </div>

    <div class="kpi-grid mb-1">
        <article class="kpi-card kpi-card--indigo">
            <div class="kpi-card-head">
                <span class="kpi-card-label">Leads in range</span>
            </div>
            <div class="kpi-card-value">{{ number_format($report['leads_in_range']) }}</div>
        </article>
        <article class="kpi-card kpi-card--cyan">
            <div class="kpi-card-head">
                <span class="kpi-card-label">New customers</span>
            </div>
            <div class="kpi-card-value">{{ number_format($report['new_customers']) }}</div>
        </article>
    </div>

    <div class="flex gap mb-1" style="flex-wrap:wrap">
        <div class="card" style="flex:1;min-width:16rem">
            <h2 class="card-title">By source</h2>
            <div class="table-wrap">
                <table class="data-table">
                    <thead><tr><th>Source</th><th>Count</th></tr></thead>
                    <tbody>
                    @forelse ($report['by_source'] as $row)
                        <tr>
                            <td>{{ $row['label'] }}</td>
                            <td>{{ number_format($row['count']) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="2">No leads in this period.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        <div class="card" style="flex:1;min-width:16rem">
            <h2 class="card-title">By stage</h2>
            <div class="table-wrap">
                <table class="data-table">
                    <thead><tr><th>Stage</th><th>Count</th></tr></thead>
                    <tbody>
                    @forelse ($report['by_stage'] as $row)
                        <tr>
                            <td>{{ $row['label'] }}</td>
                            <td>{{ number_format($row['count']) }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="2">No leads in this period.</td></tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection
