@extends('layouts.app')

@section('title', 'Dashboard')
@section('page_title', 'Dashboard')

@section('content')
    <div class="portal-page-header">
        <p class="subtitle mb-0">Overview of leads, deadlines, sales pipeline, and vendor pricing.</p>
    </div>

    <div class="row g-3 mb-4">
        <div class="col-6 col-lg-3">
            <div class="portal-stat" style="--stat-accent: #4f46e5;">
                <div class="d-flex justify-content-between align-items-start">
                    <div>
                        <div class="portal-stat-value">{{ $stats['total_leads'] }}</div>
                        <div class="portal-stat-label">Total leads</div>
                    </div>
                    <div class="portal-stat-icon">📋</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-lg-3">
            <div class="portal-stat" style="--stat-accent: #ef4444;">
                <div class="d-flex justify-content-between align-items-start">
                    <div>
                        <div class="portal-stat-value">{{ $stats['deadline_alerts'] }}</div>
                        <div class="portal-stat-label">Deadline alerts</div>
                    </div>
                    <div class="portal-stat-icon" style="background:#fef2f2;color:#ef4444;">⏰</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-lg-3">
            <div class="portal-stat" style="--stat-accent: #f59e0b;">
                <div class="d-flex justify-content-between align-items-start">
                    <div>
                        <div class="portal-stat-value">{{ $stats['sales_pipeline'] }}</div>
                        <div class="portal-stat-label">Sales pipeline</div>
                    </div>
                    <div class="portal-stat-icon" style="background:#fffbeb;color:#d97706;">📈</div>
                </div>
            </div>
        </div>
        <div class="col-6 col-lg-3">
            <div class="portal-stat" style="--stat-accent: #10b981;">
                <div class="d-flex justify-content-between align-items-start">
                    <div>
                        <div class="portal-stat-value">{{ $stats['with_quotes'] }}</div>
                        <div class="portal-stat-label">With vendor quotes</div>
                    </div>
                    <div class="portal-stat-icon" style="background:#ecfdf5;color:#059669;">💰</div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-12">
            <div class="portal-card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <span>Project-wise vendor price</span>
                    @if(can_access_module('leads.listing'))
                        <a href="{{ route('leads.listing') }}" class="btn btn-sm btn-portal-outline">All leads</a>
                    @endif
                </div>
                <div class="portal-table-wrap">
                    <table class="table portal-table mb-0">
                        <thead>
                        <tr>
                            <th>Project (Fair)</th>
                            <th>Company</th>
                            <th>Vendor</th>
                            <th>Vendor cost</th>
                            <th>Client price</th>
                            <th>Type</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($projectVendorPrices as $projectVendorPrice)
                            @php($lead = $projectVendorPrice['lead'])
                            @php($quote = $projectVendorPrice['quote'])
                            <tr>
                                <td><a href="{{ route('leads.show', $lead) }}" class="lead-link">{{ $lead->displayFairName() }}</a></td>
                                <td>{{ $lead->company_name ?: '—' }}</td>
                                <td>{{ $quote->vendor_name }}</td>
                                <td>{{ number_format((float) $quote->vendor_raw_cost, 0) }}</td>
                                <td><strong>{{ number_format((float) $quote->client_final_price, 0) }}</strong></td>
                                <td>
                                    @if($projectVendorPrice['is_selected'])
                                        <span class="portal-badge portal-badge-won">Selected</span>
                                    @else
                                        <span class="portal-badge portal-badge-default">Lowest</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr><td colspan="6"><div class="portal-empty">No vendor quotes yet.</div></td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="portal-card portal-card-alert">
                <div class="card-header">Deadline alerts <span class="fw-normal text-muted small">(next 21 days)</span></div>
                <div class="portal-table-wrap">
                    <table class="table portal-table mb-0">
                        <thead>
                        <tr>
                            <th>Fair</th>
                            <th>Company</th>
                            <th>Status</th>
                            <th>Start</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($deadlineAlerts as $lead)
                            <tr>
                                <td><a href="{{ route('leads.show', $lead) }}" class="lead-link">{{ $lead->displayFairName() }}</a></td>
                                <td>{{ $lead->company_name ?: '—' }}</td>
                                <td>@include('partials.lead-status-badge', ['status' => $lead->clean_lead_status])</td>
                                <td>{{ optional($lead->effectiveFairStartDate())->format('M d, Y') ?: '—' }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4"><div class="portal-empty py-4">No upcoming deadlines.</div></td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-lg-6">
            <div class="portal-card">
                <div class="card-header">Sales view <span class="fw-normal text-muted small">(Quoted & Negotiation)</span></div>
                <div class="portal-table-wrap">
                    <table class="table portal-table mb-0">
                        <thead>
                        <tr>
                            <th>Fair</th>
                            <th>Company</th>
                            <th>Status</th>
                            <th>Start</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($salesLeads as $lead)
                            <tr>
                                <td><a href="{{ route('leads.show', $lead) }}" class="lead-link">{{ $lead->displayFairName() }}</a></td>
                                <td>{{ $lead->company_name ?: '—' }}</td>
                                <td>@include('partials.lead-status-badge', ['status' => $lead->clean_lead_status])</td>
                                <td>{{ optional($lead->effectiveFairStartDate())->format('M d, Y') ?: '—' }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="4"><div class="portal-empty py-4">No leads in sales stages.</div></td></tr>
                        @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
