@extends('layouts.app')

@section('title', 'Laporan Inventaris')
@section('page_title', 'Laporan Inventaris')
@section('page_pretitle', 'Rekapitulasi')

@section('page_actions')
  <button class="btn" onclick="window.print()">
    <i class="ti ti-printer me-1"></i>
    Cetak
  </button>
@endsection

@section('content')
  @php
    $metrics = [
      ['label' => 'Total Barang', 'value' => $summary['total_items'], 'description' => 'Seluruh aset tercatat', 'icon' => 'ti-box-multiple', 'class' => 'bg-primary-lt text-primary'],
      ['label' => 'Total Ruangan', 'value' => $summary['total_rooms'], 'description' => 'Ruangan aktif/nonaktif', 'icon' => 'ti-door', 'class' => 'bg-success-lt text-success'],
      ['label' => 'Perawatan', 'value' => $summary['maintenance'], 'description' => 'Barang perlu dipelihara', 'icon' => 'ti-tool', 'class' => 'bg-warning-lt text-warning'],
      ['label' => 'Rusak', 'value' => $summary['broken'], 'description' => 'Barang rusak', 'icon' => 'ti-alert-triangle', 'class' => 'bg-danger-lt text-danger'],
      ['label' => 'Scan Hari Ini', 'value' => $summary['scan_today'], 'description' => 'Aktivitas petugas mobile', 'icon' => 'ti-scan', 'class' => 'bg-purple-lt text-purple'],
    ];
  @endphp

  <div class="row row-deck row-cards">
    <div class="col-12">
      <div class="row row-cards">
        @foreach ($metrics as $metric)
          <div class="col-sm-6 col-xl">
            <div class="card metric-card">
              <div class="card-body">
                <div class="d-flex align-items-center">
                  <span class="metric-icon {{ $metric['class'] }}">
                    <i class="ti {{ $metric['icon'] }}"></i>
                  </span>
                  <div class="ms-3">
                    <div class="text-secondary">{{ $metric['label'] }}</div>
                    <div class="h1 mb-0">{{ $metric['value'] }}</div>
                  </div>
                </div>
                <div class="text-secondary small mt-3">{{ $metric['description'] }}</div>
              </div>
            </div>
          </div>
        @endforeach
      </div>
    </div>

    <div class="col-lg-6">
      <div class="card">
        <div class="card-header">
          <div>
            <h3 class="card-title">Rekap per Kategori</h3>
            <p class="card-subtitle">Jumlah barang pada setiap kategori inventaris.</p>
          </div>
        </div>
        <div class="card-body">
          <div class="row g-2">
            @foreach ($categories as $category)
              <div class="col-sm-6">
                <div class="border rounded p-3 h-100">
                  <div class="text-secondary">{{ $category->nama_kategori }}</div>
                  <div class="h1 mb-0">{{ $category->inventory_items_count }}</div>
                </div>
              </div>
            @endforeach
          </div>
        </div>
      </div>
    </div>

    <div class="col-lg-6">
      <div class="card">
        <div class="card-header">
          <div>
            <h3 class="card-title">Top Ruangan</h3>
            <p class="card-subtitle">Ruangan dengan jumlah barang terbanyak.</p>
          </div>
        </div>
        <div class="list-group list-group-flush">
          @foreach ($rooms->take(5) as $room)
            <div class="list-group-item">
              <div class="row align-items-center">
                <div class="col-auto">
                  <span class="avatar bg-blue-lt text-blue"><i class="ti ti-building"></i></span>
                </div>
                <div class="col">
                  <div class="fw-bold">{{ $room->nama_ruangan }}</div>
                  <div class="text-secondary small">{{ $room->inventory_items_count }} barang</div>
                </div>
                <div class="col-auto">
                  <span class="badge bg-primary-lt text-primary">{{ $room->kode_ruangan }}</span>
                </div>
              </div>
            </div>
          @endforeach
        </div>
      </div>
    </div>

    <div class="col-12">
      <div class="card">
        <div class="card-header">
          <div>
            <h3 class="card-title">Riwayat Scan Terbaru</h3>
            <p class="card-subtitle">Aktivitas scan QR ruangan dan barang dari petugas lapangan.</p>
          </div>
        </div>
        <div class="table-responsive">
          <table class="table table-vcenter card-table">
            <thead>
              <tr>
                <th>Waktu</th>
                <th>Ruangan</th>
                <th>Barang</th>
                <th>Petugas</th>
                <th>Aktivitas</th>
              </tr>
            </thead>
            <tbody>
              @forelse ($latestScans as $scan)
                <tr>
                  <td data-label="Waktu">{{ $scan->dipindai_pada->format('d/m/Y H:i') }}</td>
                  <td data-label="Ruangan">{{ $scan->room?->nama_ruangan ?? '-' }}</td>
                  <td data-label="Barang">{{ $scan->inventoryItem?->nama_barang ?? '-' }}</td>
                  <td data-label="Petugas">{{ $scan->nama_petugas }}</td>
                  <td data-label="Aktivitas">{{ $scan->deskripsi }}</td>
                </tr>
              @empty
                <tr>
                  <td colspan="5" class="text-center text-secondary py-5">Belum ada riwayat scan.</td>
                </tr>
              @endforelse
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
@endsection
