@extends('layouts.app')
@section('title', 'Semua Riwayat Analisis - Admin')

@push('styles')
<style>
/* ===== DARK MODE FOR ADMIN ANALYSES PAGE ===== */

.dark .page-title {
    color: #f1f5f9 !important;
}

.dark .page-subtitle {
    color: #cbd5e1 !important;
}

/* Stat Cards */
.dark [style*="background:#fff"] {
    background: #1e293b !important;
}

.dark [style*="background:#fff;border:1px solid #E2E8F0"] {
    background: #1e293b !important;
    border-color: #334155 !important;
}

.dark [style*="border:1px solid #E2E8F0"] {
    border-color: #334155 !important;
}

/* Form and Filter */
.dark .card {
    background: #1e293b !important;
    border-color: #334155 !important;
}

.dark .form-control {
    background: #334155 !important;
    border-color: #475569 !important;
    color: #f1f5f9 !important;
}

.dark .form-control::placeholder {
    color: #94a3b8 !important;
}

.dark [style*="color:#64748B"][style*="display:block"] {
    color: #cbd5e1 !important;
}

.dark [style*="color:#64748B"] {
    color: #cbd5e1 !important;
}

/* Table */
.dark .table-wrapper table thead {
    background: #0f172a !important;
}

.dark .table-wrapper table thead th {
    color: #cbd5e1 !important;
    border-color: #334155 !important;
}

.dark .table-wrapper table tbody tr {
    border-color: #334155 !important;
}

.dark .table-wrapper table tbody tr:hover {
    background: rgba(51, 65, 85, 0.3) !important;
}

.dark .table-wrapper table tbody td {
    border-color: #334155 !important;
}

/* Text colors */
.dark [style*="color:#1E293B"] {
    color: #f1f5f9 !important;
}

.dark [style*="color:#94A3B8"] {
    color: #cbd5e1 !important;
}

/* Badges */
.dark [style*="background:#D1FAE5"],
.dark [style*="background:#D1FAE5;color"] {
    background: rgba(16, 185, 129, 0.2) !important;
    color: #86efac !important;
}

.dark [style*="background:#FEF3C7"],
.dark [style*="background:#FEF3C7;color"] {
    background: rgba(217, 119, 6, 0.2) !important;
    color: #fcd34d !important;
}

.dark [style*="background:#FEE2E2"],
.dark [style*="background:#FEE2E2;color"] {
    background: rgba(220, 38, 38, 0.2) !important;
    color: #fca5a5 !important;
}

/* Action links */
.dark [style*="color:#059669"] {
    color: #86efac !important;
}

.dark [style*="color:#DC2626"] {
    color: #fca5a5 !important;
}

/* Pagination */
.dark [style*="border-top:1px solid #E2E8F0"] {
    border-top-color: #334155 !important;
}

/* ===== LIGHT MODE FALLBACK ===== */

.light .page-title {
    color: #0f172a !important;
}

.light .page-subtitle {
    color: #64748b !important;
}

.light [style*="background:#fff"] {
    background: #ffffff !important;
}

.light .card {
    background: #ffffff !important;
    border-color: #e2e8f0 !important;
}

.light .form-control {
    background: #ffffff !important;
    color: #1e293b !important;
}
</style>
@endpush

@section('content')
<div>
<div style="display:flex;align-items:center;justify-content:space-between;flex-wrap:wrap;gap:1rem;margin-bottom:2rem">
    <div>
        <h1 class="page-title">Semua Riwayat Analisis</h1>
        <p class="page-subtitle">Total {{ $stats['total'] }} analisis dari semua user</p>
    </div>
</div>

{{-- Stats --}}
<div style="display:grid;grid-template-columns:repeat(4,1fr);gap:1rem;margin-bottom:1.5rem">
    @foreach([['Total',$stats['total'],'#DBEAFE','#1A56DB'],['Zona Aman',$stats['safe'],'#D1FAE5','#059669'],['Zona Waspada',$stats['grey'],'#FEF3C7','#D97706'],['Zona Berbahaya',$stats['distress'],'#FEE2E2','#DC2626']] as [$l,$v,$bg,$c])
    <div style="background:#fff;border:1px solid #E2E8F0;border-radius:12px;padding:1rem;display:flex;align-items:center;gap:10px">
        <div style="width:36px;height:36px;border-radius:9px;background:{{ $bg }};display:flex;align-items:center;justify-content:center;font-size:1rem;font-weight:800;color:{{ $c }}">{{ $v }}</div>
        <div style="font-size:.8125rem;color:#64748B">{{ $l }}</div>
    </div>
    @endforeach
</div>

{{-- Filter --}}
<div class="card" style="margin-bottom:1.25rem;padding:1rem 1.25rem">
    <form method="GET" style="display:flex;gap:.75rem;flex-wrap:wrap;align-items:flex-end">
        <div style="flex:1;min-width:180px">
            <label style="font-size:.8rem;font-weight:600;color:#64748B;display:block;margin-bottom:4px">Cari Perusahaan</label>
            <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Nama perusahaan..." style="height:38px">
        </div>
        <div>
            <label style="font-size:.8rem;font-weight:600;color:#64748B;display:block;margin-bottom:4px">Kategori</label>
            <select name="category" class="form-control" style="height:38px;width:150px">
                <option value="">Semua</option>
                <option value="safe" {{ request('category')==='safe'?'selected':'' }}>Zona Aman</option>
                <option value="grey" {{ request('category')==='grey'?'selected':'' }}>Zona Waspada</option>
                <option value="distress" {{ request('category')==='distress'?'selected':'' }}>Zona Berbahaya</option>
            </select>
        </div>
        <button type="submit" class="btn btn-primary btn-sm" style="height:38px">Filter</button>
        <a href="{{ route('admin.analyses.index') }}" class="btn btn-secondary btn-sm" style="height:38px">Reset</a>
    </form>
</div>

{{-- Table --}}
<div class="card" style="padding:0">
    <div class="table-wrapper">
        <table class="table">
            <thead>
                <tr>
                    <th>Perusahaan</th>
                    <th>User</th>
                    <th>Periode</th>
                    <th style="text-align:right">Z-Score</th>
                    <th style="text-align:center">Kategori</th>
                    <th>Tanggal</th>
                    <th style="text-align:center">Aksi</th>
                </tr>
            </thead>
            <tbody>
                @forelse($analyses as $a)
                @php $bc=match($a->z_category){'safe'=>['#D1FAE5','#065F46','Aman'],'grey'=>['#FEF3C7','#92400E','Waspada'],'distress'=>['#FEE2E2','#991B1B','Berbahaya'],default=>['#F1F5F9','#64748B','-']}; @endphp
                <tr>
                    <td>
                        <div style="font-weight:600;color:#1E293B">{{ Str::limit($a->company_name,20) }}</div>
                        <div style="font-size:.75rem;color:#94A3B8">{{ $a->business_field }}</div>
                    </td>
                    <td>
                        <div style="font-size:.8125rem;color:#1E293B;font-weight:500">{{ $a->user?->name ?? '—' }}</div>
                        <div style="font-size:.7rem;color:#94A3B8">{{ $a->user?->email }}</div>
                    </td>
                    <td style="color:#64748B;font-size:.875rem">{{ $a->period }}</td>
                    <td style="text-align:right;font-weight:700;font-family:monospace;color:{{ $a->z_category==='safe'?'#059669':($a->z_category==='grey'?'#D97706':'#DC2626') }}">
                        {{ number_format($a->z_score,4) }}
                    </td>
                    <td style="text-align:center">
                        <span style="background:{{ $bc[0] }};color:{{ $bc[1] }};padding:3px 10px;border-radius:20px;font-size:.75rem;font-weight:700">{{ $bc[2] }}</span>
                    </td>
                    <td style="font-size:.8125rem;color:#64748B">{{ $a->created_at->format('d/m/Y H:i') }}</td>
                    <td style="text-align:center">
                        <div style="display:flex;align-items:center;justify-content:center;gap:8px">
                            <a href="{{ route('report.preview',$a->id) }}" target="_blank" style="color:#059669" title="Preview">
                                <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/></svg>
                            </a>
                            <form action="{{ route('admin.analyses.destroy',$a->id) }}" method="POST" onsubmit="return confirm('Hapus analisis ini?')">
                                @csrf @method('DELETE')
                                <button type="submit" style="background:none;border:none;cursor:pointer;color:#DC2626">
                                    <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/></svg>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr><td colspan="7" style="text-align:center;padding:3rem;color:#94A3B8">Tidak ada data analisis</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($analyses->hasPages())
    <div style="padding:1rem 1.5rem;border-top:1px solid #E2E8F0">{{ $analyses->links() }}</div>
    @endif
</div>
</div>
@endsection