@extends('layouts.admin')
@section('content')
<div class="container">
  <h4 class="mb-3">Dashboard Kunjungan</h4>

  <form class="row g-2 mb-3" method="GET">
    <div class="col-auto">
      <input type="date" name="from" class="form-control" value="{{ request('from', now()->toDateString()) }}">
    </div>
    <div class="col-auto">
      <input type="date" name="to" class="form-control" value="{{ request('to', now()->addDays(7)->toDateString()) }}">
    </div>
    <div class="col-auto">
      <select name="technician_id" class="form-select">
        <option value="">-- Semua Teknisi --</option>
        @foreach($technicians ?? [] as $t)
          <option value="{{ $t->id }}" {{ request('technician_id') == $t->id ? 'selected' : '' }}>{{ $t->name }}</option>
        @endforeach
      </select>
    </div>
    <div class="col-auto">
      <button class="btn btn-primary">Filter</button>
    </div>
  </form>

  <div class="row mb-3">
    <div class="col-auto"><div class="p-3 bg-light rounded">Total kunjungan: <strong>{{ $total ?? 0 }}</strong></div></div>
    <div class="col-auto"><div class="p-3 bg-light rounded">Prepared: <strong>{{ $prepared ?? 0 }}</strong></div></div>
    <div class="col-auto"><div class="p-3 bg-light rounded">Completed: <strong>{{ $completed ?? 0 }}</strong></div></div>
    <div class="col-auto"><div class="p-3 {{ $missingPreparationCount > 0 ? 'bg-warning text-dark' : 'bg-success text-white' }} rounded">Belum preparation: <strong>{{ $missingPreparationCount ?? 0 }}</strong></div></div>
  </div>
  @if(!empty($missingPreparationCount) && $missingPreparationCount > 0)
    <div class="alert alert-warning">
      <strong>{{ $missingPreparationCount }} kunjungan belum dipersiapkan.</strong> Pastikan H-1 preparation diisi.
    </div>
  @endif

  @if($visits->isEmpty())
    <div class="alert alert-info">Tidak ada kunjungan pada rentang tanggal ini.</div>
  @else
    <table class="table table-sm">
      <thead>
        <tr><th>#</th><th>Tanggal</th><th>Jam</th><th>Klien</th><th>Teknisi</th><th>Status</th><th>Preparation</th><th>Aksi</th></tr>
      </thead>
      <tbody>
      @foreach($visits as $v)
        <tr>
          <td>{{ $v->id }}</td>
          <td>{{ $v->visit_date->format('d/m/Y') }}</td>
          <td>{{ $v->visit_time_start ?? '-' }}</td>
          <td>{{ $v->client?->name ?? '-' }}</td>
          <td>{{ $v->technician?->name ?? '-' }}</td>
          <td>{{ ucfirst($v->status) }}</td>
          <td>
            @if(isset($preps[$v->id]))
              <span class="badge bg-success">Prepared</span>
            @else
              <a href="{{ route('admin.visits.preparation.form', $v) }}" class="btn btn-sm btn-outline-primary">Prepare</a>
            @endif
          </td>
          <td>
            <a class="btn btn-sm btn-outline-secondary" href="{{ route('admin.visits.checkin.form', $v) }}">Check-in</a>
            <a class="btn btn-sm btn-outline-secondary" href="{{ route('admin.visits.work.form', $v) }}">Work</a>
            <a class="btn btn-sm btn-outline-success" href="{{ route('admin.visits.prepulang.form', $v) }}">Pre-Pulang</a>
          </td>
        </tr>
      @endforeach
      </tbody>
    </table>
  @endif
</div>
@endsection
