@extends('layouts.admin')
@section('title','ISP – '.$client->name)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3">
  <h6 class="mb-0"><a href="{{ route('admin.clients.show',$client) }}" class="text-decoration-none">{{ $client->name }}</a> › ISP</h6>
  <button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#addIspModal"><i class="fas fa-plus me-1"></i>Tambah ISP</button>
</div>
@foreach($client->isps as $isp)
<div class="card mb-3">
  <div class="card-header d-flex justify-content-between align-items-center">
    <div><span class="fw-bold">{{ $isp->provider_name }}</span><span class="text-muted small ms-2">{{ $isp->package }} · {{ $isp->bandwidth }}</span></div>
    <div>
      @if($isp->incidents->where('status','ongoing')->count())<span class="badge badge-breach me-2"><i class="fas fa-wifi me-1"></i>DOWN</span>@else<span class="badge badge-ok me-2">UP</span>@endif
      <button class="btn btn-sm btn-outline-warning" data-bs-toggle="modal" data-bs-target="#incidentModal{{ $isp->id }}"><i class="fas fa-exclamation-triangle"></i> Lapor Insiden</button>
    </div>
  </div>
  <div class="card-body py-2">
    <div class="row g-2 small text-muted">
      <div class="col-md-3">IP: {{ $isp->ip_address ?? '—' }}</div>
      <div class="col-md-3">No. Akun: {{ $isp->account_number ?? '—' }}</div>
      <div class="col-md-3">Support: {{ $isp->support_contact ?? '—' }}</div>
    </div>
    @if($isp->incidents->count())
    <div class="mt-2">
      <div class="fw-semibold small mb-1">Riwayat Insiden:</div>
      @foreach($isp->incidents->take(5) as $inc)
      <div class="d-flex gap-2 align-items-center py-1 border-bottom small">
        <span class="badge {{ $inc->status==='ongoing'?'badge-breach':'bg-secondary' }}">{{ strtoupper($inc->status) }}</span>
        <span>{{ $inc->down_at->format('d/m/Y H:i') }}</span>
        @if($inc->up_at)<span class="text-muted">→ {{ $inc->up_at->format('H:i') }} ({{ $inc->duration }})</span>@endif
        <span class="text-muted">{{ $inc->description }}</span>
      </div>
      @endforeach
    </div>
    @endif
  </div>
</div>
{{-- Incident Modal --}}
<div class="modal fade" id="incidentModal{{ $isp->id }}" tabindex="-1">
  <div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><h6 class="modal-title">Lapor Insiden ISP – {{ $isp->provider_name }}</h6><button class="btn-close" data-bs-dismiss="modal"></button></div>
    <form action="{{ route('admin.clients.isp.incident',[$client,$isp]) }}" method="POST">@csrf
    <div class="modal-body">
      <div class="mb-2"><label class="form-label small fw-semibold">Waktu Down *</label><input type="datetime-local" name="down_at" class="form-control form-control-sm" value="{{ now()->format('Y-m-d\TH:i') }}" required></div>
      <div class="mb-2"><label class="form-label small fw-semibold">Waktu Up (kosong jika masih down)</label><input type="datetime-local" name="up_at" class="form-control form-control-sm"></div>
      <div class="mb-2"><label class="form-label small fw-semibold">Keterangan</label><textarea name="description" class="form-control form-control-sm" rows="2"></textarea></div>
    </div>
    <div class="modal-footer"><button class="btn btn-warning btn-sm">Simpan</button></div>
    </form>
  </div></div>
</div>
@endforeach

{{-- Add ISP Modal --}}
<div class="modal fade" id="addIspModal" tabindex="-1">
  <div class="modal-dialog"><div class="modal-content">
    <div class="modal-header"><h6 class="modal-title">Tambah ISP</h6><button class="btn-close" data-bs-dismiss="modal"></button></div>
    <form action="{{ route('admin.clients.isp.store',$client) }}" method="POST">@csrf
    <div class="modal-body">
      <div class="row g-2">
        <div class="col-12"><label class="form-label small fw-semibold">Provider *</label><input type="text" name="provider_name" class="form-control form-control-sm" required placeholder="Biznet, IndiHome, dll"></div>
        <div class="col-6"><label class="form-label small fw-semibold">Paket</label><input type="text" name="package" class="form-control form-control-sm"></div>
        <div class="col-6"><label class="form-label small fw-semibold">Bandwidth</label><input type="text" name="bandwidth" class="form-control form-control-sm" placeholder="100 Mbps"></div>
        <div class="col-6"><label class="form-label small fw-semibold">IP Address</label><input type="text" name="ip_address" class="form-control form-control-sm"></div>
        <div class="col-6"><label class="form-label small fw-semibold">No. Akun</label><input type="text" name="account_number" class="form-control form-control-sm"></div>
        <div class="col-12"><label class="form-label small fw-semibold">Kontak Support</label><input type="text" name="support_contact" class="form-control form-control-sm"></div>
      </div>
    </div>
    <div class="modal-footer"><button class="btn btn-primary btn-sm">Simpan</button></div>
    </form>
  </div></div>
</div>
@endsection
