<!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" />
  <title>Daftar Permintaan Peminjaman - {{ 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://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js" defer></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.1);
      --font-main: 'Inter', system-ui, -apple-system, sans-serif;
    }

    * { box-sizing: border-box; }

    body {
      margin: 0;
      font-family: var(--font-main);
      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 {
      position: relative;
      overflow: hidden;
      border-radius: 32px;
      padding: 26px;
      color: var(--text);
      background:
        radial-gradient(circle at top right, rgba(99, 102, 241, 0.14), transparent 22%),
        linear-gradient(135deg, rgba(255, 255, 255, 0.98) 0%, rgba(243, 247, 255, 0.96) 100%);
    }

    .hero::before {
      content: "";
      position: absolute;
      inset: 0;
      background:
        radial-gradient(circle at 16% 18%, rgba(79, 70, 229, 0.08), transparent 19%),
        radial-gradient(circle at 86% 14%, rgba(56, 189, 248, 0.10), transparent 22%);
    }

    .hero-inner {
      position: relative;
      display: flex;
      flex-direction: column;
      gap: 22px;
    }

    .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: 800;
      letter-spacing: 0.16em;
      text-transform: uppercase;
      border: 1px solid rgba(99, 102, 241, 0.10);
    }

    .hero-title {
      margin: 16px 0 10px;
      font-size: clamp(32px, 4vw, 48px);
      line-height: 0.98;
      letter-spacing: -0.05em;
      font-weight: 900;
    }

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

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

    .btn-primary,
    .btn-soft {
      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: 800;
      transition: all 0.2s ease;
    }

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

    .btn-primary:hover,
    .btn-soft:hover {
      transform: translateY(-1px);
    }

    .btn-soft {
      color: #dbeafe;
      background: #ffffff;
      color: var(--text);
      border: 1px solid var(--line);
    }

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

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

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

    .stat-card.primary::after { background: #ede9fe; }
    .stat-card.blue::after { background: #e0edff; }
    .stat-card.amber::after { background: #fff4df; }
    .stat-card.red::after { background: #ffe7ec; }

    .stat-icon {
      position: absolute;
      top: 22px;
      right: 22px;
      z-index: 1;
    }

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

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

    .stat-value.primary { color: #4f46e5; }
    .stat-value.blue { color: #2563eb; }
    .stat-value.amber { color: #f59e0b; }
    .stat-value.red { color: #ef4444; }

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

    .request-card {
      border-radius: 32px;
      overflow: hidden;
      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%);
    }

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

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

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

    .alert {
      display: flex;
      align-items: center;
      gap: 12px;
      padding: 16px 18px;
      border-radius: 22px;
      border: 1px solid #bbf7d0;
      background: #f0fdf4;
      color: #15803d;
      font-size: 14px;
      font-weight: 700;
    }

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

    .request-item {
      border-radius: 26px;
      padding: 16px;
      background: 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 26px rgba(148, 163, 184, 0.10);
      transition: all 0.2s ease;
    }

    .request-item:hover {
      transform: translateY(-2px);
      border-color: rgba(79, 70, 229, 0.28);
      box-shadow: 0 22px 34px rgba(59, 84, 145, 0.14);
    }

    .request-top {
      display: flex;
      align-items: flex-start;
      gap: 12px;
    }

    .item-thumb {
      width: 62px;
      height: 62px;
      border-radius: 20px;
      overflow: hidden;
      flex-shrink: 0;
      border: 1px solid rgba(219, 230, 247, 0.95);
      background:
        radial-gradient(circle at top left, rgba(79, 70, 229, 0.12), transparent 30%),
        linear-gradient(180deg, #f8fbff, #eef4ff);
      display: flex;
      align-items: center;
      justify-content: center;
      color: #94a3b8;
    }

    .item-thumb img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .item-name {
      color: #0f172a;
      font-weight: 900;
      line-height: 1.2;
    }

    .item-meta {
      margin-top: 4px;
      color: #64748b;
      font-size: 12px;
      font-weight: 600;
    }

    .request-desc {
      margin: 12px 0 0;
      color: #64748b;
      font-size: 13px;
      line-height: 1.55;
      min-height: 40px;
    }

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

    .request-info {
      border-radius: 18px;
      padding: 11px 12px;
      background: rgba(238, 242, 255, 0.72);
      border: 1px solid rgba(219, 230, 247, 0.95);
    }

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

    .request-info-value {
      margin-top: 5px;
      color: #172554;
      font-size: 12px;
      font-weight: 900;
      line-height: 1.35;
    }

    .request-bottom {
      margin-top: 14px;
      padding-top: 14px;
      border-top: 1px solid rgba(226, 232, 240, 0.86);
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 10px;
    }

    .badge {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      padding: 7px 10px;
      border-radius: 999px;
      font-size: 11px;
      font-weight: 900;
      letter-spacing: 0.08em;
      text-transform: uppercase;
      border: 1px solid transparent;
      white-space: nowrap;
    }

    .badge-pending { background: #fff7ed; color: #c2410c; border-color: #fed7aa; }
    .badge-staff { background: #eff6ff; color: #1d4ed8; border-color: #bfdbfe; }
    .badge-success { background: #f0fdf4; color: #15803d; border-color: #bbf7d0; }
    .badge-danger { background: #fef2f2; color: #b91c1c; border-color: #fecaca; }
    .badge-gray { background: #f1f5f9; color: #475569; border-color: #e2e8f0; }

    .detail-link {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 7px;
      min-height: 38px;
      padding: 0 14px;
      border-radius: 14px;
      color: var(--primary);
      background: #eef2ff;
      border: 1px solid #dbeafe;
      font-size: 13px;
      font-weight: 900;
      text-decoration: none;
      transition: all 0.2s ease;
    }

    .detail-link:hover {
      transform: translateY(-1px);
      background: #e0e7ff;
    }

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

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

    @media (max-width: 1024px) {
      .app { display: block; }
      .content { padding: 20px; padding-top: 84px; }
      .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
      .request-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    }

    @media (max-width: 640px) {
      .content { padding: 16px; padding-top: 84px; }
      .hero,
      .request-card { border-radius: 24px; }
      .hero-actions,
      .request-head { flex-direction: column; align-items: stretch; }
      .stats-grid { grid-template-columns: 1fr; }
      .request-grid { grid-template-columns: 1fr; padding: 16px; }
      .request-info-grid { grid-template-columns: 1fr; }
      .request-bottom { flex-direction: column; align-items: stretch; }
      .btn-primary,
      .btn-soft,
      .detail-link { width: 100%; }
    }
  </style>
</head>

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

    <main class="content">
      <div class="shell">
        @php
          $collection = $requests->getCollection();
          $activeCount = $collection->whereIn('status', ['pending', 'staff_approved', 'sekretariat_approved', 'kepala_approved'])->count();
          $approvedCount = $collection->whereIn('status', ['final_approved', 'approved'])->count();
        @endphp

        <section class="hero">
          <div class="hero-inner">
            <div>
              <span class="pill">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                  <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"></path>
                </svg>
                Peminjaman Barang
              </span>
              <h1 class="hero-title">Permintaan Peminjaman</h1>
              <p class="hero-text">
                Pantau semua request barang, cek status approval, dan buat peminjaman baru dengan alur yang lebih rapi.
              </p>
              <div class="hero-actions">
                <a href="{{ route('borrow.select') }}" class="btn-primary">
                  <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
                  </svg>
                  Buat Peminjaman Baru
                </a>
                <a href="{{ route('items.index') }}" class="btn-soft">
                  <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 17v-2m3 2v-4m3 4v-6M4 19h16"></path>
                  </svg>
                  Lihat Stok Barang
                </a>
              </div>
            </div>

            <div class="stats-grid">
              <div class="stat-card primary">
                <i data-lucide="clipboard-list" class="stat-icon w-5 h-5 text-indigo-600"></i>
                <div class="stat-label">Total Halaman Ini</div>
                <div class="stat-value primary">{{ $collection->count() }}</div>
                <div class="stat-sub">Request yang tampil di halaman ini.</div>
              </div>
              <div class="stat-card blue">
                <i data-lucide="loader-circle" class="stat-icon w-5 h-5 text-blue-600"></i>
                <div class="stat-label">Masih Diproses</div>
                <div class="stat-value blue">{{ $activeCount }}</div>
                <div class="stat-sub">Menunggu approval atau verifikasi.</div>
              </div>
              <div class="stat-card amber">
                <i data-lucide="circle-check" class="stat-icon w-5 h-5 text-amber-500"></i>
                <div class="stat-label">Disetujui</div>
                <div class="stat-value amber">{{ $approvedCount }}</div>
                <div class="stat-sub">Request yang sudah disetujui.</div>
              </div>
              <div class="stat-card red">
                <i data-lucide="database" class="stat-icon w-5 h-5 text-red-500"></i>
                <div class="stat-label">Total Data</div>
                <div class="stat-value red">{{ $requests->total() }}</div>
                <div class="stat-sub">Semua request sesuai akses Anda.</div>
              </div>
            </div>
          </div>
        </section>

        @if (session('success'))
        <div class="alert">
          <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
          </svg>
          {{ session('success') }}
        </div>
        @endif

        <section class="request-card surface">
          <div class="request-head">
            <div>
              <h2 class="request-title">Riwayat & Status Request</h2>
              <p class="request-subtitle">Tampilan kartu untuk melihat barang, jadwal, jumlah, dan status approval terbaru.</p>
            </div>
          </div>

          <div class="request-grid">
            @forelse ($requests as $request)
            @php
              $status = strtolower($request->status ?? '');
              $badgeClass = 'badge-gray';
              $label = ucfirst(str_replace('_', ' ', $status));

              if ($status === 'pending') {
                $badgeClass = 'badge-pending';
                $label = 'Menunggu';
              } elseif (in_array($status, ['staff_approved', 'sekretariat_approved', 'kepala_approved'])) {
                $badgeClass = 'badge-staff';
                $label = 'Proses';
              } elseif (in_array($status, ['final_approved', 'approved'])) {
                $badgeClass = 'badge-success';
                $label = 'Disetujui';
              } elseif ($status === 'rejected') {
                $badgeClass = 'badge-danger';
                $label = 'Ditolak';
              } elseif ($status === 'returned') {
                $badgeClass = 'badge-gray';
                $label = 'Dikembalikan';
              } elseif ($status === 'revision_requested') {
                $badgeClass = 'badge-pending';
                $label = 'Revisi';
              }

              $submittedHistory = $request->histories->firstWhere('action', 'submitted');
              $requestNote = trim((string) ($submittedHistory->comment ?? ''));
              $displayRequestNote = $requestNote !== '' && strcasecmp($requestNote, 'Permintaan dibuat') !== 0 ? $requestNote : '-';
            @endphp

            <article class="request-item">
              <div class="request-top">
                <div class="item-thumb">
                  @if(($request->item->image ?? null) && Storage::disk('public')->exists($request->item->image))
                  <img src="{{ asset('storage/' . $request->item->image) }}" alt="{{ $request->item->nama ?? 'Barang' }}">
                  @else
                  <i data-lucide="package" class="w-6 h-6"></i>
                  @endif
                </div>
                <div class="min-w-0 flex-1">
                  <div class="item-name">{{ $request->item->nama ?? '-' }}</div>
                  <div class="item-meta">{{ $request->item->divisi->nama ?? 'Umum' }}</div>
                </div>
                <span class="badge {{ $badgeClass }}">{{ $label }}</span>
              </div>

              <p class="request-desc">{{ $request->item->deskripsi ?? 'Tidak ada deskripsi barang.' }}</p>

              <div class="request-info-grid">
                <div class="request-info">
                  <div class="request-info-label">Peminjam</div>
                  <div class="request-info-value">{{ $request->user->name ?? '-' }}</div>
                </div>
                <div class="request-info">
                  <div class="request-info-label">Jumlah</div>
                  <div class="request-info-value">{{ $request->jumlah ?? 0 }} Unit</div>
                </div>
                <div class="request-info">
                  <div class="request-info-label">Pinjam</div>
                  <div class="request-info-value">{{ $request->tanggal_pinjam ? \Carbon\Carbon::parse($request->tanggal_pinjam)->format('d M Y, H:i') : '-' }}</div>
                </div>
                <div class="request-info">
                  <div class="request-info-label">Kembali</div>
                  <div class="request-info-value">{{ $request->tanggal_kembali ? \Carbon\Carbon::parse($request->tanggal_kembali)->format('d M Y, H:i') : '-' }}</div>
                </div>
                <div class="request-info">
                  <div class="request-info-label">Keterangan</div>
                  <div class="request-info-value">{{ $displayRequestNote }}</div>
                </div>
              </div>

              <div class="request-bottom">
                <div class="text-xs font-bold text-slate-500">
                  Dikembalikan: {{ $request->actual_return_date ? \Carbon\Carbon::parse($request->actual_return_date)->format('d M Y, H:i') : '-' }}
                </div>
                <a href="{{ route('borrow.show', $request->id) }}" class="detail-link">
                  <i data-lucide="eye" class="w-4 h-4"></i>
                  Detail
                </a>
              </div>
            </article>
            @empty
            <div class="empty-state col-span-full">
              <div class="empty-icon">
                <i data-lucide="clipboard-list" class="w-8 h-8"></i>
              </div>
              <div>
                <div class="font-black text-slate-800">Belum ada permintaan peminjaman</div>
                <div class="mt-1 text-sm text-slate-500">Mulai dengan membuat request barang baru.</div>
              </div>
              <a href="{{ route('borrow.select') }}" class="btn-primary">Buat Peminjaman Baru</a>
            </div>
            @endforelse
          </div>

          @if($requests->hasPages())
          <div class="p-5 border-t border-slate-100 bg-white/70">
            {{ $requests->links() }}
          </div>
          @endif
        </section>
      </div>
    </main>
  </div>
  <script>
    lucide.createIcons();
  </script>
</body>

</html>
