@extends('agency.layouts.app')

@section('title', 'Müşteri Döküman Tipleri')

@section('content')
<div class="container-fluid py-4">
    <div class="row mb-4">
        <div class="col-12 d-flex justify-content-between align-items-center">
            <div>
                <h4 class="mb-1 fw-bold">Döküman Tipleri</h4>
                <p class="text-muted small mb-0">Müşterilere yüklenecek belgelerin kategorilerini (sözleşme, sigorta vb.) yönetin.</p>
            </div>
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addTypeModal">
                <i class="fas fa-plus me-2"></i> Yeni Tip Ekle
            </button>
        </div>
    </div>

    @if($errors->any())
        <div class="alert alert-danger border-0 shadow-sm" style="border-radius: 14px;">
            <div class="fw-bold mb-1"><i class="fas fa-triangle-exclamation me-2"></i>Kaydedilemedi</div>
            <ul class="mb-0 small ps-3">
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif

    <div class="card border-0 shadow-sm" style="border-radius: 16px;">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="ps-4" style="width: 50px;">Sıra</th>
                            <th>Döküman Tipi Adı</th>
                            <th>Açıklama</th>
                            <th class="text-center">Zorunlu mu?</th>
                            <th class="text-center">Durum</th>
                            <th class="text-end pe-4">İşlemler</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($types as $type)
                            <tr>
                                <td class="ps-4">
                                    <span class="badge bg-light text-dark fw-bold">{{ $type->sort_order }}</span>
                                </td>
                                <td>
                                    <div class="fw-bold text-dark">{{ $type->name }}</div>
                                    @if($type->code)
                                        <div class="text-muted" style="font-size: 0.7rem;">KOD: {{ $type->code }}</div>
                                    @endif
                                </td>
                                <td><span class="text-muted small">{{ $type->description ?: '-' }}</span></td>
                                <td class="text-center">
                                    @if($type->is_required)
                                        <span class="badge bg-soft-danger text-danger">Evet</span>
                                    @else
                                        <span class="badge bg-soft-secondary text-muted">Hayır</span>
                                    @endif
                                </td>
                                <td class="text-center">
                                    @if($type->is_active)
                                        <span class="badge bg-success">Aktif</span>
                                    @else
                                        <span class="badge bg-secondary">Pasif</span>
                                    @endif
                                </td>
                                <td class="text-end pe-4">
                                    <div class="btn-group">
                                        <button class="btn btn-sm btn-icon border-0" onclick="editType({{ $type->toJson() }})">
                                            <i class="fas fa-edit text-primary"></i>
                                        </button>
                                        <form action="{{ route('agency.customers.document-types.destroy', $type) }}" method="POST" class="d-inline" onsubmit="return confirm('Bu döküman tipini silmek istediğinize emin misiniz?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-sm btn-icon border-0">
                                                <i class="fas fa-trash text-danger"></i>
                                            </button>
                                        </form>
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="text-center py-5">
                                    <div class="text-muted mb-3">
                                        <i class="fas fa-file-invoice" style="font-size: 3rem; opacity: 0.2;"></i>
                                    </div>
                                    <h6>Henüz Tanımlı Döküman Tipi Yok</h6>
                                    <p class="small text-muted">Müşteri dökümanlarını kategorize etmek için bir tip ekleyin.</p>
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

{{-- Add Modal --}}
<div class="modal fade" id="addTypeModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow" style="border-radius: 20px;">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-bold">Yeni Döküman Tipi</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('agency.customers.document-types.store') }}" method="POST">
                @csrf
                <div class="modal-body py-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted text-uppercase">TİP ADI</label>
                        <input type="text" name="name" class="form-control form-control-lg border-light bg-light" placeholder="Örn: Tur Sözleşmesi" required style="border-radius: 12px;">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted text-uppercase">AÇIKLAMA</label>
                        <textarea name="description" class="form-control border-light bg-light" rows="3" placeholder="İsteğe bağlı..." style="border-radius: 12px;"></textarea>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold small text-muted text-uppercase">SIRA</label>
                            <input type="number" name="sort_order" class="form-control border-light bg-light" value="0" style="border-radius: 12px;">
                        </div>
                        <div class="col-md-6 mb-3 d-flex align-items-center mt-3">
                            <div class="form-check form-switch">
                                <input type="hidden" name="is_required" value="0">
                                <input class="form-check-input" type="checkbox" name="is_required" id="is_required" value="1" {{ old('is_required') ? 'checked' : '' }}>
                                <label class="form-check-label fw-bold small" for="is_required">Zorunlu Belge</label>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light px-4" data-bs-dismiss="modal" style="border-radius: 10px;">Vazgeç</button>
                    <button type="submit" class="btn btn-primary px-4" style="border-radius: 10px;">Kaydet</button>
                </div>
            </form>
        </div>
    </div>
</div>

{{-- Edit Modal --}}
<div class="modal fade" id="editTypeModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow" style="border-radius: 20px;">
            <div class="modal-header border-0 pb-0">
                <h5 class="modal-title fw-bold">Döküman Tipini Düzenle</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form id="editForm" method="POST">
                @csrf
                @method('PUT')
                <div class="modal-body py-4">
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted text-uppercase">TİP ADI</label>
                        <input type="text" name="name" id="edit_name" class="form-control form-control-lg border-light bg-light" required style="border-radius: 12px;">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold small text-muted text-uppercase">AÇIKLAMA</label>
                        <textarea name="description" id="edit_description" class="form-control border-light bg-light" rows="3" style="border-radius: 12px;"></textarea>
                    </div>
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold small text-muted text-uppercase">SIRA</label>
                            <input type="number" name="sort_order" id="edit_sort_order" class="form-control border-light bg-light" style="border-radius: 12px;">
                        </div>
                        <div class="col-md-6 mb-3 d-flex align-items-center mt-3">
                            <div class="form-check form-switch">
                                <input type="hidden" name="is_required" value="0">
                                <input class="form-check-input" type="checkbox" name="is_required" id="edit_is_required" value="1">
                                <label class="form-check-label fw-bold small" for="edit_is_required">Zorunlu Belge</label>
                            </div>
                        </div>
                    </div>
                    <div class="form-check form-switch">
                        <input type="hidden" name="is_active" value="0">
                        <input class="form-check-input" type="checkbox" name="is_active" id="edit_is_active" value="1">
                        <label class="form-check-label fw-bold small" for="edit_is_active">Aktif</label>
                    </div>
                </div>
                <div class="modal-footer border-0 pt-0">
                    <button type="button" class="btn btn-light px-4" data-bs-dismiss="modal" style="border-radius: 10px;">Vazgeç</button>
                    <button type="submit" class="btn btn-primary px-4" style="border-radius: 10px;">Güncelle</button>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
.bg-soft-danger { background-color: #fee2e2; }
.bg-soft-secondary { background-color: #f1f5f9; }
.btn-icon { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: 8px; transition: all 0.2s; }
.btn-icon:hover { background: #f1f5f9; }
</style>

@push('scripts')
<script>
function editType(type) {
    $('#editForm').attr('action', '/agency/customer-document-types/' + type.id);
    $('#edit_name').val(type.name);
    $('#edit_description').val(type.description);
    $('#edit_sort_order').val(type.sort_order);
    $('#edit_is_required').prop('checked', type.is_required);
    $('#edit_is_active').prop('checked', type.is_active);
    $('#editTypeModal').modal('show');
}
</script>
@endpush
@endsection
