{{-- visits/create.blade.php --}}
@extends('layouts.admin')
@section('title','Jadwalkan Kunjungan')
@section('content')
<div class="row justify-content-center"><div class="col-lg-7">
<div class="card">
  <div class="card-header">Form Jadwal Kunjungan</div>
  <div class="card-body">
    @if($selectedClient && $selectedClient->type==='maintenance')
    <div class="alert alert-info small">
      <i class="fas fa-info-circle me-1"></i>
      Kunjungan bulan ini: <strong>{{ $selectedClient->getVisitsThisMonth() }}</strong> / {{ $selectedClient->max_visits_per_month }}
      — Sisa: <strong>{{ $selectedClient->getRemainingVisits() }}</strong>
      @if($selectedClient->getRemainingVisits()===0 && auth()->user()->role !== 'technician')<br><span class="text-warning fw-semibold">⚠️ Kunjungan ini akan dihitung sebagai ekstra (Rp {{ number_format($selectedClient->extra_visit_fee,0,',','.') }})</span>@endif
    </div>
    @endif
    <form action="{{ route('admin.visits.store') }}" method="POST">@csrf
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label fw-semibold">Klien *</label>
        <select id="clientSelect" name="client_id" class="form-select" required>
          <option value="">-- Pilih Klien --</option>
          @foreach($clients as $c)<option value="{{ $c->id }}" {{ (old('client_id', $selectedClient?->id)==$c->id)?'selected':'' }}>{{ $c->name }}</option>@endforeach
        </select>
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">Teknisi</label>
        <select name="technician_id" class="form-select">
          <option value="">-- Belum ditentukan --</option>
          @foreach($technicians as $t)<option value="{{ $t->id }}" {{ old('technician_id') == $t->id ? 'selected' : '' }}>{{ $t->name }}</option>@endforeach
        </select>
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Tanggal *</label>
        <input type="date" name="visit_date" class="form-control" value="{{ old('visit_date',now()->addDay()->format('Y-m-d')) }}" required>
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Jam Mulai</label>
        <input type="time" name="visit_time_start" class="form-control" value="{{ old('visit_time_start','09:00') }}">
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Jam Selesai</label>
        <input type="time" name="visit_time_end" class="form-control" value="{{ old('visit_time_end','17:00') }}">
      </div>
      <div class="col-12">
        <label class="form-label fw-semibold">Tiket Terkait</label>
        @if($selectedClient)
          @if($clientTickets->isNotEmpty())
            <select name="tickets[]" class="form-select" multiple size="5">
              @foreach($clientTickets as $ticket)
                <option value="{{ $ticket->id }}" {{ in_array($ticket->id, old('tickets', $selectedTicket?->id ? [$selectedTicket->id] : [])) ? 'selected' : '' }}>
                  {{ $ticket->ticket_number }} — {{ $ticket->subject }}
                </option>
              @endforeach
            </select>
            <div class="form-text">Tekan Ctrl/Cmd untuk memilih lebih dari satu tiket.</div>
          @else
            <div class="form-control-plaintext small text-muted">
              Klien ini belum memiliki tiket. <a href="{{ route('admin.tickets.create',['client_id'=>$selectedClient->id]) }}">Buat tiket baru</a> dulu, lalu kembali ke form kunjungan.
            </div>
          @endif
        @else
          <div class="form-control-plaintext small text-muted">
            Pilih klien terlebih dahulu untuk menampilkan tiket terkait. Jika tiket sudah dibuat, Anda bisa memilihnya di sini.
          </div>
        @endif
      </div>
      <div class="col-12">
        <label class="form-label fw-semibold">Tujuan Kunjungan</label>
        <textarea name="purpose" class="form-control" rows="3" placeholder="Jelaskan tujuan kunjungan...">{{ old('purpose') }}</textarea>
      </div>
      <div class="col-12">
        <label class="form-label fw-semibold">Catatan</label>
        <textarea name="notes" class="form-control" rows="2">{{ old('notes') }}</textarea>
      </div>
      <div class="col-12 d-flex gap-2 justify-content-end">
        <a href="{{ route('admin.visits.calendar') }}" class="btn btn-secondary">Batal</a>
        <button type="submit" class="btn btn-primary"><i class="fas fa-calendar-check me-1"></i>Jadwalkan</button>
      </div>
    </div>
    </form>
  </div>
</div>
</div></div>
<script>
  document.querySelector('select[name="client_id"]').addEventListener('change', function() {
    const clientId = this.value;
    const params = new URLSearchParams(window.location.search);
    if (clientId) {
      params.set('client_id', clientId);
    } else {
      params.delete('client_id');
    }
    params.delete('ticket_id');
    window.location.search = params.toString();
  });
</script>
@endsection
