@extends('layouts.admin')
@section('title', isset($client) ? 'Edit Klien' : 'Tambah Klien')
@section('content')
<div class="row justify-content-center">
<div class="col-lg-9">
<div class="card">
  <div class="card-header">{{ isset($client) ? 'Edit Klien: '.$client->name : 'Tambah Klien Baru' }}</div>
  <div class="card-body">
    <form action="{{ isset($client) ? route('admin.clients.update',$client) : route('admin.clients.store') }}" method="POST">
    @csrf
    @if(isset($client)) @method('PUT') @endif
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label fw-semibold">Nama / Perusahaan <span class="text-danger">*</span></label>
        <input type="text" name="name" class="form-control" value="{{ old('name',$client->name??'') }}" required>
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">Nama Perusahaan (opsional)</label>
        <input type="text" name="company_name" class="form-control" value="{{ old('company_name',$client->company_name??'') }}">
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Tipe Klien <span class="text-danger">*</span></label>
        <select name="type" class="form-select" required id="clientType" onchange="toggleMaintenance(this.value)">
          @foreach(['maintenance'=>'Maintenance','non_maintenance'=>'Non-Maintenance','individual'=>'Perorangan'] as $v=>$l)
          <option value="{{ $v }}" {{ old('type',$client->type??'')===  $v?'selected':'' }}>{{ $l }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Divisi <span class="text-danger">*</span></label>
        <select name="division" class="form-select" required>
          @foreach(['JPN','FixLab','Havva'] as $d)
          <option value="{{ $d }}" {{ old('division',$client->division??'JPN')===$d?'selected':'' }}>{{ $d }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-4">
        <label class="form-label fw-semibold">Status</label>
        <select name="is_active" class="form-select">
          <option value="1" {{ old('is_active',$client->is_active??1)?'selected':'' }}>Aktif</option>
          <option value="0" {{ !old('is_active',$client->is_active??1)?'selected':'' }}>Nonaktif</option>
        </select>
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">Email</label>
        <input type="email" name="email" class="form-control" value="{{ old('email',$client->email??'') }}">
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">No. Telepon</label>
        <input type="text" name="phone" class="form-control" value="{{ old('phone',$client->phone??'') }}">
      </div>
      <div class="col-12">
        <label class="form-label fw-semibold">Alamat</label>
        <textarea name="address" class="form-control" rows="2">{{ old('address',$client->address??'') }}</textarea>
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">Nama PIC</label>
        <input type="text" name="pic_name" class="form-control" value="{{ old('pic_name',$client->pic_name??'') }}">
      </div>
      <div class="col-md-6">
        <label class="form-label fw-semibold">No. HP PIC</label>
        <input type="text" name="pic_phone" class="form-control" value="{{ old('pic_phone',$client->pic_phone??'') }}">
      </div>

      {{-- Maintenance fields --}}
      <div id="maintenanceFields" class="col-12">
        <hr><h6 class="fw-bold mb-3 text-primary"><i class="fas fa-file-contract me-2"></i>Konfigurasi Maintenance</h6>
        <div class="row g-3">
          <div class="col-md-4">
            <label class="form-label fw-semibold">Max Kunjungan/Bulan</label>
            <input type="number" name="max_visits_per_month" class="form-control" min="0" value="{{ old('max_visits_per_month',$client->max_visits_per_month??0) }}">
          </div>
          <div class="col-md-4">
            <label class="form-label fw-semibold">Biaya Maintenance/Bulan (Rp)</label>
            <input type="number" name="monthly_fee" class="form-control" min="0" value="{{ old('monthly_fee',$client->monthly_fee??0) }}">
          </div>
          <div class="col-md-4">
            <label class="form-label fw-semibold">Biaya Kunjungan Ekstra (Rp)</label>
            <input type="number" name="extra_visit_fee" class="form-control" min="0" value="{{ old('extra_visit_fee',$client->extra_visit_fee??0) }}">
          </div>
          <div class="col-md-4">
            <label class="form-label fw-semibold">Mulai Kontrak</label>
            <input type="date" name="contract_start" class="form-control" value="{{ old('contract_start',$client?->contract_start?->format('Y-m-d') ?? '') }}">
          </div>
          <div class="col-md-4">
            <label class="form-label fw-semibold">Akhir Kontrak</label>
            <input type="date" name="contract_end" class="form-control" value="{{ old('contract_end',$client?->contract_end?->format('Y-m-d') ?? '') }}">
          </div>
          <div class="col-md-4 d-flex align-items-end">
            <div class="form-check form-switch">
              <input class="form-check-input" type="checkbox" name="sla_enabled" value="1" id="slaEnabled" {{ old('sla_enabled',$client->sla_enabled??false)?'checked':'' }}>
              <label class="form-check-label fw-semibold" for="slaEnabled">Aktifkan SLA</label>
            </div>
          </div>
        </div>
      </div>

      <div class="col-12">
        <label class="form-label fw-semibold">Catatan</label>
        <textarea name="notes" class="form-control" rows="2">{{ old('notes',$client->notes??'') }}</textarea>
      </div>
      <div class="col-12 d-flex gap-2 justify-content-end">
        <a href="{{ route('admin.clients.index') }}" class="btn btn-secondary">Batal</a>
        <button type="submit" class="btn btn-primary"><i class="fas fa-save me-1"></i>Simpan</button>
      </div>
    </div>
    </form>
  </div>
</div>
</div></div>
@endsection
@push('scripts')
<script>
function toggleMaintenance(type) {
  document.getElementById('maintenanceFields').style.display = type === 'maintenance' ? '' : 'none';
}
toggleMaintenance(document.getElementById('clientType').value);
</script>
@endpush
