@extends('layouts.admin')
@section('title','Manajemen Tiket')
@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-3"><input type="text" name="search" class="form-control form-control-sm" placeholder="Cari tiket / subjek..." value="{{ request('search') }}"></div>
      <div class="col-md-2">
        <select name="status" class="form-select form-select-sm">
          <option value="">Semua Status</option>
          @foreach(['open','in_progress','waiting_client','resolved','closed'] as $s)
          <option value="{{ $s }}" {{ request('status')===$s?'selected':'' }}>{{ strtoupper($s) }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-2">
        <select name="priority" class="form-select form-select-sm">
          <option value="">Semua Prioritas</option>
          @foreach(['critical','high','medium','low'] as $p)
          <option value="{{ $p }}" {{ request('priority')===$p?'selected':'' }}>{{ strtoupper($p) }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-2">
        <select name="division" class="form-select form-select-sm">
          <option value="">Semua Divisi</option>
          @foreach(['JPN','FixLab','Havva'] as $d)
          <option value="{{ $d }}" {{ request('division')===$d?'selected':'' }}>{{ $d }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-2">
        <select name="sla" class="form-select form-select-sm">
          <option value="">Semua SLA</option>
          <option value="breach" {{ request('sla')==='breach'?'selected':'' }}>Breach</option>
          <option value="warning" {{ request('sla')==='warning'?'selected':'' }}>Warning</option>
          <option value="ok" {{ request('sla')==='ok'?'selected':'' }}>OK</option>
        </select>
      </div>
      <div class="col-md-1 d-flex gap-1">
        <button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-search"></i></button>
        <a href="{{ route('admin.tickets.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 Tiket <span class="badge bg-secondary ms-1">{{ $tickets->total() }}</span></span>
    <a href="{{ route('admin.tickets.create') }}" class="btn btn-primary btn-sm"><i class="fas fa-plus me-1"></i>Buat Tiket</a>
  </div>
  <div class="table-responsive">
    <table class="table table-hover mb-0">
      <thead>
        <tr>
          <th>No. Tiket</th><th>Klien</th><th>Subjek</th><th>Prioritas</th>
          <th>Status</th><th>SLA</th><th>Teknisi</th><th>Dibuat</th><th></th>
        </tr>
      </thead>
      <tbody>
      @forelse($tickets as $t)
      <tr>
        <td><a href="{{ route('admin.tickets.show',$t) }}" class="fw-semibold text-decoration-none text-primary">{{ $t->ticket_number }}</a></td>
        <td>
          <div>{{ $t->client->name }}</div>
          <div class="text-muted" style="font-size:11px">{{ $t->division }}</div>
        </td>
        <td class="text-truncate" style="max-width:200px" title="{{ $t->subject }}">{{ $t->subject }}</td>
        <td><span class="badge bg-{{ $t->priority_badge }}">{{ strtoupper($t->priority) }}</span></td>
        <td><span class="badge bg-{{ $t->status_badge }}">{{ str_replace('_',' ',strtoupper($t->status)) }}</span></td>
        <td>
          @if($t->sla_status==='breach') <span class="badge badge-breach">BREACH</span>
          @elseif($t->sla_status==='warning') <span class="badge badge-warning">WARNING</span>
          @elseif($t->sla_status==='ok') <span class="badge badge-ok">OK</span>
          @else <span class="text-muted small">—</span>
          @endif
        </td>
        <td>{{ $t->assignedTo?->name ?? '<span class="text-muted">—</span>' }}</td>
        <td><span class="text-muted small" title="{{ $t->created_at }}">{{ $t->created_at->diffForHumans() }}</span></td>
        <td><a href="{{ route('admin.tickets.show',$t) }}" class="btn btn-sm btn-outline-primary"><i class="fas fa-eye"></i></a></td>
      </tr>
      @empty
      <tr><td colspan="9" class="text-center text-muted py-5">Tidak ada tiket ditemukan</td></tr>
      @endforelse
      </tbody>
    </table>
  </div>
  <div class="card-footer">{{ $tickets->links() }}</div>
</div>
@endsection
