@extends('layouts.app')

@section('title', 'Penggantian Barang')
@section('page_title', 'Penggantian Barang')
@section('page_pretitle', 'Penggantian')

@section('content')
  @php
    $statusMap = [
      'good' => 'bg-success-lt text-success',
      'maintenance' => 'bg-warning-lt text-warning',
      'broken' => 'bg-danger-lt text-danger',
      'replace' => 'bg-purple-lt text-purple',
    ];
  @endphp

  <div class="card">
    <div class="card-header">
      <div>
        <h3 class="card-title">Barang Rusak atau Diajukan Ganti</h3>
        <p class="card-subtitle">Kelola pengajuan penggantian barang dari dashboard dan mobile petugas.</p>
      </div>
    </div>
    <div class="table-responsive">
      <table class="table table-vcenter card-table">
        <thead>
          <tr>
            <th>Barang</th>
            <th>Kategori</th>
            <th>Ruangan</th>
            <th>Status</th>
            <th>Pengajuan</th>
            <th class="w-1">Aksi</th>
          </tr>
        </thead>
        <tbody>
          @forelse ($items as $item)
            <tr>
              <td data-label="Barang">
                <div class="fw-bold">{{ $item->nama_barang }}</div>
                <div class="text-secondary small">{{ $item->kode_aset }}</div>
              </td>
              <td data-label="Kategori">{{ $item->category->nama_kategori }}</td>
              <td data-label="Ruangan">{{ $item->room->nama_ruangan }}</td>
              <td data-label="Status"><span class="badge {{ $statusMap[$item->status_barang] ?? 'bg-secondary-lt' }}">{{ $item->status_label }}</span></td>
              <td data-label="Pengajuan">
                <span class="badge {{ $item->pengajuan_penggantian ? 'bg-danger-lt text-danger' : 'bg-secondary-lt text-secondary' }}">
                  {{ $item->pengajuan_penggantian ? 'Diajukan' : 'Belum diajukan' }}
                </span>
              </td>
              <td data-label="Aksi">
                <div class="btn-list flex-nowrap">
                  @unless ($item->pengajuan_penggantian)
                    <form action="{{ route('replacements.request', $item) }}" method="POST">
                      @csrf
                      <button class="btn btn-primary btn-sm" type="submit">
                        <i class="ti ti-send me-1"></i>
                        Ajukan
                      </button>
                    </form>
                  @endunless
                  <form action="{{ route('replacements.complete', $item) }}" method="POST">
                    @csrf
                    <button class="btn btn-success btn-sm" type="submit">
                      <i class="ti ti-check me-1"></i>
                      Selesai
                    </button>
                  </form>
                </div>
              </td>
            </tr>
          @empty
            <tr>
              <td colspan="6" class="text-center text-secondary py-5">Tidak ada barang yang perlu diganti.</td>
            </tr>
          @endforelse
        </tbody>
      </table>
    </div>
  </div>
@endsection
