@extends('layouts.admin')
@section('title','Detail Kunjungan')
@section('content')
<div class="row justify-content-center"><div class="col-lg-7">
<div class="card mb-3">
  <div class="card-header d-flex justify-content-between align-items-center">
    <span class="fw-bold">Kunjungan ke {{ $visit->client->name }}</span>
    <div class="d-flex gap-2 align-items-center">
      <span class="badge {{ ['scheduled'=>'bg-primary','in_progress'=>'bg-warning text-dark','done'=>'bg-success','cancelled'=>'bg-danger'][$visit->status]??'bg-secondary' }}">{{ strtoupper($visit->status) }}</span>
      <form action="{{ route('admin.visits.remind',$visit) }}" method="POST" style="display:inline">@csrf
        <button class="btn btn-sm btn-outline-success" title="Kirim reminder"><i class="fas fa-bell"></i></button>
      </form>
      <a href="{{ route('admin.visits.preparation.form',$visit) }}" class="btn btn-sm btn-outline-primary">Checklist</a>
      @if(auth()->user()->role === 'admin')
      <form action="{{ route('admin.visits.destroy',$visit) }}" method="POST" onsubmit="return confirm('Hapus kunjungan ini?')" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></button></form>
      @endif
    </div>
  </div>
  <div class="card-body">
    <table class="table table-sm">
      <tr><td class="text-muted small" width="140">Klien</td><td><a href="{{ route('admin.clients.show',$visit->client) }}">{{ $visit->client->name }}</a></td></tr>
      <tr><td class="text-muted small">Tanggal</td><td class="fw-semibold">{{ $visit->visit_date->format('d F Y') }}</td></tr>
      <tr><td class="text-muted small">Jam</td><td>{{ $visit->visit_time_start ?? '—' }} – {{ $visit->visit_time_end ?? '—' }}</td></tr>
      <tr><td class="text-muted small">Teknisi</td><td>{{ $visit->technician?->name ?? '—' }}</td></tr>
      @if($visit->tickets->isNotEmpty())
      <tr><td class="text-muted small">Tiket</td><td>
        @foreach($visit->tickets as $ticket)
          <div><a href="{{ route('admin.tickets.show',$ticket) }}">{{ $ticket->ticket_number }}</a> — {{ $ticket->subject }}</div>
        @endforeach
      </td></tr>
      @elseif($visit->ticket)
      <tr><td class="text-muted small">Tiket</td><td><a href="{{ route('admin.tickets.show',$visit->ticket) }}">{{ $visit->ticket->ticket_number }}</a> — {{ $visit->ticket->subject }}</td></tr>
      @endif
      <tr><td class="text-muted small">Tujuan</td><td>{{ $visit->purpose ?? '—' }}</td></tr>
      <tr><td class="text-muted small">Catatan</td><td>{{ $visit->notes ?? '—' }}</td></tr>
      @if($visit->is_extra && auth()->user()->role !== 'technician')<tr><td class="text-muted small">Kunjungan Ekstra</td><td><span class="badge badge-warning">YA</span> — Rp {{ number_format($visit->extra_charge,0,',','.') }}</td></tr>@endif
      @if($visit->checklists->isNotEmpty())
      <tr><td class="text-muted small">Status Checklist</td><td>
        @foreach($visit->checklists as $checklist)
          <div class="mb-1">
            <span class="badge {{ $checklist->completed ? 'bg-success' : 'bg-warning text-dark' }}">
              {{ $checklist->completed ? 'SELESAI' : 'BELUM SELESAI' }}
            </span>
            <small class="text-muted">{{ $checklist->user?->name ?? 'Unknown' }}
              @if($checklist->completed_at)
                • {{ $checklist->completed_at->format('d/m/Y H:i') }}
              @endif
            </small>
          </div>
        @endforeach
      </td></tr>
      @endif
    </table>
  </div>
</div>
<div class="card mb-3">
  <div class="card-header">Tambahkan Tiket ke Kunjungan</div>
  <div class="card-body">
    @if($availableTickets->isNotEmpty())
    <form action="{{ route('admin.visits.tickets.attach',$visit) }}" method="POST" class="row g-2 align-items-end">
      @csrf
      <div class="col-md-9">
        <label class="form-label fw-semibold">Pilih tiket</label>
        <select name="ticket_id" class="form-select" required>
          <option value="">-- Pilih tiket dari klien ini --</option>
          @foreach($availableTickets as $ticket)
            <option value="{{ $ticket->id }}">{{ $ticket->ticket_number }} — {{ $ticket->subject }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-3 d-grid">
        <button type="submit" class="btn btn-primary">Tambah</button>
      </div>
    </form>
    @else
    <div class="small text-muted">
      Tidak ada tiket lain yang belum terhubung ke kunjungan ini. <a href="{{ route('admin.tickets.create',['client_id'=>$visit->client_id,'visit_id'=>$visit->id]) }}">Buat tiket baru</a> jika diperlukan.
    </div>
    @endif
  </div>
</div>
<div class="card mb-3">
  <div class="card-header">Update Status</div>
  <div class="card-body">
    <form action="{{ route('admin.visits.status',$visit) }}" method="POST" class="d-flex gap-2">@csrf
      <select name="status" class="form-select">
        @foreach(['scheduled','in_progress','done','cancelled'] as $s)
        <option value="{{ $s }}" {{ $visit->status===$s?'selected':'' }}>{{ strtoupper($s) }}</option>
        @endforeach
      </select>
      <button type="submit" class="btn btn-primary text-nowrap"><i class="fas fa-check me-1"></i>Update</button>
    </form>
  </div>
</div>
<div class="card">
  <div class="card-header">Edit Kunjungan</div>
  <div class="card-body">
    <form action="{{ route('admin.visits.update',$visit) }}" method="POST">@csrf @method('PUT')
    <div class="row g-2">
      <div class="col-md-4"><label class="form-label small fw-semibold">Tanggal</label><input type="date" name="visit_date" class="form-control form-control-sm" value="{{ $visit->visit_date->format('Y-m-d') }}" required></div>
      <div class="col-md-4"><label class="form-label small fw-semibold">Jam Mulai</label><input type="time" name="visit_time_start" class="form-control form-control-sm" value="{{ $visit->visit_time_start }}"></div>
      <div class="col-md-4"><label class="form-label small fw-semibold">Jam Selesai</label><input type="time" name="visit_time_end" class="form-control form-control-sm" value="{{ $visit->visit_time_end }}"></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">{{ $visit->notes }}</textarea></div>
      <div class="col-12 d-flex justify-content-end"><button type="submit" class="btn btn-secondary btn-sm"><i class="fas fa-save me-1"></i>Simpan</button></div>
    </div>
    </form>
  </div>
</div>
</div></div>
@endsection
