@extends('layouts.admin')
@section('title','Perangkat - '.$client->name)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
  <div>
    <a href="{{ route('admin.clients.show',$client) }}" class="text-decoration-none text-muted small"><i class="fas fa-arrow-left me-1"></i>Kembali ke {{ $client->name }}</a>
    <h5 class="fw-bold mb-0 mt-1">Inventaris Perangkat — {{ $client->name }}</h5>
  </div>
  <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addDeviceModal">
    <i class="fas fa-plus me-1"></i>Tambah Perangkat
  </button>
</div>
<div class="card">
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead><tr><th>Tipe</th><th>Merk / Model</th><th>Serial Number</th><th>Spesifikasi</th><th>Kondisi</th><th>User</th><th>Beli</th><th></th></tr></thead>
      <tbody>
      @forelse($client->devices as $d)
      <tr>
        <td><span class="badge bg-light text-dark border">{{ strtoupper($d->type) }}</span></td>
        <td class="small fw-semibold">{{ $d->brand }} {{ $d->model }}</td>
        <td class="text-muted small">{{ $d->serial_number ?? '—' }}</td>
        <td class="small" style="max-width:150px;white-space:pre-wrap;font-size:11px">{{ $d->specs ?? '—' }}</td>
        <td>
          <span class="badge {{ $d->condition==='good'?'badge-ok':($d->condition==='fair'?'badge-warning':'badge-breach') }}">
            {{ strtoupper($d->condition) }}
          </span>
        </td>
        <td class="small">{{ $d->clientUser?->name ?? '—' }}</td>
        <td class="small text-muted">{{ $d->purchase_date?->format('d/m/Y') ?? '—' }}</td>
        <td>
          @if(auth()->user()->role === 'admin')
          <form action="{{ route('admin.clients.devices.destroy',[$client,$d]) }}" method="POST" onsubmit="return confirm('Hapus perangkat ini?')">
            @csrf @method('DELETE')
            <button class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button>
          </form>
          @endif
        </td>
      </tr>
      @empty
      <tr><td colspan="8" class="text-center text-muted py-4">Belum ada perangkat terdaftar</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
</div>

<div class="modal fade" id="addDeviceModal" tabindex="-1">
  <div class="modal-dialog modal-lg"><div class="modal-content">
    <div class="modal-header"><h6 class="modal-title fw-bold">Tambah Perangkat</h6><button class="btn-close" data-bs-dismiss="modal"></button></div>
    <form action="{{ route('admin.clients.devices.store',$client) }}" method="POST">@csrf
    <div class="modal-body">
      <div class="row g-2">
        <div class="col-md-6">
          <label class="form-label small fw-semibold">User Pemilik</label>
          <select name="client_user_id" class="form-select form-select-sm">
            <option value="">-- Tidak ada --</option>
            @foreach($client->users as $u)<option value="{{ $u->id }}">{{ $u->name }}</option>@endforeach
          </select>
        </div>
        <div class="col-md-6">
          <label class="form-label small fw-semibold">Tipe *</label>
          <select name="type" class="form-select form-select-sm" required>
            @foreach(['laptop'=>'Laptop','pc'=>'PC / Desktop','printer'=>'Printer','router'=>'Router','switch'=>'Switch','cctv'=>'CCTV','other'=>'Lainnya'] as $v=>$l)
            <option value="{{ $v }}">{{ $l }}</option>
            @endforeach
          </select>
        </div>
        <div class="col-md-4"><label class="form-label small fw-semibold">Merk</label><input type="text" name="brand" class="form-control form-control-sm"></div>
        <div class="col-md-4"><label class="form-label small fw-semibold">Model</label><input type="text" name="model" class="form-control form-control-sm"></div>
        <div class="col-md-4"><label class="form-label small fw-semibold">Serial Number</label><input type="text" name="serial_number" class="form-control form-control-sm"></div>
        <div class="col-md-8"><label class="form-label small fw-semibold">Spesifikasi</label><textarea name="specs" class="form-control form-control-sm" rows="2" placeholder="RAM 8GB, Intel i5, SSD 256GB..."></textarea></div>
        <div class="col-md-4">
          <label class="form-label small fw-semibold">Kondisi</label>
          <select name="condition" class="form-select form-select-sm">
            <option value="good">Good</option>
            <option value="fair">Fair</option>
            <option value="poor">Poor</option>
          </select>
        </div>
        <div class="col-md-4"><label class="form-label small fw-semibold">Tgl Pembelian</label><input type="date" name="purchase_date" class="form-control form-control-sm"></div>
        <div class="col-12"><label class="form-label small fw-semibold">Catatan</label><textarea name="notes" class="form-control form-control-sm" rows="2"></textarea></div>
      </div>
    </div>
    <div class="modal-footer"><button type="submit" class="btn btn-primary btn-sm">Simpan</button></div>
    </form>
  </div></div>
</div>
@endsection
