@php
    $layout = auth()->user()->role === 'super_admin' ? 'layouts.admin' : 'component2.layouts.app';
@endphp

@extends($layout)

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-3">
    <h3></h3>
    <div class="d-flex gap-2">
        <a href="{{ route('component2.create') }}" class="btn btn-success">
            <i class="bi bi-person-plus"></i> Add Beneficiary
        </a>
        <a href="{{ route('component2.export') }}" class="btn btn-info text-white">
            <i class="bi bi-file-excel"></i> Export Excel
        </a>
        <button onclick="generatePDF()" class="btn btn-danger">
            <i class="bi bi-file-pdf"></i> PDF Report
        </button>
    </div>
</div>
<div class="container-fluid px-4 py-3">
<!-- Dashboard Header -->
    <div class="dashboard-header">
        <div class="container-fluid">
            <div class="row align-items-center dashboard-header-content">
                <div class="col-lg-6">
                    <h1 class="dashboard-title">
                        <i class="fas fa-tachometer-alt" style="color: var(--dhis2-blue-primary);"></i>
                        WEOF Beneficiaries Dashboard
                    </h1>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
                            <li class="breadcrumb-item active">Component 2</li>
                        </ol>
                    </nav>
                </div>
                <div class="col-lg-6">
                    <div class="d-flex justify-content-lg-end gap-2 dashboard-actions">
                        <a href="{{ route('component2.create') }}" class="btn btn-success">
                            <i class="bi bi-person-plus"></i> Add Beneficiary
                        </a>
                        <a href="{{ route('component2.export') }}" class="btn btn-info text-white">
                            <i class="bi bi-file-excel"></i> Export Excel
                        </a>
                        <button onclick="generatePDF()" class="btn btn-danger">
                            <i class="bi bi-file-pdf"></i> PDF Report
                        </button>
                        <!-- <a href="{{ route('component1.weof.index') }}" class="btn btn-dhis2-primary">
                            <i class="fas fa-briefcase me-1"></i> WEOF Beneficiaries
                        </a> -->
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <form method="GET" class="row g-3 mb-4">
        <div class="col-md-3">
            <select name="county" class="form-select">
                <option value="">All Counties</option>
                @foreach(\App\Models\C2WeofBeneficiary::distinct()->orderBy('county')->pluck('county') as $c)
                    <option value="{{ $c }}" {{ request('county')==$c ? 'selected':'' }}>{{ $c }}</option>
                @endforeach
            </select>
        </div>
        <div class="col-md-3">
            <select name="accelerator" class="form-select">
                <option value="">Accelerator Status</option>
                <option value="1" {{ request('accelerator')=='1' ? 'selected':'' }}>In Accelerator</option>
                <option value="graduated" {{ request('accelerator')=='graduated' ? 'selected':'' }}>Graduated</option>
            </select>
        </div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-primary w-100">Filter</button>
        </div>
        <div class="col-md-2">
            <a href="{{ route('component2.index') }}" class="btn btn-secondary w-100">Clear</a>
        </div>
    </form>

    <!-- Stats -->
    <div class="row mb-4 g-3">
        <div class="col-md-3"><div class="stat-card p-3 rounded shadow-sm"><h5>Total</h5><h3 class="text-success">{{ $stats['total'] }}</h3></div></div>
        <div class="col-md-3"><div class="stat-card p-3 rounded shadow-sm"><h5>In Accelerator</h5><h3 class="text-primary">{{ $stats['accelerator'] }}</h3></div></div>
        <div class="col-md-3"><div class="stat-card p-3 rounded shadow-sm"><h5>Graduated</h5><h3 class="text-info">{{ $stats['graduated'] }}</h3></div></div>
        <div class="col-md-3"><div class="stat-card p-3 rounded shadow-sm"><h5>Received Grant</h5><h3 class="text-warning">{{ $stats['grant_received'] }}</h3></div></div>
    </div>

    <!-- Charts -->
    <div class="row mb-5">
        <div class="col-lg-6">
            <div class="card shadow-sm">
                <div class="card-header bg-success text-white">Business Registration Status</div>
                <div class="card-body"><canvas id="registrationChart"></canvas></div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="card shadow-sm">
                <div class="card-header bg-success text-white">Accelerator Progress</div>
                <div class="card-body"><canvas id="acceleratorChart"></canvas></div>
            </div>
        </div>
    </div>

    <!-- Table -->
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-success">
                        <tr>
                            <th>WEOF ID</th>
                            <th>Name</th>
                            <th>Business</th>
                            <th>County/Payam</th>
                            <th>Revenue</th>
                            <th>Accelerator</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($beneficiaries as $b)
                        <tr>
                            <td><strong>{{ $b->woef_id }}</strong></td>
                            <td>{{ $b->name }}</td>
                            <td>{{ Str::limit($b->business_name, 25) }}</td>
                            <td>{{ $b->county }} / {{ $b->payam }}</td>
                            <td>{{ $b->monthly_revenue ? '$'.number_format($b->monthly_revenue) : '-' }}</td>
                            <td>
                                @if($b->accelerator_graduation)
                                    <span class="badge bg-success">Graduated</span>
                                @elseif($b->accelerator_participation)
                                    <span class="badge bg-primary">In Program</span>
                                @else
                                    <span class="badge bg-secondary">Not Enrolled</span>
                                @endif
                            </td>
                            <td>
                                <a href="{{ route('component2.edit', $b) }}" class="btn btn-sm btn-warning"><i class="bi bi-pencil"></i></a>
                                <form action="{{ route('component2.destroy', $b) }}" method="POST" class="d-inline">
                                    @csrf @method('DELETE')
                                    <button class="btn btn-sm btn-danger" onclick="return confirm('Delete?')"><i class="bi bi-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                        @empty
                        <tr><td colspan="7" class="text-center py-4 text-muted">No beneficiaries found.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
            {{ $beneficiaries->links() }}
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jspdf@2.5.1/dist/jspdf.umd.min.js"></script>
<script>
const stats = @json($stats);
const registered = {{ \App\Models\C2WeofBeneficiary::where('registration_status', true)->count() }};
const notRegistered = {{ \App\Models\C2WeofBeneficiary::where('registration_status', false)->count() }};

new Chart(document.getElementById('registrationChart'), {
    type: 'doughnut',
    data: {
        labels: ['Registered', 'Not Registered'],
        datasets: [{ data: [registered, notRegistered], backgroundColor: ['#006400', '#dc3545'] }]
    },
    options: { responsive: true }
});

new Chart(document.getElementById('acceleratorChart'), {
    type: 'bar',
    data: {
        labels: ['Applied', 'In Program', 'Graduated'],
        datasets: [{
            label: 'Count',
            data: [
                {{ \App\Models\C2WeofBeneficiary::whereNotNull('pitch_outcome')->count() }},
                stats.accelerator,
                stats.graduated
            ],
            backgroundColor: '#006400'
        }]
    },
    options: { responsive: true }
});

function generatePDF() {
    const { jsPDF } = window.jspdf;
    const doc = new jsPDF('l', 'mm', 'a4');
    doc.setFontSize(18);
    doc.text('WEOF Beneficiaries Report', 15, 20);
    doc.setFontSize(12);
    doc.text('Generated: {{ now()->format('d M Y') }}', 15, 30);
    doc.setFontSize(11);
    doc.text(`Total: ${stats.total} | Accelerator: ${stats.accelerator} | Graduated: ${stats.graduated}`, 15, 45);
    doc.save('WEOF_Report_{{ now()->format('Y-m-d') }}.pdf');
}
</script>
@endpush