@extends('admin.layouts.app')
@section('title', 'Feasibility Reports')
@section('breadcrumb')
    <span class="current">Feasibility Reports</span>
@endsection

@section('content')
<div class="page-header">
    <h1 class="page-title">
        <span class="title-icon"><i data-lucide="trending-up" style="width:20px;height:20px;"></i></span>
        Feasibility Reports
    </h1>
    <p class="page-subtitle">All generated land feasibility analysis reports</p>
</div>

<div class="admin-table-wrap">
    <div class="admin-table-header">
        <div class="admin-table-title">
            <i data-lucide="trending-up" class="icon" style="width:14px;height:14px;"></i>
            Reports
        </div>
        <form method="GET" style="display:flex;gap:8px;">
            <select name="status" class="form-control" style="width:140px;">
                <option value="">All Status</option>
                <option value="pending" {{ request('status') === 'pending' ? 'selected' : '' }}>Pending</option>
                <option value="processing" {{ request('status') === 'processing' ? 'selected' : '' }}>Processing</option>
                <option value="completed" {{ request('status') === 'completed' ? 'selected' : '' }}>Completed</option>
                <option value="failed" {{ request('status') === 'failed' ? 'selected' : '' }}>Failed</option>
            </select>
            <button type="submit" class="btn btn-secondary btn-sm">
                <i data-lucide="filter" style="width:12px;height:12px;"></i>
            </button>
        </form>
    </div>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Report ID</th>
                    <th>Location</th>
                    <th>State</th>
                    <th>User</th>
                    <th>Feasibility Score</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th style="text-align:right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($reports as $report)
                    @php
                        $statusColors = ['pending'=>'badge-orange','processing'=>'badge-blue','completed'=>'badge-green','failed'=>'badge-red'];
                        $score = $report->feasibility_score ?? 0;
                        $scoreColor = $score >= 70 ? 'var(--vsc-green-terminal)' : ($score >= 40 ? 'var(--vsc-orange)' : 'var(--vsc-red)');
                    @endphp
                    <tr>
                        <td style="font-family:'JetBrains Mono',monospace;font-size:11px;color:var(--vsc-text-secondary);">
                            #{{ str_pad($report->id, 5, '0', STR_PAD_LEFT) }}
                        </td>
                        <td>
                            <div style="font-weight:500;color:var(--vsc-text-active);">{{ Str::limit($report->address ?? '—', 30) }}</div>
                            <div style="font-size:11px;color:var(--vsc-text-secondary);">
                                {{ $report->latitude ? number_format($report->latitude, 4).'°N' : '' }}
                                {{ $report->longitude ? number_format($report->longitude, 4).'°E' : '' }}
                            </div>
                        </td>
                        <td><span class="badge badge-teal">{{ $report->state?->code ?? '—' }}</span></td>
                        <td style="font-size:12px;color:var(--vsc-text-secondary);">{{ $report->user?->name ?? 'System' }}</td>
                        <td>
                            <div style="display:flex;align-items:center;gap:8px;">
                                <div style="flex:1;height:4px;background:rgba(255,255,255,0.1);border-radius:2px;min-width:60px;">
                                    <div style="height:100%;border-radius:2px;width:{{ $score }}%;background:{{ $scoreColor }};"></div>
                                </div>
                                <span style="font-weight:600;color:{{ $scoreColor }};font-size:12px;">{{ $score }}%</span>
                            </div>
                        </td>
                        <td><span class="badge {{ $statusColors[$report->status] ?? 'badge-gray' }}">{{ ucfirst($report->status ?? 'pending') }}</span></td>
                        <td style="font-size:12px;color:var(--vsc-text-secondary);">
                            {{ $report->created_at->format('d M Y') }}
                        </td>
                        <td>
                            <div style="display:flex;justify-content:flex-end;gap:4px;">
                                <a href="{{ route('admin.feasibility.show', $report) }}" class="btn btn-icon btn-secondary" data-tooltip="View Report">
                                    <i data-lucide="eye" style="width:13px;height:13px;"></i>
                                </a>
                                @if($report->status === 'completed')
                                <a href="{{ route('admin.feasibility.pdf', $report->id) }}" class="btn btn-icon btn-secondary" data-tooltip="Download PDF" target="_blank">
                                    <i data-lucide="download" style="width:13px;height:13px;"></i>
                                </a>
                                @endif
                                <form method="POST" action="{{ route('admin.feasibility.destroy', $report) }}">
                                    @csrf @method('DELETE')
                                    <button type="submit" class="btn btn-icon btn-danger"
                                            data-tooltip="Delete"
                                            data-confirm-delete="Delete this report?">
                                        <i data-lucide="trash-2" style="width:13px;height:13px;"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="8" style="text-align:center;padding:48px;color:var(--vsc-text-secondary);">
                            <i data-lucide="trending-up" style="width:32px;height:32px;display:block;margin:0 auto 12px;opacity:0.3;"></i>
                            No feasibility reports yet
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if(isset($reports) && method_exists($reports, 'links') && $reports->hasPages())
        {{ $reports->links('vendor.pagination.admin') }}
    @endif
</div>
@endsection
