@extends('admin.layouts.app')
@section('title', 'Türler')
@section('page-title', 'Türler')

@section('content')

<div class="page-header">
    <div>
        <h5 style="font-size:18px;font-weight:700;color:var(--text);margin:0">Türler</h5>
        <div style="font-size:13px;color:var(--text2);margin-top:2px">{{ $genres->count() }} tür kayıtlı</div>
    </div>
</div>

<div class="row g-4">

    {{-- ── Sol: Ekle ── --}}
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">
                <i class="bi bi-plus-circle" style="color:var(--accent-lt)"></i>
                <h6>Yeni Tür Ekle</h6>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('admin.genres.store') }}">
                    @csrf
                    <div class="mb-3">
                        <label class="form-label">Tür Adı <span style="color:var(--danger)">*</span></label>
                        <input type="text" name="name" class="form-control" placeholder="Örn: Aksiyon" required autofocus>
                    </div>
                    <div class="mb-4">
                        <label class="form-label">Renk</label>
                        <div class="d-flex gap-3 align-items-center">
                            <input type="color" name="color" class="form-control form-control-color" value="#7c3aed" style="width:50px;height:38px;padding:2px">
                            <span style="font-size:12px;color:var(--text2)">Kategori kartlarında görünecek renk</span>
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-plus-lg"></i> Tür Ekle
                    </button>
                </form>
            </div>
        </div>
    </div>

    {{-- ── Sağ: Liste ── --}}
    <div class="col-lg-8">
        <div class="table-wrap">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="padding-left:20px">Tür</th>
                            <th style="text-align:center">Renk</th>
                            <th style="text-align:center">Anime Sayısı</th>
                            <th style="text-align:center">Durum</th>
                            <th style="text-align:right;padding-right:20px">İşlem</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($genres as $genre)
                        <tr>
                            <td style="padding-left:20px">
                                <div style="display:flex;align-items:center;gap:10px">
                                    <div style="width:10px;height:10px;border-radius:50%;background:{{ $genre->color ?: 'var(--text3)' }};flex-shrink:0"></div>
                                    <span style="font-size:13.5px;font-weight:600;color:var(--text)">{{ $genre->name }}</span>
                                </div>
                            </td>
                            <td style="text-align:center">
                                @if($genre->color)
                                    <span style="font-size:12px;font-family:monospace;color:var(--text2)">{{ $genre->color }}</span>
                                @else
                                    <span style="color:var(--text3)">—</span>
                                @endif
                            </td>
                            <td style="text-align:center;font-size:13px;font-weight:700;color:var(--text)">
                                {{ $genre->animes_count }}
                            </td>
                            <td style="text-align:center">
                                @if($genre->is_active)
                                    <span class="badge bg-success">Aktif</span>
                                @else
                                    <span class="badge bg-secondary">Pasif</span>
                                @endif
                            </td>
                            <td style="text-align:right;padding-right:20px">
                                <div class="d-flex gap-2 justify-content-end">
                                    <button class="btn btn-sm btn-secondary"
                                        onclick="openEdit({{ $genre->id }}, '{{ addslashes($genre->name) }}', '{{ $genre->color }}', {{ $genre->is_active ? 1 : 0 }})"
                                        data-bs-toggle="modal" data-bs-target="#editModal">
                                        <i class="bi bi-pencil"></i>
                                    </button>
                                    <form method="POST" action="{{ route('admin.genres.destroy', $genre) }}"
                                          onsubmit="return confirm('{{ addslashes($genre->name) }} türünü silmek istediğinize emin misiniz?')">
                                        @csrf @method('DELETE')
                                        <button type="submit" class="btn btn-sm btn-outline-danger">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="5">
                                <div class="empty-state">
                                    <i class="bi bi-tags"></i>
                                    <h6>Henüz tür eklenmemiş</h6>
                                    <p>Sol panelden ilk türü ekleyebilirsiniz.</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</div>

{{-- Edit Modal --}}
<div class="modal fade" id="editModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title"><i class="bi bi-pencil-square me-2" style="color:var(--accent-lt)"></i>Tür Düzenle</h6>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" id="editForm">
                @csrf @method('PUT')
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Tür Adı <span style="color:var(--danger)">*</span></label>
                        <input type="text" name="name" id="editName" class="form-control" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Renk</label>
                        <div class="d-flex gap-3 align-items-center">
                            <input type="color" name="color" id="editColor" class="form-control form-control-color" style="width:50px;height:38px;padding:2px">
                            <span style="font-size:12px;color:var(--text2)">Kategori rengi</span>
                        </div>
                    </div>
                    <div class="d-flex align-items-center justify-content-between p-3 rounded" style="background:var(--surface2);border:1px solid var(--border)">
                        <div>
                            <div style="font-size:13.5px;font-weight:600;color:var(--text)">Aktif</div>
                            <div style="font-size:12px;color:var(--text2)">Pasif türler kullanıcılara gösterilmez</div>
                        </div>
                        <div class="form-check form-switch mb-0">
                            <input class="form-check-input" type="checkbox" name="is_active" id="editActive" value="1" role="switch">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">İptal</button>
                    <button type="submit" class="btn btn-primary"><i class="bi bi-check-lg"></i> Kaydet</button>
                </div>
            </form>
        </div>
    </div>
</div>

@endsection

@push('scripts')
<script>
function openEdit(id, name, color, active) {
    document.getElementById('editForm').action = '/admin/genres/' + id;
    document.getElementById('editName').value   = name;
    document.getElementById('editColor').value  = color || '#7c3aed';
    document.getElementById('editActive').checked = active === 1;
}
</script>
@endpush
