@extends('layouts.app')

@push('head')
<style>
    .report-card {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 28px;
        padding: 34px 36px 32px;
        border-radius: 28px;
        background: linear-gradient(150deg, rgba(14, 21, 44, 0.96), rgba(10, 15, 32, 0.9));
        border: 1px solid rgba(148, 163, 184, 0.2);
        box-shadow: 0 28px 60px rgba(3, 7, 18, 0.6);
        overflow: hidden;
    }
    .report-card::before,
    .report-card::after {
        content: "";
        position: absolute;
        inset: 0;
        pointer-events: none;
    }
    .report-card::before {
        background:
            radial-gradient(circle at 12% 16%, rgba(99, 102, 241, 0.32), transparent 55%),
            radial-gradient(circle at 86% -6%, rgba(56, 189, 248, 0.28), transparent 60%);
        opacity: 0.9;
    }
    .report-card::after {
        background: linear-gradient(135deg, transparent 40%, rgba(59, 130, 246, 0.14));
        opacity: 0.8;
    }
    .report-card > * { position: relative; z-index: 1; }

    .report-hero {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 24px;
        flex-wrap: wrap;
    }
    .report-hero__title {
        display: flex;
        flex-direction: column;
        gap: 12px;
        max-width: 560px;
    }
    .report-hero__tagline {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        padding: 6px 14px;
        border-radius: 999px;
        background: rgba(129, 140, 248, 0.18);
        border: 1px solid rgba(129, 140, 248, 0.38);
        color: #e0e7ff;
        font-size: 12px;
        letter-spacing: 0.1em;
        text-transform: uppercase;
        font-weight: 600;
    }
    .report-hero h2 {
        margin: 0;
        font-size: 30px;
        font-weight: 700;
        letter-spacing: -0.02em;
        color: #f8fafc;
    }
    .report-hero p {
        margin: 0;
        color: rgba(226, 232, 240, 0.76);
        font-size: 15px;
        line-height: 1.6;
    }
    .report-hero__actions {
        display: flex;
        flex-wrap: wrap;
        gap: 12px;
    }
    .report-hero__button {
        display: inline-flex;
        align-items: center;
        gap: 10px;
        padding: 10px 20px;
        border-radius: 14px;
        background: linear-gradient(135deg, rgba(14, 165, 233, 0.28), rgba(99, 102, 241, 0.42));
        border: 1px solid rgba(129, 140, 248, 0.52);
        color: #e0f2fe;
        font-weight: 600;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        text-decoration: none;
        box-shadow: 0 22px 32px rgba(6, 15, 38, 0.5);
        transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease;
    }
    .report-hero__button:hover {
        transform: translateY(-2px);
        border-color: rgba(165, 180, 252, 0.76);
        background: linear-gradient(135deg, rgba(56, 189, 248, 0.35), rgba(99, 102, 241, 0.48));
    }

    .report-search {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 12px 18px;
        border-radius: 18px;
        border: 1px solid rgba(148, 163, 184, 0.24);
        background: rgba(8, 16, 36, 0.82);
        box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.12);
    }
    .report-search svg {
        width: 18px;
        height: 18px;
        color: rgba(148, 163, 184, 0.75);
    }
    .report-search input {
        flex: 1 1 auto;
        background: transparent;
        border: none;
        color: #f1f5f9;
        font-size: 13px;
        outline: none;
    }
    .report-search input::placeholder {
        color: rgba(148, 163, 184, 0.7);
    }
    .report-search button {
        padding: 8px 18px;
        border-radius: 12px;
        background: linear-gradient(135deg, #38bdf8, #2563eb);
        border: none;
        color: #f8fafc;
        font-weight: 600;
        letter-spacing: 0.04em;
        cursor: pointer;
        box-shadow: 0 14px 28px rgba(37, 99, 235, 0.28);
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .report-search button:hover {
        transform: translateY(-1px);
        box-shadow: 0 18px 32px rgba(37, 99, 235, 0.32);
    }

    .report-summary {
        display: flex;
        flex-direction: column;
        gap: 16px;
    }
    .report-summary__subtitle {
        margin: 0;
        font-size: 13px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: rgba(148, 163, 184, 0.72);
    }
    .report-summary__grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
        gap: 16px;
    }
    .summary-card {
        position: relative;
        display: flex;
        flex-direction: column;
        gap: 8px;
        padding: 18px 20px;
        border-radius: 20px;
        background: linear-gradient(145deg, rgba(12, 21, 44, 0.9), rgba(19, 35, 70, 0.82));
        border: 1px solid rgba(148, 163, 184, 0.24);
        box-shadow: 0 18px 36px rgba(5, 12, 28, 0.5);
        overflow: hidden;
    }
    .summary-card::after {
        content: "";
        position: absolute;
        inset: 0;
        background: radial-gradient(circle at 80% 18%, rgba(59, 130, 246, 0.24), transparent 50%);
        opacity: 0.7;
        pointer-events: none;
    }
    .summary-card__label {
        font-size: 12px;
        letter-spacing: 0.12em;
        text-transform: uppercase;
        color: rgba(203, 213, 225, 0.78);
        font-weight: 600;
    }
    .summary-card__value {
        font-size: 30px;
        font-weight: 700;
        color: #f8fafc;
        letter-spacing: -0.02em;
    }
    .summary-card__meta {
        font-size: 12px;
        color: rgba(226, 232, 240, 0.68);
    }

    .report-alert {
        padding: 12px 14px;
        border-radius: 12px;
        font-size: 13px;
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        gap: 12px;
    }
    .report-alert__message { margin: 0; }

    .report-table-shell {
        border-radius: 24px;
        border: 1px solid rgba(148, 163, 184, 0.2);
        background: rgba(8, 16, 34, 0.84);
        box-shadow: 0 24px 44px rgba(4, 10, 26, 0.55);
        overflow: hidden;
    }
    .report-table-scroll {
        width: 100%;
        overflow-x: auto;
    }
    .report-table {
        width: 100%;
        min-width: 980px;
        border-collapse: separate;
        border-spacing: 0;
    }
    .report-table thead th {
        position: sticky;
        top: 0;
        background: rgba(10, 19, 40, 0.96);
        color: #cbd5f5;
        padding: 14px 20px;
        font-weight: 600;
        font-size: 13px;
        letter-spacing: 0.04em;
        text-transform: uppercase;
        border-bottom: 1px solid rgba(148, 163, 184, 0.22);
    }
    .report-table tbody td {
        padding: 16px 20px;
        color: #e2e8f0;
        font-size: 13px;
        border-bottom: 1px solid rgba(148, 163, 184, 0.1);
        vertical-align: top;
    }
    .report-table tbody tr:nth-child(even) {
        background: rgba(10, 23, 55, 0.32);
    }
    .report-table tbody tr:hover {
        background: rgba(17, 29, 62, 0.52);
    }
    .report-table td[data-align="center"],
    .report-table th[data-align="center"] {
        text-align: center;
    }

    .report-text {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    .report-tags {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
    }
    .report-tag {
        padding: 4px 10px;
        border-radius: 999px;
        background: rgba(59, 130, 246, 0.18);
        border: 1px solid rgba(59, 130, 246, 0.3);
        color: #bfdbfe;
        font-size: 12px;
        letter-spacing: 0.02em;
    }

    .report-status {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 6px 14px;
        border-radius: 999px;
        font-weight: 600;
        font-size: 12px;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        border: 1px solid transparent;
        white-space: nowrap;
    }
    .report-status.dilaporkan {
        background: rgba(59, 130, 246, 0.22);
        color: #bfdbfe;
        border-color: rgba(59, 130, 246, 0.38);
    }
    .report-status.diproses {
        background: rgba(245, 158, 11, 0.2);
        color: #fcd34d;
        border-color: rgba(245, 158, 11, 0.38);
    }
    .report-status.selesai {
        background: rgba(34, 197, 94, 0.2);
        color: #bbf7d0;
        border-color: rgba(34, 197, 94, 0.36);
    }
    .report-status.dibatalkan {
        background: rgba(248, 113, 113, 0.2);
        color: #fecaca;
        border-color: rgba(248, 113, 113, 0.36);
    }

    .report-admin-form {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 14px;
        padding: 16px;
        border-radius: 16px;
        background: rgba(6, 14, 32, 0.78);
        border: 1px solid rgba(148, 163, 184, 0.24);
        box-shadow: inset 0 1px 0 rgba(148, 163, 184, 0.12);
    }
    .report-admin-form label {
        font-size: 12px;
        font-weight: 600;
        color: #cbd5f5;
        letter-spacing: 0.02em;
    }
    .report-admin-form select,
    .report-admin-form textarea {
        width: 100%;
        border-radius: 14px;
        border: 1px solid rgba(148, 163, 184, 0.3);
        background: rgba(7, 15, 34, 0.92);
        color: #f1f5f9;
        padding: 10px 12px;
        font-size: 13px;
        transition: border-color 0.2s ease, box-shadow 0.2s ease;
    }
    .report-admin-form textarea {
        min-height: 90px;
        resize: vertical;
        line-height: 1.5;
        grid-column: 1 / -1;
    }
    .report-admin-form select:focus,
    .report-admin-form textarea:focus {
        outline: none;
        border-color: rgba(99, 102, 241, 0.62);
        box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
    }
    .report-admin-form button {
        justify-self: flex-end;
        padding: 10px 22px;
        border-radius: 12px;
        border: none;
        background: linear-gradient(135deg, #0ea5e9, #2563eb);
        color: #f8fafc;
        font-weight: 700;
        letter-spacing: 0.05em;
        text-transform: uppercase;
        cursor: pointer;
        box-shadow: 0 18px 32px rgba(14, 165, 233, 0.32);
        transition: transform 0.18s ease, box-shadow 0.18s ease;
    }
    .report-admin-form button:hover {
        transform: translateY(-1px);
        box-shadow: 0 22px 36px rgba(14, 165, 233, 0.36);
    }

    .report-action {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 16px 20px;
    }
    .report-action form {
        margin: 0;
    }
    .report-action button {
        border-radius: 12px;
        border: none;
        background: rgba(248, 113, 113, 0.16);
        color: #fecaca;
        padding: 10px 18px;
        font-weight: 700;
        cursor: pointer;
        transition: background 0.2s ease, transform 0.2s ease;
    }
    .report-action button:hover {
        transform: translateY(-1px);
        background: rgba(248, 113, 113, 0.28);
    }

    .report-empty {
        padding: 24px;
        text-align: center;
        color: rgba(148, 163, 184, 0.72);
        font-size: 14px;
    }

    .report-footer {
        display: flex;
        flex-wrap: wrap;
        gap: 16px;
        align-items: center;
        justify-content: space-between;
    }
    .report-footer__info {
        font-size: 12px;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: rgba(148, 163, 184, 0.72);
    }
    .report-footer nav > * {
        display: inline-flex;
        align-items: center;
        padding: 6px 12px;
        border-radius: 10px;
        background: rgba(15, 23, 42, 0.78);
        color: #cbd5f5;
        border: 1px solid rgba(148, 163, 184, 0.2);
        box-shadow: 0 12px 24px rgba(7, 16, 36, 0.35);
    }

    @media (max-width: 1280px) {
        .report-card {
            padding: 30px 28px;
        }
        .report-table {
            min-width: 920px;
        }
    }
    @media (max-width: 960px) {
        .report-card {
            padding: 28px 24px;
        }
        .report-hero {
            flex-direction: column;
        }
        .report-hero__actions {
            width: 100%;
            justify-content: flex-start;
        }
    }
    @media (max-width: 768px) {
        .report-card {
            padding: 24px 20px;
        }
        .report-summary__grid {
            grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
        }
        .report-table {
            min-width: 720px;
        }
        .report-admin-form button {
            width: 100%;
            justify-self: stretch;
        }
    }
    @media (max-width: 620px) {
        .report-card {
            padding: 22px 18px;
            border-radius: 22px;
        }
        .report-hero__button,
        .report-search {
            width: 100%;
        }
        .report-summary__grid {
            grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
        }
        .report-footer {
            flex-direction: column;
            align-items: flex-start;
        }
    }
</style>
@endpush

@section('content')
<div class="report-card">
    @php
        $totalLaporan = $items->total();
        $totalDilaporkan = $items->filter(fn ($row) => $row->status === 'dilaporkan')->count();
        $totalDiproses = $items->filter(fn ($row) => $row->status === 'diproses')->count();
        $totalSelesai = $items->filter(fn ($row) => $row->status === 'selesai')->count();
    @endphp

    <div class="report-hero">
        <div class="report-hero__title">
            <span class="report-hero__tagline">Monitoring Aset</span>
            <h2>Laporan Barang Rusak</h2>
            <p>Kelola perbaikan peralatan pegawai dengan ringkasan status, catatan terbaru, dan pembaruan cepat.</p>
        </div>
        <div class="report-hero__actions">
            <a href="{{ route('admin.barang_rusak.index') }}" class="report-hero__button">Segarkan Data</a>
        </div>
    </div>

    <div class="search-filter-container">
        <form method="GET" action="{{ route('admin.barang_rusak.index') }}" class="search-form">
            <div class="search-box">
                <svg class="search-icon" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m21 21-4.35-4.35m0 0A7.5 7.5 0 1 0 5.25 5.25a7.5 7.5 0 0 0 11.4 10.9Z" />
                </svg>
                <input 
                    type="text" 
                    name="q" 
                    value="{{ $q ?? '' }}" 
                    placeholder="Cari berdasarkan nama user..." 
                    class="search-input"
                >
                <button type="submit" class="search-button">
                    <span>Cari</span>
                    <svg class="search-arrow" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14 5l7 7m0 0l-7 7m7-7H3" />
                    </svg>
                </button>
            </div>
        </form>
    </div>

    <style>
        .search-filter-container {
            margin-bottom: 2rem;
            width: 100%;
        }
        
        .search-form {
            max-width: 500px;
            margin: 0;
        }
        
        .search-box {
            position: relative;
            display: flex;
            align-items: center;
            background: rgba(30, 41, 59, 0.7);
            border-radius: 8px;
            border: 1px solid rgba(99, 102, 241, 0.3);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            overflow: hidden;
            max-width: 100%;
        }
        
        .search-box:focus-within {
            border-color: #6366f1;
            box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.25);
            transform: translateY(-1px);
        }
        
        .search-icon {
            width: 18px;
            height: 18px;
            margin-left: 16px;
            color: #94a3b8;
            flex-shrink: 0;
            transition: all 0.2s ease;
        }
        
        .search-input:focus + .search-button .search-icon {
            color: #6366f1;
        }
        
        .search-input {
            flex: 1;
            background: transparent;
            border: none;
            padding: 12px 16px 12px 12px;
            color: #f1f5f9;
            font-size: 14px;
            outline: none;
            min-width: 0;
            transition: all 0.2s ease;
        }
        
        .search-input::placeholder {
            color: #64748b;
            opacity: 0.8;
        }
        
        .search-button {
            display: flex;
            align-items: center;
            gap: 6px;
            background: rgba(99, 102, 241, 0.9);
            color: white;
            border: none;
            padding: 0 16px;
            height: 46px;
            font-weight: 500;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.2s ease;
            white-space: nowrap;
        }
        
        .search-button:hover {
            background: rgba(79, 70, 229, 0.95);
        }
        
        .search-arrow {
            width: 16px;
            height: 16px;
            transition: transform 0.2s ease;
        }
        
        .search-button:hover .search-arrow {
            transform: translateX(2px);
        }
        
        /* Animation */
        @keyframes bounce {
            0%, 100% { transform: translateY(0); }
            50% { transform: translateY(-2px); }
        }
        
        .search-input:focus ~ .search-icon {
            animation: bounce 0.5s ease;
        }
        
        @media (max-width: 768px) {
            .search-form {
                width: 100%;
            }
            
            .search-box {
                border-radius: 8px;
            }
            
            .search-button span {
                display: none;
            }
            
            .search-button {
                padding: 0 12px;
            }
        }
    </style>

    <div class="report-summary">
        <p class="report-summary__subtitle">Ringkasan Hari Ini</p>
        <div class="report-summary__grid">
            <div class="summary-card">
                <span class="summary-card__label">Total Laporan</span>
                <span class="summary-card__value">{{ number_format($totalLaporan) }}</span>
                <span class="summary-card__meta">Keseluruhan permintaan perbaikan</span>
            </div>
            <div class="summary-card">
                <span class="summary-card__label">Belum Diproses</span>
                <span class="summary-card__value">{{ number_format($totalDilaporkan) }}</span>
                <span class="summary-card__meta">Menunggu tindak lanjut admin</span>
            </div>
            <div class="summary-card">
                <span class="summary-card__label">Sedang Ditangani</span>
                <span class="summary-card__value">{{ number_format($totalDiproses) }}</span>
                <span class="summary-card__meta">Proses perbaikan berjalan</span>
            </div>
            <div class="summary-card">
                <span class="summary-card__label">Selesai</span>
                <span class="summary-card__value">{{ number_format($totalSelesai) }}</span>
                <span class="summary-card__meta">Perbaikan telah dirampungkan</span>
            </div>
        </div>
    </div>

    @if (session('success'))
        <div class="report-alert" style="background: rgba(34,197,94,0.14); border: 1px solid rgba(34,197,94,0.38); color:#bbf7d0;">
            <p class="report-alert__message">{{ session('success') }}</p>
        </div>
    @endif

    @if ($errors->any())
        <div class="report-alert" style="background: rgba(248,113,113,0.14); border: 1px solid rgba(248,113,113,0.4); color:#fecaca; flex-direction: column; align-items: flex-start;">
            <p class="report-alert__message" style="font-weight:600;">Periksa kembali input Anda:</p>
            <ul style="margin:0 0 0 18px; padding:0;">
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="report-table-shell">
        <div class="report-table-scroll">
            <table class="report-table">
                <thead>
                    <tr>
                        <th>Pelapor</th>
                        <th>Tanggal</th>
                        <th>Lokasi</th>
                        <th>Kerusakan</th>
                        <th>Status</th>
                        <th>Keterangan</th>
                        <th>Catatan Admin</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($items as $row)
                        <tr>
                            <td data-label="Pelapor" class="report-text" title="{{ $row->user->name ?? 'N/A' }}">{{ $row->user->name ?? 'N/A' }}</td>
                            <td data-label="Tanggal">{{ date('d/m/Y', strtotime($row->tanggal)) }}</td>
                            <td data-label="Lokasi" class="report-text" title="{{ $row->lokasi ?? '-' }}">{{ $row->lokasi ?? '-' }}</td>
                            <td data-label="Kerusakan">
                                @if(is_array($row->kerusakan) && count($row->kerusakan) > 0)
                                    <div class="report-tags">
                                        @foreach($row->kerusakan as $k)
                                            <span class="report-tag">{{ $k }}</span>
                                        @endforeach
                                    </div>
                                @else
                                    <span style="color:#94a3b8;">-</span>
                                @endif
                            </td>
                            <td data-label="Status" data-align="center">
                                <span class="report-status {{ $row->status }}">{{ ucfirst($row->status) }}</span>
                            </td>
                            <td data-label="Keterangan" class="report-text" title="{{ $row->keterangan ?? '-' }}">{{ $row->keterangan ?? '-' }}</td>
                            <td data-label="Catatan Admin">
                                <form action="{{ route('admin.barang_rusak.update', $row) }}" method="POST" class="report-admin-form">
                                    @csrf
                                    <input type="hidden" name="barang_rusak_id" value="{{ $row->id }}">
                                    <div>
                                        <label for="status-{{ $row->id }}">Status</label>
                                        <select id="status-{{ $row->id }}" name="status">
                                            @foreach($statusOptions as $value => $label)
                                                <option value="{{ $value }}" @selected(old('status', $row->status) === $value)>{{ $label }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div>
                                        <label for="note-{{ $row->id }}">Catatan Admin</label>
                                        <textarea id="note-{{ $row->id }}" name="admin_note" placeholder="Tambahkan catatan untuk pelapor...">{{ old('barang_rusak_id') == $row->id ? old('admin_note') : ($row->admin_note ?? '') }}</textarea>
                                    </div>
                                    <button type="submit">Simpan</button>
                                </form>
                            </td>
                            <td data-label="Aksi" class="report-action">
                                <form action="{{ route('admin.barang_rusak.destroy', $row) }}" method="POST" onsubmit="return confirm('Yakin ingin menghapus laporan barang rusak ini?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit">Hapus</button>
                                </form>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="8" class="report-empty">Belum ada laporan.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="report-footer">
        <span class="report-footer__info">Terakhir diperbarui {{ now()->timezone(config('app.timezone'))->format('d M Y H:i') }}</span>
        <nav>
            {{ $items->links() }}
        </nav>
    </div>
</div>
@endsection
