@extends('agency.layouts.app')

@section('title', 'Yeni Müşteri')
@section('page_title', 'Yeni Müşteri Ekle')

@push('styles')
<style>
.form-page {
    max-width: 880px;
    margin: 0 auto;
}
.form-card {
    background: #fff; border-radius: 20px;
    border: 1px solid #f1f5f9;
    box-shadow: 0 2px 12px rgba(0,0,0,.05);
    overflow: hidden; margin-bottom: 1.25rem;
}
.form-section-header {
    display: flex; align-items: center; gap: .75rem;
    padding: 1.1rem 1.5rem;
    border-bottom: 1px solid #f1f5f9;
    background: #fafbff;
}
.form-section-icon {
    width: 36px; height: 36px; border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    font-size: 15px; flex-shrink: 0;
}
.form-section-title { font-size: .9rem; font-weight: 700; color: #0f172a; }
.form-section-body { padding: 1.5rem; }

/* Field */
.field-group { margin-bottom: 1.25rem; }
.field-label {
    display: block; font-size: .78rem; font-weight: 700;
    color: #64748b; text-transform: uppercase; letter-spacing: .4px;
    margin-bottom: .4rem;
}
.field-label .req { color: #ef4444; margin-left: 2px; }
.field-input {
    width: 100%; border: 1.5px solid #e2e8f0; border-radius: 10px;
    padding: .65rem .9rem; font-size: .875rem; color: #0f172a;
    background: #f8fafc; transition: all .2s;
    height: 44px;
}
.field-input:focus {
    outline: none; border-color: #6366f1;
    box-shadow: 0 0 0 3px rgba(99,102,241,.1);
    background: #fff;
}
textarea.field-input { height: auto; resize: vertical; }
select.field-input { cursor: pointer; }
.field-help { font-size: .75rem; color: #94a3b8; margin-top: .3rem; }

/* Grid */
.fg-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.fg-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 1rem; }
@media(max-width:640px) { .fg-2,.fg-3 { grid-template-columns: 1fr; } }

/* Toggle switch */
.toggle-row {
    display: flex; align-items: center; justify-content: space-between;
    padding: .75rem 1rem; border-radius: 10px; background: #f8fafc;
    border: 1px solid #f1f5f9; margin-bottom: .5rem;
}
.toggle-info .tl { font-size:.85rem;font-weight:600;color:#0f172a; }
.toggle-info .td { font-size:.75rem;color:#94a3b8; }
.toggle-switch { position:relative;display:inline-block;width:44px;height:24px;flex-shrink:0 }
.toggle-switch input { opacity:0;width:0;height:0 }
.toggle-slider {
    position:absolute;cursor:pointer;top:0;left:0;right:0;bottom:0;
    background:#e2e8f0;border-radius:999px;transition:.3s;
}
.toggle-slider:before {
    content:'';position:absolute;width:18px;height:18px;border-radius:50%;
    left:3px;bottom:3px;background:#fff;transition:.3s;
    box-shadow:0 1px 4px rgba(0,0,0,.2);
}
input:checked+.toggle-slider { background:#6366f1; }
input:checked+.toggle-slider:before { transform:translateX(20px); }

/* Sticky footer */
.form-footer {
    position: sticky; bottom: 0; z-index: 10;
    background: #fff; border-top: 1px solid #f1f5f9;
    padding: 1rem 1.5rem; display: flex; justify-content: space-between;
    align-items: center; border-radius: 0 0 20px 20px;
    box-shadow: 0 -4px 20px rgba(0,0,0,.05);
}

.field-error { border-color: #ef4444 !important; background:#fff5f5 !important; }
.error-msg { font-size:.75rem;color:#ef4444;margin-top:.25rem; }

/* Email dynamic list */
.email-list { display: flex; flex-direction: column; gap: .5rem; }
.email-item { display: flex; gap: .5rem; align-items: center; }
.btn-add-email { 
    background: #eef2ff; color: #6366f1; border: none; 
    width: 38px; height: 38px; border-radius: 8px; 
    cursor: pointer; transition: all .2s;
    display: flex; align-items: center; justify-content: center;
}
.btn-add-email:hover { background: #6366f1; color: #fff; }
.btn-remove-email {
    background: #fff1f2; color: #ef4444; border: none;
    width: 38px; height: 38px; border-radius: 8px;
    cursor: pointer; transition: all .2s;
    display: flex; align-items: center; justify-content: center;
}
.btn-remove-email:hover { background: #ef4444; color: #fff; }
</style>
@endpush

@section('content')
<div class="container-fluid">
<div class="form-page">

{{-- Back link --}}
<div class="d-flex align-items-center gap-2 mb-3" style="font-size:.83rem;color:#94a3b8">
    <a href="{{ route('agency.customers.index') }}" style="color:#6366f1;text-decoration:none;font-weight:600">
        <i class="fas fa-chevron-left me-1"></i>Müşteriler
    </a>
    <i class="fas fa-chevron-right" style="font-size:10px"></i>
    <span style="color:#0f172a;font-weight:600">Yeni Müşteri</span>
</div>

@if(session('success'))
    <div class="alert alert-success border-0 shadow-sm" style="border-radius:12px; font-size:.85rem">
        <i class="fas fa-check-circle me-2"></i>{{ session('success') }}
    </div>
@endif

@if(session('error'))
    <div class="alert alert-danger border-0 shadow-sm" style="border-radius:12px; font-size:.85rem">
        <i class="fas fa-exclamation-circle me-2"></i>{{ session('error') }}
    </div>
@endif

@if($errors->any())
    <div class="alert alert-danger border-0 shadow-sm" style="border-radius:12px; font-size:.85rem">
        <ul class="mb-0">
            @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

<form method="POST" action="{{ route('agency.customers.store') }}" id="customerForm">
@csrf

{{-- ─── TEMEL BİLGİLER ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#eef2ff;color:#6366f1"><i class="fas fa-user"></i></div>
        <div>
            <div class="form-section-title">Temel Bilgiler</div>
        </div>
        {{-- Type selector --}}
        <div class="ms-auto d-flex rounded-pill overflow-hidden" style="border:1.5px solid #e2e8f0">
            <label style="cursor:pointer">
                <input type="radio" name="customer_type" value="individual" {{ old('customer_type','individual') === 'individual' ? 'checked' : '' }} class="d-none type-radio">
                <span class="type-pill" data-val="individual">👤 Kişi</span>
            </label>
            <label style="cursor:pointer">
                <input type="radio" name="customer_type" value="corporate" {{ old('customer_type') === 'corporate' ? 'checked' : '' }} class="d-none type-radio">
                <span class="type-pill" data-val="corporate">🏢 Şirket</span>
            </label>
        </div>
    </div>
    <div class="form-section-body">
        {{-- Kişi adı (individual) --}}
        <div id="personNameFields" class="fg-2">
            <div class="field-group">
                <label class="field-label">Ad<span class="req">*</span></label>
                <input type="text" name="first_name" value="{{ old('first_name') }}"
                       class="field-input {{ $errors->has('first_name') ? 'field-error' : '' }}" placeholder="Ad">
                @error('first_name') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
            <div class="field-group">
                <label class="field-label">Soyad<span class="req">*</span></label>
                <input type="text" name="last_name" value="{{ old('last_name') }}"
                       class="field-input {{ $errors->has('last_name') ? 'field-error' : '' }}" placeholder="Soyad">
                @error('last_name') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
        </div>

        {{-- Firma adı (corporate / şirket kartı) --}}
        <div id="companyNameField" class="field-group" style="display:none">
            <label class="field-label">Firma Adı<span class="req">*</span></label>
            <input type="text" name="company_name" id="company_name" value="{{ old('company_name') }}"
                   class="field-input {{ $errors->has('company_name') ? 'field-error' : '' }}" placeholder="Firma / Şirket Adı">
            @error('company_name') <div class="error-msg">{{ $message }}</div> @enderror
        </div>

        <div class="fg-2">
            <div class="field-group">
                <label class="field-label">E-posta Adresleri</label>
                <div class="email-list" id="emailList">
                    @php $oldEmails = old('emails', ['']); @endphp
                    @foreach($oldEmails as $index => $oldEmail)
                        <div class="email-item">
                            <input type="email" name="emails[]" value="{{ $oldEmail }}" class="field-input" placeholder="ornek@mail.com">
                            @if($loop->first)
                                <button type="button" class="btn-add-email" id="btnAddEmail"><i class="fas fa-plus"></i></button>
                            @else
                                <button type="button" class="btn-remove-email remove-email"><i class="fas fa-trash"></i></button>
                            @endif
                        </div>
                    @endforeach
                </div>
                @error('emails.*') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
            <div class="field-group">
                <label class="field-label">Telefon</label>
                <input type="text" name="phone" value="{{ old('phone') }}" class="field-input" placeholder="+90 5xx xxx xx xx">
            </div>
        </div>

        <div class="fg-3">
            <div class="field-group">
                <label class="field-label">Alt Telefon</label>
                <input type="text" name="phone_alt" value="{{ old('phone_alt') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Doğum Tarihi</label>
                <input type="date" name="birth_date" value="{{ old('birth_date') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Cinsiyet</label>
                <select name="gender" class="field-input">
                    <option value="">Seç</option>
                    <option value="male"   {{ old('gender') === 'male'   ? 'selected' : '' }}>Erkek</option>
                    <option value="female" {{ old('gender') === 'female' ? 'selected' : '' }}>Kadın</option>
                    <option value="other"  {{ old('gender') === 'other'  ? 'selected' : '' }}>Diğer</option>
                </select>
            </div>
        </div>

        {{-- Şirket kartı bilgileri (customer_type = corporate) --}}
        <div id="corporateFields" style="display:none">
            <hr style="border-color:#f1f5f9;margin:1rem 0">
            <div class="fg-3">
                <div class="field-group">
                    <label class="field-label">Şirket Türü</label>
                    <select name="company_type" id="company_type" class="field-input">
                        <option value="">Seçiniz</option>
                        @foreach(\App\Models\Customer\Customer::companyTypes() as $val => $label)
                            <option value="{{ $val }}" {{ old('company_type') === $val ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="field-group">
                    <label class="field-label">Vergi No</label>
                    <input type="text" name="company_tax_number" id="company_tax_number" value="{{ old('company_tax_number') }}" class="field-input">
                </div>
                <div class="field-group">
                    <label class="field-label">Vergi Dairesi</label>
                    <input type="text" name="company_tax_office" id="company_tax_office" value="{{ old('company_tax_office') }}" class="field-input">
                </div>
            </div>
        </div>

        {{-- Kişinin bağlı olduğu şirketler (customer_type = individual) --}}
        <div id="individualCompanyFields" style="display:none">
            <hr style="border-color:#f1f5f9;margin:1rem 0">
            <div class="fg-2">
                <div class="field-group">
                    <label class="field-label">Bağlı Şirketler</label>
                    @include('agency.customers.partials.company-multiselect', [
                        'companies' => $companies,
                        'selectedCompanyIds' => array_map('strval', (array) old('company_ids', [])),
                    ])
                    <div class="field-help">Bir kişi birden fazla şirkete bağlı olabilir.</div>
                </div>
                <div class="field-group">
                    <label class="field-label">Unvan / Pozisyon</label>
                    <input type="text" name="company_position" value="{{ old('company_position') }}" class="field-input" placeholder="CEO, Direktör...">
                </div>
            </div>
        </div>
    </div>
</div>

{{-- ─── FATURA BİLGİLERİ ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#e0f2fe;color:#0284c7"><i class="fas fa-file-invoice"></i></div>
        <div class="form-section-title">Fatura Bilgileri</div>
    </div>
    <div class="form-section-body">
        <div class="field-group">
            <label class="field-label">Fatura Adresi<span class="req">*</span></label>
            <textarea name="billing_address" class="field-input {{ $errors->has('billing_address') ? 'field-error' : '' }}" rows="2" placeholder="Fatura Adresi">{{ old('billing_address') }}</textarea>
            @error('billing_address') <div class="error-msg">{{ $message }}</div> @enderror
        </div>

        <div class="fg-3">
            <div class="field-group">
                <label class="field-label">Fatura İli</label>
                <input type="text" name="billing_city" value="{{ old('billing_city') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Fatura İlçe</label>
                <input type="text" name="billing_district" value="{{ old('billing_district') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Fatura E-posta<span class="req">*</span></label>
                <input type="email" name="billing_email" value="{{ old('billing_email') }}" class="field-input {{ $errors->has('billing_email') ? 'field-error' : '' }}" placeholder="fatura@mail.com">
                @error('billing_email') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
        </div>

        <div class="fg-2 mt-2">
            <div class="field-group">
                <label class="field-label" id="taxNumberLabel">T.C. Kimlik Numarası / Vergi No<span class="req">*</span></label>
                <input type="text" name="billing_tax_number" value="{{ old('billing_tax_number') }}" class="field-input {{ $errors->has('billing_tax_number') ? 'field-error' : '' }}">
                @error('billing_tax_number') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
            <div class="field-group" id="taxOfficeField" style="display:none">
                <label class="field-label">Fatura Vergi Dairesi<span class="req">*</span></label>
                <input type="text" name="billing_tax_office" value="{{ old('billing_tax_office') }}" class="field-input {{ $errors->has('billing_tax_office') ? 'field-error' : '' }}">
                @error('billing_tax_office') <div class="error-msg">{{ $message }}</div> @enderror
            </div>
        </div>
    </div>
</div>

{{-- ─── DURUM & SINIFLANDIRMA ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#fef3c7;color:#f59e0b"><i class="fas fa-tag"></i></div>
        <div class="form-section-title">Durum & Sınıflandırma</div>
    </div>
    <div class="form-section-body">
        <div class="fg-3">
            <div class="field-group">
                <label class="field-label">Durum<span class="req">*</span></label>
                <select name="status" class="field-input">
                    <option value="active"    {{ old('status','active') === 'active'    ? 'selected' : '' }}>✅ Aktif</option>
                    <option value="potential" {{ old('status') === 'potential' ? 'selected' : '' }}>🔔 Potansiyel</option>
                    <option value="inactive"  {{ old('status') === 'inactive'  ? 'selected' : '' }}>⏸ Pasif</option>
                    <option value="blocked"   {{ old('status') === 'blocked'   ? 'selected' : '' }}>🚫 Engellenmiş</option>
                </select>
            </div>
            <div class="field-group">
                <label class="field-label">Öncelik</label>
                <select name="priority" class="field-input">
                    <option value="1" {{ old('priority') == '1' ? 'selected' : '' }}>⭐ Öncelikli</option>
                    <option value="0" {{ old('priority', '0') == '0' ? 'selected' : '' }}>⚪ Normal</option>
                </select>
            </div>
            <div class="field-group">
                <label class="field-label">Temsilci</label>
                <select name="assigned_agent_id" class="field-input">
                    <option value="">Atanmamış</option>
                    @foreach($agents as $agent)
                        <option value="{{ $agent->id }}" {{ old('assigned_agent_id') == $agent->id ? 'selected' : '' }}>
                            {{ $agent->full_name }}
                        </option>
                    @endforeach
                </select>
            </div>
        </div>

        <div class="fg-2">
            <div class="field-group">
                <label class="field-label">Referans Kaynağı</label>
                <input type="text" name="referral_source" value="{{ old('referral_source') }}" class="field-input" placeholder="Instagram, Referans, Web...">
            </div>
            <div class="field-group">
                <label class="field-label">Tercih İletişim Yöntemi</label>
                <select name="preferred_contact_method" class="field-input">
                    <option value="">Seç</option>
                    <option value="phone"    {{ old('preferred_contact_method') === 'phone'    ? 'selected' : '' }}>📞 Telefon</option>
                    <option value="email"    {{ old('preferred_contact_method') === 'email'    ? 'selected' : '' }}>📧 E-posta</option>
                    <option value="sms"      {{ old('preferred_contact_method') === 'sms'      ? 'selected' : '' }}>💬 SMS</option>
                </select>
            </div>
        </div>
        <div class="fg-2">
            <div class="field-group">
                <label class="field-label">Etiketler</label>
                <input type="text" name="tags" value="{{ old('tags') }}" class="field-input" placeholder="virgül ile ayırın (örn: gezi, kültür)">
            </div>
            <div class="field-group">
                <label class="field-label">İlgi Alanları</label>
                <input type="text" name="interests" value="{{ old('interests') }}" class="field-input" placeholder="virgül ile ayırın (örn: tarih, doğa)">
            </div>
        </div>
    </div>
</div>

{{-- ─── KİMLİK & ADRES ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#d1fae5;color:#10b981"><i class="fas fa-id-card"></i></div>
        <div class="form-section-title">Kimlik & Adres</div>
    </div>
    <div class="form-section-body">
        <div class="fg-3">
            <div class="field-group">
                <label class="field-label">TC / Kimlik No</label>
                <input type="text" name="national_id" value="{{ old('national_id') }}" class="field-input" maxlength="20">
            </div>
            <div class="field-group">
                <label class="field-label">Pasaport No</label>
                <input type="text" name="passport_number" value="{{ old('passport_number') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Pasaport Geçerlilik</label>
                <input type="date" name="passport_expiry" value="{{ old('passport_expiry') }}" class="field-input">
            </div>
        </div>
        <div class="fg-3">
            <div class="field-group">
                <label class="field-label">Uyruk (ISO)</label>
                <input type="text" name="nationality" value="{{ old('nationality') }}" class="field-input" maxlength="2" placeholder="TR, DE, GB...">
            </div>
            <div class="field-group">
                <label class="field-label">Şehir</label>
                <input type="text" name="city" value="{{ old('city') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">İlçe</label>
                <input type="text" name="district" value="{{ old('district') }}" class="field-input">
            </div>
        </div>
        <div class="fg-2">
            <div class="field-group">
                <label class="field-label">Adres</label>
                <input type="text" name="address" value="{{ old('address') }}" class="field-input">
            </div>
            <div class="field-group">
                <label class="field-label">Posta Kodu</label>
                <input type="text" name="postal_code" value="{{ old('postal_code') }}" class="field-input" maxlength="10">
            </div>
        </div>
    </div>
</div>

{{-- ─── PAZARLAMA İZİNLERİ ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#ede9fe;color:#8b5cf6"><i class="fas fa-bullhorn"></i></div>
        <div class="form-section-title">İletişim İzinleri</div>
    </div>
    <div class="form-section-body">
        @foreach([
            ['name'=>'email_marketing','label'=>'E-posta Pazarlama','desc'=>'Kampanya ve fırsat e-postaları gönderilebilir'],
            ['name'=>'sms_marketing','label'=>'SMS Pazarlama','desc'=>'Bildirim ve promosyon SMS\'leri gönderilebilir'],
        ] as $perm)
        <div class="toggle-row">
            <div class="toggle-info">
                <div class="tl">{{ $perm['label'] }}</div>
                <div class="td">{{ $perm['desc'] }}</div>
            </div>
            <label class="toggle-switch">
                <input type="hidden" name="{{ $perm['name'] }}" value="0">
                <input type="checkbox" name="{{ $perm['name'] }}" value="1"
                       {{ old($perm['name'], 1) ? 'checked' : '' }}>
                <span class="toggle-slider"></span>
            </label>
        </div>
        @endforeach
    </div>
</div>

{{-- ─── ÖZEL ALANLAR ─── --}}
@if($definitions->isNotEmpty())
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#f1f5f9;color:#475569"><i class="fas fa-plus-square"></i></div>
        <div class="form-section-title">Ek Bilgiler (Özel Alanlar)</div>
    </div>
    <div class="form-section-body">
        <div class="fg-2">
            @foreach($definitions as $def)
                <div class="field-group">
                    <label class="field-label">
                        {{ $def->label }}
                        @if($def->is_required) <span class="req">*</span> @endif
                    </label>

                    @if($def->field_type === 'select')
                        <select name="custom_fields[{{ $def->id }}]" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}">
                            <option value="">Seçiniz</option>
                            @foreach($def->options ?? [] as $opt)
                                <option value="{{ $opt['value'] }}" {{ old('custom_fields.'.$def->id) == $opt['value'] ? 'selected' : '' }}>{{ $opt['label'] }}</option>
                            @endforeach
                        </select>
                    @elseif($def->field_type === 'multiselect')
                        <select name="custom_fields[{{ $def->id }}][]" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}" multiple style="height:auto">
                            @foreach($def->options ?? [] as $opt)
                                <option value="{{ $opt['value'] }}" {{ is_array(old('custom_fields.'.$def->id)) && in_array($opt['value'], old('custom_fields.'.$def->id)) ? 'selected' : '' }}>{{ $opt['label'] }}</option>
                            @endforeach
                        </select>
                    @elseif($def->field_type === 'boolean')
                        <select name="custom_fields[{{ $def->id }}]" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}">
                            <option value="">Seçiniz</option>
                            <option value="1" {{ old('custom_fields.'.$def->id) == '1' ? 'selected' : '' }}>Evet</option>
                            <option value="0" {{ old('custom_fields.'.$def->id) == '0' ? 'selected' : '' }}>Hayır</option>
                        </select>
                    @elseif($def->field_type === 'textarea')
                        <textarea name="custom_fields[{{ $def->id }}]" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}" rows="3">{{ old('custom_fields.'.$def->id) }}</textarea>
                    @elseif($def->field_type === 'date')
                        <input type="date" name="custom_fields[{{ $def->id }}]" value="{{ old('custom_fields.'.$def->id) }}" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}">
                    @elseif($def->field_type === 'number' || $def->field_type === 'decimal')
                        <input type="number" step="any" name="custom_fields[{{ $def->id }}]" value="{{ old('custom_fields.'.$def->id) }}" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}">
                    @else
                        <input type="text" name="custom_fields[{{ $def->id }}]" value="{{ old('custom_fields.'.$def->id) }}" class="field-input {{ $errors->has('custom_fields.'.$def->id) ? 'field-error' : '' }}">
                    @endif

                    @error('custom_fields.'.$def->id)
                        <div class="error-msg">Bu alan zorunludur.</div>
                    @enderror

                    @if($def->help_text)
                        <div class="field-help">{{ $def->help_text }}</div>
                    @endif
                </div>
            @endforeach
        </div>
    </div>
</div>
@endif

{{-- ─── NOT ─── --}}
<div class="form-card">
    <div class="form-section-header">
        <div class="form-section-icon" style="background:#fef3c7;color:#f59e0b"><i class="fas fa-sticky-note"></i></div>
        <div class="form-section-title">Dahili Notlar</div>
    </div>
    <div class="form-section-body">
        <textarea name="internal_notes" class="field-input" rows="4" placeholder="Bu müşteri hakkında dahili notlar...">{{ old('internal_notes') }}</textarea>
        <div class="field-help">Bu notlar yalnızca acente personelinize görünür.</div>
    </div>
</div>

{{-- ─── FOOTER ─── --}}
<div class="form-footer">
    <a href="{{ route('agency.customers.index') }}" class="btn btn-outline-secondary" style="border-radius:10px">
        <i class="fas fa-times me-1"></i>İptal
    </a>
    <button type="submit" class="btn text-white" style="background:linear-gradient(135deg,#6366f1,#8b5cf6);border-radius:10px;padding:.65rem 2rem;font-weight:600">
        <i class="fas fa-check me-1"></i>Müşteriyi Kaydet
    </button>
</div>

</form>
</div>
</div>
@endsection

@push('styles')
<style>
.type-pill {
    display: block; padding: .45rem 1rem; font-size: .82rem; font-weight: 600;
    color: #64748b; background: transparent; transition: all .2s;
    cursor: pointer;
}
input[type=radio]:checked + .type-pill {
    background: #6366f1; color: #fff;
}
</style>
@endpush

@push('scripts')
<script>
// Customer type toggle — Kişi (individual) / Şirket (corporate)
function toggleCustomerType(type) {
    const isCorporate = type === 'corporate';

    const personNameFields = document.getElementById('personNameFields');
    const companyNameField = document.getElementById('companyNameField');
    const corporateFields = document.getElementById('corporateFields');
    const individualCompanyFields = document.getElementById('individualCompanyFields');

    if (personNameFields) personNameFields.style.display = isCorporate ? 'none' : '';
    if (companyNameField) companyNameField.style.display = isCorporate ? '' : 'none';
    if (corporateFields) corporateFields.style.display = isCorporate ? '' : 'none';
    if (individualCompanyFields) individualCompanyFields.style.display = isCorporate ? 'none' : '';

    const taxNumberLabel = document.getElementById('taxNumberLabel');
    const taxOfficeField = document.getElementById('taxOfficeField');

    if (isCorporate) {
        if (taxNumberLabel) taxNumberLabel.innerHTML = 'Vergi Numarası<span class="req">*</span>';
        if (taxOfficeField) taxOfficeField.style.display = '';
    } else {
        if (taxNumberLabel) taxNumberLabel.innerHTML = 'T.C. Kimlik Numarası<span class="req">*</span>';
        if (taxOfficeField) taxOfficeField.style.display = 'none';
    }
}

document.querySelectorAll('.type-radio').forEach(r => {
    r.addEventListener('change', function() {
        toggleCustomerType(this.value);
    });
});

document.addEventListener('DOMContentLoaded', function() {
    const activeRadio = document.querySelector('.type-radio:checked');
    toggleCustomerType(activeRadio ? activeRadio.value : 'individual');
});

// Dynamic Email List
const emailList = document.getElementById('emailList');
const btnAddEmail = document.getElementById('btnAddEmail');

btnAddEmail.addEventListener('click', function() {
    const newItem = document.createElement('div');
    newItem.className = 'email-item';
    newItem.innerHTML = `
        <input type="email" name="emails[]" class="field-input" placeholder="ikincil@mail.com">
        <button type="button" class="btn-remove-email remove-email"><i class="fas fa-trash"></i></button>
    `;
    emailList.appendChild(newItem);
});

emailList.addEventListener('click', function(e) {
    if (e.target.closest('.remove-email')) {
        e.target.closest('.email-item').remove();
    }
});
</script>
@endpush
