<!DOCTYPE html>
<html lang="id">

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Data Barang Permintaan - {{ config('app.name', 'Laravel') }}</title>
    <link rel="icon" type="image/svg+xml" href="{{ asset('favicon.svg') }}">

    <script src="https://cdn.tailwindcss.com"></script>
    <script src="https://unpkg.com/lucide@latest"></script>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap" rel="stylesheet">

    <style>
        :root {
            --bg: #eef4ff;
            --panel: rgba(255, 255, 255, 0.9);
            --text: #172554;
            --muted: #64748b;
            --line: #dbe6f7;
            --primary: #4f46e5;
            --secondary: #2563eb;
            --cyan: #38bdf8;
            --shadow-soft: 0 28px 55px rgba(59, 84, 145, 0.10);
            --shadow-card: 0 18px 32px rgba(148, 163, 184, 0.12);
        }

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
            font-family: 'Inter', system-ui, -apple-system, sans-serif;
            color: var(--text);
            background:
                radial-gradient(circle at top left, rgba(79, 70, 229, 0.08), transparent 20%),
                radial-gradient(circle at top right, rgba(56, 189, 248, 0.08), transparent 22%),
                linear-gradient(180deg, #f8fbff 0%, var(--bg) 100%);
            -webkit-font-smoothing: antialiased;
        }

        .app {
            display: flex;
            min-height: 100vh;
        }

        .content {
            flex: 1;
            width: 100%;
            padding: 28px;
        }

        .shell {
            max-width: 1480px;
            margin: 0 auto;
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .surface {
            background: var(--panel);
            border: 1px solid rgba(255, 255, 255, 0.82);
            box-shadow: var(--shadow-soft);
            backdrop-filter: blur(16px);
        }

        .hero-card {
            position: relative;
            overflow: hidden;
            border-radius: 34px;
            padding: 26px;
            background:
                radial-gradient(circle at top right, rgba(99, 102, 241, 0.16), transparent 24%),
                radial-gradient(circle at bottom left, rgba(56, 189, 248, 0.13), transparent 22%),
                linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(243, 247, 255, 0.96) 100%);
        }

        .hero-card::after {
            content: "";
            position: absolute;
            top: -86px;
            right: -74px;
            width: 270px;
            height: 270px;
            border-radius: 999px;
            background: linear-gradient(135deg, rgba(79, 70, 229, 0.16), rgba(56, 189, 248, 0.14));
        }

        .pill {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            padding: 8px 14px;
            border-radius: 999px;
            background: linear-gradient(135deg, rgba(79, 70, 229, 0.10), rgba(37, 99, 235, 0.08));
            color: var(--primary);
            font-size: 11px;
            font-weight: 900;
            letter-spacing: 0.14em;
            text-transform: uppercase;
            border: 1px solid rgba(99, 102, 241, 0.10);
        }

        .hero-title {
            margin: 16px 0 8px;
            font-size: clamp(34px, 5vw, 58px);
            line-height: 0.92;
            letter-spacing: -0.06em;
            font-weight: 900;
        }

        .hero-text {
            margin: 0;
            color: var(--muted);
            line-height: 1.8;
            font-size: 14px;
            max-width: 760px;
        }

        .hero-actions {
            display: flex;
            flex-wrap: wrap;
            gap: 12px;
        }

        .action-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            min-height: 46px;
            padding: 0 18px;
            border-radius: 18px;
            text-decoration: none;
            font-size: 14px;
            font-weight: 900;
            transition: all 0.2s ease;
        }

        .action-btn.primary {
            color: #fff;
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            box-shadow: 0 18px 28px rgba(79, 70, 229, 0.24);
        }

        .action-btn.soft {
            color: #233876;
            background: rgba(255, 255, 255, 0.9);
            border: 1px solid rgba(214, 226, 247, 0.98);
        }

        .stat-grid {
            display: grid;
            grid-template-columns: repeat(4, minmax(0, 1fr));
            gap: 16px;
        }

        .stat-card {
            position: relative;
            overflow: hidden;
            min-height: 136px;
            padding: 24px 22px;
            border-radius: 26px;
            background: rgba(255, 255, 255, 0.88);
            border: 1px solid rgba(214, 226, 247, 0.98);
            box-shadow: var(--shadow-card);
        }

        .stat-card::after {
            content: "";
            position: absolute;
            top: 0;
            right: 0;
            width: 66px;
            height: 66px;
            border-bottom-left-radius: 32px;
            opacity: 0.72;
        }

        .stat-card.indigo::after {
            background: #ede9fe;
        }

        .stat-card.emerald::after {
            background: #dcfce7;
        }

        .stat-card.slate::after {
            background: #e2e8f0;
        }

        .stat-card.rose::after {
            background: #ffe4e6;
        }

        .stat-top {
            display: flex;
            align-items: flex-start;
            justify-content: space-between;
            gap: 12px;
        }

        .stat-label {
            color: var(--muted);
            font-size: 11px;
            font-weight: 900;
            letter-spacing: 0.12em;
            text-transform: uppercase;
        }

        .stat-value {
            margin-top: 12px;
            font-size: 34px;
            font-weight: 900;
            line-height: 1;
        }

        .stat-card.indigo .stat-value {
            color: #4f46e5;
        }

        .stat-card.emerald .stat-value {
            color: #16a34a;
        }

        .stat-card.slate .stat-value {
            color: #475569;
        }

        .stat-card.rose .stat-value {
            color: #e11d48;
        }

        .stat-sub {
            margin-top: 12px;
            color: var(--muted);
            font-size: 13px;
            line-height: 1.5;
        }

        .notice {
            display: flex;
            align-items: flex-start;
            gap: 12px;
            padding: 16px 18px;
            border-radius: 24px;
            font-size: 14px;
            line-height: 1.6;
            font-weight: 700;
        }

        .notice.success {
            color: #047857;
            background: rgba(236, 253, 245, 0.94);
            border: 1px solid rgba(167, 243, 208, 0.9);
        }

        .notice.danger {
            color: #be123c;
            background: rgba(255, 241, 242, 0.94);
            border: 1px solid rgba(254, 205, 211, 0.9);
        }

        .inventory-panel {
            overflow: hidden;
            border-radius: 34px;
            background:
                radial-gradient(circle at top right, rgba(99, 102, 241, 0.12), transparent 22%),
                linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(243, 247, 255, 0.96) 100%);
        }

        .panel-head {
            padding: 22px 24px;
            display: flex;
            justify-content: space-between;
            gap: 18px;
            align-items: center;
            border-bottom: 1px solid rgba(219, 230, 247, 0.95);
        }

        .panel-title {
            margin: 0;
            color: #172554;
            font-size: 21px;
            font-weight: 900;
            letter-spacing: -0.04em;
        }

        .panel-subtitle {
            margin: 6px 0 0;
            color: var(--muted);
            font-size: 13px;
            line-height: 1.6;
        }

        .search-box {
            position: relative;
            width: min(360px, 100%);
        }

        .search-box i {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            color: #94a3b8;
        }

        .search-input {
            width: 100%;
            min-height: 46px;
            border-radius: 17px;
            border: 1px solid rgba(214, 226, 247, 0.98);
            background: rgba(255, 255, 255, 0.92);
            color: #233876;
            padding: 0 14px 0 44px;
            font-size: 13px;
            font-weight: 800;
            outline: none;
            box-shadow: 0 10px 20px rgba(148, 163, 184, 0.10);
        }

        .card-grid {
            padding: 22px;
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 18px;
        }

        .item-card {
            position: relative;
            overflow: hidden;
            border-radius: 30px;
            background:
                radial-gradient(circle at top right, rgba(79, 70, 229, 0.08), transparent 28%),
                linear-gradient(180deg, rgba(248, 251, 255, 0.96), rgba(255, 255, 255, 0.98));
            border: 1px solid rgba(219, 230, 247, 0.95);
            box-shadow: 0 14px 28px rgba(148, 163, 184, 0.12);
            transition: all 0.2s ease;
        }

        .item-card:hover {
            transform: translateY(-3px);
            border-color: rgba(79, 70, 229, 0.28);
            box-shadow: 0 24px 38px rgba(59, 84, 145, 0.15);
        }

        .item-card.low-stock {
            background:
                radial-gradient(circle at top right, rgba(244, 63, 94, 0.12), transparent 30%),
                linear-gradient(180deg, rgba(255, 241, 242, 0.94), rgba(255, 255, 255, 0.98));
            border-color: rgba(254, 205, 211, 0.95);
        }

        .card-image {
            position: relative;
            height: 220px;
            background:
                radial-gradient(circle at top left, rgba(79, 70, 229, 0.12), transparent 32%),
                linear-gradient(180deg, #f8fbff, #eef4ff);
            overflow: hidden;
        }

        .card-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            display: block;
            transition: transform 0.3s ease;
        }

        .item-card:hover .card-image img {
            transform: scale(1.04);
        }

        .card-status {
            position: absolute;
            top: 14px;
            right: 14px;
            box-shadow: 0 14px 24px rgba(15, 23, 42, 0.14);
        }

        .card-unit {
            position: absolute;
            left: 14px;
            bottom: 14px;
            box-shadow: 0 14px 24px rgba(15, 23, 42, 0.12);
        }

        .card-body {
            padding: 18px;
        }

        .item-name {
            color: #0f172a;
            font-size: 15px;
            font-weight: 900;
            line-height: 1.25;
        }

        .item-desc {
            margin-top: 5px;
            color: #64748b;
            font-size: 13px;
            line-height: 1.6;
            min-height: 42px;
        }

        .chip {
            display: inline-flex;
            align-items: center;
            gap: 7px;
            padding: 8px 11px;
            border-radius: 999px;
            font-size: 11px;
            font-weight: 900;
            letter-spacing: 0.04em;
            white-space: nowrap;
            border: 1px solid transparent;
        }

        .chip.blue {
            color: #1d4ed8;
            background: #eff6ff;
            border-color: #bfdbfe;
        }

        .chip.green {
            color: #047857;
            background: #ecfdf5;
            border-color: #a7f3d0;
        }

        .chip.rose {
            color: #be123c;
            background: #fff1f2;
            border-color: #fecdd3;
        }

        .chip.slate {
            color: #475569;
            background: #f8fafc;
            border-color: #e2e8f0;
        }

        .stock-grid {
            margin-top: 16px;
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 10px;
        }

        .stock-box {
            border-radius: 20px;
            padding: 13px 14px;
            background: rgba(238, 242, 255, 0.72);
            border: 1px solid rgba(219, 230, 247, 0.95);
        }

        .stock-label {
            color: #64748b;
            font-size: 10px;
            font-weight: 900;
            letter-spacing: 0.1em;
            text-transform: uppercase;
        }

        .stock-value {
            margin-top: 6px;
            color: #172554;
            font-size: 22px;
            font-weight: 900;
            line-height: 1;
        }

        .icon-btn {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 7px;
            min-height: 42px;
            padding: 0 13px;
            border-radius: 15px;
            font-size: 13px;
            font-weight: 900;
            transition: all 0.2s ease;
        }

        .card-actions {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            margin-top: 16px;
        }

        .empty-state {
            min-height: 260px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            gap: 12px;
            color: #64748b;
            text-align: center;
            padding: 36px;
        }

        .empty-icon {
            width: 74px;
            height: 74px;
            border-radius: 26px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #4f46e5;
            background: #eef2ff;
            border: 1px solid #dbeafe;
        }

        .pagination-wrap {
            padding: 0 22px 22px;
        }

        @media (max-width: 1180px) {
            .stat-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }

            .card-grid {
                grid-template-columns: repeat(2, minmax(0, 1fr));
            }

            .panel-head {
                flex-direction: column;
                align-items: stretch;
            }

            .search-box {
                width: 100%;
            }
        }

        @media (max-width: 1024px) {
            .app {
                display: block;
            }

            .content {
                padding: 20px;
                padding-top: 84px;
            }
        }

        @media (max-width: 680px) {
            .content {
                padding: 16px;
                padding-top: 84px;
            }

            .hero-card,
            .inventory-panel {
                border-radius: 24px;
            }

            .stat-grid {
                grid-template-columns: 1fr;
            }

            .card-grid {
                grid-template-columns: 1fr;
                padding: 16px;
            }

            .card-image {
                height: 190px;
            }

            .hero-actions {
                width: 100%;
            }

            .action-btn {
                width: 100%;
            }

            .stock-grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>

<body>
    <div class="app">
        @include('partials.sidebar')

        <main class="content">
            <div class="shell">
                <section class="hero-card surface">
                    <div class="relative z-10 flex flex-col xl:flex-row xl:items-center xl:justify-between gap-7">
                        <div>
                            <span class="pill">
                                <i data-lucide="boxes" class="w-4 h-4"></i>
                                Admin Menu
                            </span>
                            <h1 class="hero-title">Barang Permintaan</h1>
                            <p class="hero-text">Kelola master barang habis pakai yang bisa diajukan user, lengkap dengan stok, satuan, status aktif, foto, dan batas minimum agar pengadaan lebih terkendali.</p>
                        </div>

                        <div class="hero-actions">
                            <a href="{{ route('admin.request-items.create') }}" class="action-btn primary">
                                <i data-lucide="plus" class="w-4 h-4"></i>
                                Tambah Barang
                            </a>
                            <span class="action-btn soft">
                                <i data-lucide="shield-check" class="w-4 h-4"></i>
                                Khusus Pihak Pengelola Barang
                            </span>
                        </div>
                    </div>
                </section>

                @if(session('success'))
                <div class="notice success surface">
                    <i data-lucide="check-circle-2" class="w-5 h-5 flex-shrink-0 mt-0.5"></i>
                    <span>{{ session('success') }}</span>
                </div>
                @endif

                @if($lowStockCount > 0)
                <div class="notice danger surface">
                    <i data-lucide="triangle-alert" class="w-5 h-5 flex-shrink-0 mt-0.5"></i>
                    <span>Ada {{ $lowStockCount }} barang yang menyentuh batas stok minimum. Barang tersebut ditandai merah agar lebih cepat ditindaklanjuti.</span>
                </div>
                @endif

                <section class="stat-grid">
                    <div class="stat-card indigo">
                        <div class="stat-top">
                            <div class="stat-label">Total Barang</div>
                            <i data-lucide="boxes" class="w-5 h-5 text-indigo-600 relative z-10"></i>
                        </div>
                        <div class="stat-value">{{ $totalItemCount }}</div>
                        <div class="stat-sub">Semua master barang permintaan.</div>
                    </div>

                    <div class="stat-card emerald">
                        <div class="stat-top">
                            <div class="stat-label">Barang Aktif</div>
                            <i data-lucide="badge-check" class="w-5 h-5 text-emerald-600 relative z-10"></i>
                        </div>
                        <div class="stat-value">{{ $activeItemCount }}</div>
                        <div class="stat-sub">Bisa diajukan oleh user.</div>
                    </div>

                    <div class="stat-card slate">
                        <div class="stat-top">
                            <div class="stat-label">Nonaktif</div>
                            <i data-lucide="archive-x" class="w-5 h-5 text-slate-600 relative z-10"></i>
                        </div>
                        <div class="stat-value">{{ $inactiveItemCount }}</div>
                        <div class="stat-sub">Disembunyikan sementara.</div>
                    </div>

                    <div class="stat-card rose">
                        <div class="stat-top">
                            <div class="stat-label">Stok Minimum</div>
                            <i data-lucide="siren" class="w-5 h-5 text-rose-600 relative z-10"></i>
                        </div>
                        <div class="stat-value">{{ $lowStockCount }}</div>
                        <div class="stat-sub">Butuh perhatian admin.</div>
                    </div>
                </section>

                <section class="inventory-panel surface">
                    <div class="panel-head">
                        <div>
                            <h2 class="panel-title">Daftar Barang</h2>
                            <p class="panel-subtitle">Kelola stok, batas minimum, status aktif, dan detail barang yang muncul di form permintaan barang.</p>
                        </div>

                        <div class="flex flex-col sm:flex-row gap-3 w-full lg:w-auto">
                            <div class="search-box">
                                <i data-lucide="search" class="w-4 h-4"></i>
                                <input id="searchInput" type="text" placeholder="Cari barang atau deskripsi..." class="search-input">
                            </div>
                        </div>
                    </div>

                    <div id="itemCards" class="card-grid">
                        @forelse($items as $item)
                        <article class="item-card {{ $item->is_low_stock ? 'low-stock' : '' }}" data-name="{{ strtolower($item->display_name . ' ' . ($item->description ?? '')) }}">
                            <div class="card-image">
                                <img src="{{ $item->image_url ?: 'https://via.placeholder.com/120x120?text=No+Image' }}"
                                    alt="{{ $item->display_name }}"
                                    onerror="this.onerror=null;this.src='https://via.placeholder.com/120x120?text=No+Image';">

                                <span class="chip slate card-unit">{{ $item->unit }}</span>

                                @if($item->is_low_stock)
                                <span class="chip rose card-status"><span class="w-2 h-2 rounded-full bg-rose-500"></span> Stok Minimum</span>
                                @elseif($item->is_active)
                                <span class="chip green card-status"><span class="w-2 h-2 rounded-full bg-emerald-500"></span> Aktif</span>
                                @else
                                <span class="chip slate card-status"><span class="w-2 h-2 rounded-full bg-slate-500"></span> Nonaktif</span>
                                @endif
                            </div>

                            <div class="card-body">
                                <h3 class="item-name {{ $item->is_low_stock ? 'text-rose-800' : '' }}">{{ $item->display_name }}</h3>
                                <p class="item-desc {{ $item->is_low_stock ? 'text-rose-600' : '' }}">{{ $item->description ?: 'Belum ada deskripsi barang.' }}</p>

                                <div class="stock-grid">
                                    <div class="stock-box">
                                        <div class="stock-label">Stok Saat Ini</div>
                                        <div class="stock-value {{ $item->is_low_stock ? 'text-rose-700' : '' }}">
                                            {{ rtrim(rtrim(number_format($item->stock, 2, '.', ''), '0'), '.') }}
                                        </div>
                                    </div>
                                    <div class="stock-box">
                                        <div class="stock-label">Batas Minimum</div>
                                        <div class="stock-value text-slate-700">
                                            {{ rtrim(rtrim(number_format($item->min_stock, 2, '.', ''), '0'), '.') }}
                                        </div>
                                    </div>
                                </div>

                                <div class="card-actions">
                                    <a href="{{ route('admin.request-items.edit', $item->id) }}" class="icon-btn bg-amber-50 text-amber-700 border border-amber-200 hover:bg-amber-100">
                                        <i data-lucide="pencil" class="w-4 h-4"></i>
                                        Edit
                                    </a>
                                    <form action="{{ route('admin.request-items.destroy', $item->id) }}" method="POST" onsubmit="return confirm('Yakin ingin menghapus barang ini?')">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit" class="icon-btn w-full bg-rose-50 text-rose-700 border border-rose-200 hover:bg-rose-100">
                                            <i data-lucide="trash-2" class="w-4 h-4"></i>
                                            Hapus
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </article>
                        @empty
                        <div class="empty-state">
                            <div class="empty-icon">
                                <i data-lucide="inbox" class="w-8 h-8"></i>
                            </div>
                            <div class="font-black text-slate-800">Belum ada barang</div>
                            <div class="text-sm text-slate-500">Tambahkan barang baru agar user bisa membuat permintaan.</div>
                        </div>
                        @endforelse
                    </div>

                    <div class="pagination-wrap">
                        {{ $items->links() }}
                    </div>
                </section>
            </div>
        </main>
    </div>

    <script>
        lucide.createIcons();

        const searchInput = document.getElementById('searchInput');

        if (searchInput) {
            searchInput.addEventListener('input', function() {
                const keyword = this.value.toLowerCase();

                document.querySelectorAll('.item-card').forEach(card => {
                    const name = card.dataset.name || '';
                    card.style.display = name.includes(keyword) ? '' : 'none';
                });
            });
        }
    </script>
</body>

</html>