@extends('layouts.admin')
@section('title','Manajemen Klien')
@section('content')
<div class="card mb-3">
  <div class="card-body py-2">
    <form class="row g-2 align-items-end" method="GET">
      <div class="col-md-5"><input type="text" name="search" class="form-control form-control-sm" placeholder="Cari nama / perusahaan..." value="{{ request('search') }}"></div>
      <div class="col-md-3">
        <select name="type" class="form-select form-select-sm">
          <option value="">Semua Tipe</option>
          <option value="maintenance" {{ request('type')==='maintenance'?'selected':'' }}>Maintenance</option>
          <option value="non_maintenance" {{ request('type')==='non_maintenance'?'selected':'' }}>Non-Maintenance</option>
          <option value="individual" {{ request('type')==='individual'?'selected':'' }}>Perorangan</option>
        </select>
      </div>
      <div class="col-md-2 d-flex gap-1">
        <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i></button>
        <a href="{{ route('admin.clients.index') }}" class="btn btn-secondary btn-sm"><i class="fas fa-xmark"></i></a>
      </div>
    </form>
  </div>
</div>
<div class="card">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span>Daftar Klien <span class="badge bg-secondary ms-1">{{ $clients->total() }}</span></span>
    <a href="{{ route('admin.clients.create') }}" class="btn btn-primary btn-sm"><i class="fas fa-plus me-1"></i>Tambah Klien</a>
  </div>
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead><tr><th>Nama</th><th>Tipe</th><th>Divisi</th><th>PIC</th><th>Tiket</th><th>Kunjungan</th><th>Status</th><th></th></tr></thead>
      <tbody>
      @forelse($clients as $c)
      <tr>
        <td>
          <a href="{{ route('admin.clients.show',$c) }}" class="fw-semibold text-decoration-none">{{ $c->name }}</a>
          @if($c->company_name && $c->company_name !== $c->name)<div class="text-muted small">{{ $c->company_name }}</div>@endif
        </td>
        <td>
          <span class="badge {{ $c->type==='maintenance'?'bg-primary':($c->type==='non_maintenance'?'bg-warning text-dark':'bg-secondary') }}">
            {{ $c->type_label }}
          </span>
        </td>
        <td><span class="badge bg-light text-dark border">{{ $c->division }}</span></td>
        <td><div class="small">{{ $c->pic_name ?? '—' }}</div><div class="text-muted" style="font-size:11px">{{ $c->pic_phone ?? '' }}</div></td>
        <td><span class="badge bg-light text-dark">{{ $c->tickets_count }}</span></td>
        <td><span class="badge bg-light text-dark">{{ $c->visits_count }}</span></td>
        <td>
          @if($c->is_active) <span class="badge badge-ok">Aktif</span>
          @else <span class="badge bg-secondary">Nonaktif</span>
          @endif
        </td>
        <td>
          <a href="{{ route('admin.clients.show',$c) }}" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i></a>
          <a href="{{ route('admin.clients.edit',$c) }}" class="btn btn-sm btn-outline-secondary"><i class="fas fa-edit"></i></a>
          @if(auth()->user()->role === 'admin')
          <form action="{{ route('admin.clients.destroy',$c) }}" method="POST" onsubmit="return confirm('Hapus klien ini beserta semua data terkait?')" style="display:inline">@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-5">Belum ada klien</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
  <div class="card-footer">{{ $clients->links() }}</div>
</div>
@endsection
