@extends('layouts.admin')
@section('title','Dashboard')
@section('content')
<div class="row g-3 mb-4">
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#1a56db,#2563eb)">
      <div class="num">{{ $stats['tickets_open'] }}</div>
      <div class="label"><i class="fas fa-ticket me-1"></i>Tiket Open</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#d97706,#f59e0b)">
      <div class="num">{{ $stats['tickets_inprogress'] }}</div>
      <div class="label"><i class="fas fa-spinner me-1"></i>In Progress</div>
    </div>
  </div>
  <div class="col-6 col-md-3">
    <div class="stat-card" style="background:linear-gradient(135deg,#dc2626,#ef4444)">
      <div class="num">{{ $stats['sla_breach'] }}</div>
      <div class="label"><i class="fas fa-exclamation-triangle me-1"></i>SLA Breach</div>
    </div>
  </div>
  @if($stats['revenue_month'] !== null)
    <div class="col-6 col-md-3">
      <div class="stat-card" style="background:linear-gradient(135deg,#059669,#10b981)">
        <div class="num">Rp {{ number_format($stats['revenue_month'],0,',','.') }}</div>
        <div class="label"><i class="fas fa-coins me-1"></i>Revenue Bulan Ini</div>
      </div>
    </div>
  @endif
</div>

<div class="row g-3 mb-4">
  <div class="col-md-3 col-6"><div class="card p-3 text-center"><div class="text-muted small">Klien Aktif</div><div class="fw-bold fs-4">{{ $stats['clients_active'] }}</div></div></div>
  <div class="col-md-3 col-6"><div class="card p-3 text-center"><div class="text-muted small">Kunjungan Hari Ini</div><div class="fw-bold fs-4">{{ $stats['visits_today'] }}</div></div></div>
  @if($stats['invoices_overdue'] !== null)
    <div class="col-md-3 col-6"><div class="card p-3 text-center"><div class="text-muted small">Invoice Overdue</div><div class="fw-bold fs-4 text-danger">{{ $stats['invoices_overdue'] }}</div></div></div>
  @endif
  <div class="col-md-3 col-6"><div class="card p-3 text-center"><div class="text-muted small">Tiket Selesai Bulan Ini</div><div class="fw-bold fs-4 text-success">{{ $stats['tickets_resolved'] }}</div></div></div>
</div>

<div class="row g-3">
  <div class="col-lg-8">
    <div class="card">
      <div class="card-header d-flex justify-content-between align-items-center">
        <span>Tiket Terbaru</span>
        <a href="{{ route('admin.tickets.index') }}" class="btn btn-sm btn-outline-primary">Lihat Semua</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></tr></thead>
          <tbody>
          @forelse($recentTickets as $t)
          <tr>
            <td><a href="{{ route('admin.tickets.show',$t) }}" class="fw-semibold text-decoration-none">{{ $t->ticket_number }}</a></td>
            <td>{{ $t->client->name }}</td>
            <td class="text-truncate" style="max-width:180px">{{ $t->subject }}</td>
            <td><span class="badge bg-{{ $t->priority_badge }}">{{ strtoupper($t->priority) }}</span></td>
            <td><span class="badge bg-{{ $t->status_badge }}">{{ 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">—</span>
              @endif
            </td>
          </tr>
          @empty
          <tr><td colspan="6" class="text-center text-muted py-4">Belum ada tiket</td></tr>
          @endforelse
          </tbody>
        </table>
      </div>
    </div>
  </div>
  <div class="col-lg-4">
    <div class="card mb-3">
      <div class="card-header">SLA Breach <span class="badge bg-danger ms-1">{{ $slaBreaches->count() }}</span></div>
      <div class="list-group list-group-flush">
        @forelse($slaBreaches as $t)
        <a href="{{ route('admin.tickets.show',$t) }}" class="list-group-item list-group-item-action py-2">
          <div class="d-flex justify-content-between">
            <span class="fw-semibold small">{{ $t->ticket_number }}</span>
            <span class="badge bg-danger small">BREACH</span>
          </div>
          <div class="text-muted" style="font-size:12px">{{ $t->client->name }} · {{ $t->sla_solve_deadline?->diffForHumans() }}</div>
        </a>
        @empty
        <div class="list-group-item text-center text-muted small py-3">Tidak ada SLA breach 🎉</div>
        @endforelse
      </div>
    </div>
    <div class="card">
      <div class="card-header">Kunjungan Hari Ini</div>
      <div class="list-group list-group-flush">
        @forelse($todayVisits as $v)
        <a href="{{ route('admin.visits.show',$v) }}" class="list-group-item list-group-item-action py-2">
          <div class="fw-semibold small">{{ $v->client->name }}</div>
          <div class="text-muted" style="font-size:12px">{{ $v->visit_time_start ?? '—' }} · {{ $v->technician?->name ?? 'Belum ditugaskan' }}</div>
        </a>
        @empty
        <div class="list-group-item text-center text-muted small py-3">Tidak ada kunjungan hari ini</div>
        @endforelse
      </div>
    </div>
  </div>
</div>
@endsection
