@extends('admin.layout')

@section('header-title', 'Forum Kategorisi Düzenle')

@section('content')
<style>
    .page-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 32px;
        flex-wrap: wrap;
        gap: 16px;
    }
    .page-title {
        font-size: 24px;
        font-weight: 800;
        color: #1f2937;
    }
    .page-subtitle {
        font-size: 14px;
        color: #6b7280;
        margin-top: 4px;
    }

    .form-row {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 20px;
        margin-bottom: 20px;
    }
    .form-group {
        margin-bottom: 20px;
    }
    .form-label {
        display: block;
        font-size: 14px;
        font-weight: 600;
        color: #1f2937;
        margin-bottom: 8px;
    }
    .form-control,
    .form-select {
        width: 100%;
        padding: 10px 12px;
        border: 1px solid #e5e7eb;
        border-radius: 6px;
        font-size: 14px;
        font-family: inherit;
        outline: none;
        transition: all 0.2s;
    }
    .form-control:focus,
    .form-select:focus {
        border-color: #1e40af;
        box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.1);
    }
    textarea.form-control {
        resize: vertical;
        min-height: 100px;
    }
    .form-hint {
        font-size: 13px;
        color: #6b7280;
        margin-top: 4px;
    }
    .form-error {
        font-size: 13px;
        color: #ef4444;
        margin-top: 4px;
    }
    .required {
        color: #ef4444;
    }

    .card {
        background: white;
        border: 1px solid #e5e7eb;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 1px 2px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }
    .card-header {
        background: #f9fafb;
        border-bottom: 1px solid #e5e7eb;
        padding: 20px;
    }
    .card-title {
        font-size: 16px;
        font-weight: 700;
        color: #1f2937;
        margin: 0;
    }
    .card-body {
        padding: 20px;
    }

    .form-actions {
        display: flex;
        gap: 12px;
        margin-top: 24px;
        padding-top: 20px;
        border-top: 1px solid #e5e7eb;
    }
    .btn {
        padding: 10px 20px;
        border: none;
        border-radius: 6px;
        font-weight: 600;
        font-size: 14px;
        cursor: pointer;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 8px;
        transition: all 0.2s;
    }
    .btn-primary {
        background: #1e40af;
        color: white;
    }
    .btn-primary:hover {
        background: #1e3a8a;
    }
    .btn-secondary {
        background: white;
        color: #374151;
        border: 1px solid #e5e7eb;
    }
    .btn-secondary:hover {
        background: #f9fafb;
    }
    .btn-danger {
        background: #fecaca;
        color: #991b1b;
        margin-left: auto;
    }
    .btn-danger:hover {
        background: #fca5a5;
    }

    .info-table {
        width: 100%;
        border-collapse: collapse;
        font-size: 14px;
    }
    .info-table tr {
        border-bottom: 1px solid #f3f4f6;
    }
    .info-table tr:last-child {
        border-bottom: none;
    }
    .info-table td {
        padding: 12px 0;
    }
    .info-table td:first-child {
        color: #6b7280;
        font-weight: 500;
        width: 40%;
    }
    .info-table code {
        background: #f3f4f6;
        padding: 4px 8px;
        border-radius: 4px;
        font-family: monospace;
        color: #1f2937;
    }

    .list-items {
        display: flex;
        flex-direction: column;
        gap: 0;
    }
    .list-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 12px 0;
        border-bottom: 1px solid #f3f4f6;
        font-size: 14px;
        color: #1f2937;
        text-decoration: none;
        transition: all 0.2s;
    }
    .list-item:last-child {
        border-bottom: none;
    }
    .list-item:hover {
        background: #f9fafb;
        padding-left: 4px;
    }
    .list-item-name {
        font-weight: 500;
        color: #1e40af;
    }
    .list-item-badge {
        display: inline-block;
        background: #e0e7ff;
        color: #3730a3;
        padding: 4px 12px;
        border-radius: 6px;
        font-size: 12px;
        font-weight: 600;
    }

    .grid-2 {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 24px;
    }

    @media (max-width: 768px) {
        .grid-2 {
            grid-template-columns: 1fr;
        }
        .form-row {
            grid-template-columns: 1fr;
        }
    }
</style>

<div class="page-header">
    <div>
        <h1 class="page-title">🗂️ Forum Kategorisi Düzenle</h1>
        <p class="page-subtitle">{{ $forumCategory->name }}</p>
    </div>
</div>

<div class="grid-2">
    <div>
        <div class="card">
            <div class="card-body">
                <form action="{{ route('admin.forum-categories.update', $forumCategory) }}" method="POST">
                    @csrf
                    @method('PUT')

                    <div class="form-group">
                        <label class="form-label" for="name">Kategori Adı <span class="required">*</span></label>
                        <input 
                            type="text" 
                            id="name" 
                            name="name" 
                            value="{{ old('name', $forumCategory->name) }}" 
                            class="form-control" 
                            placeholder="Örn: KPSS Hazırlığı, Mülakat Deneyimleri" 
                            required 
                            autofocus
                        >
                        @error('name')
                            <div class="form-error">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="slug">Slug</label>
                        <input 
                            type="text" 
                            id="slug" 
                            name="slug" 
                            value="{{ old('slug', $forumCategory->slug) }}" 
                            class="form-control" 
                            placeholder="Otomatik oluşturulur"
                        >
                        <div class="form-hint">Boş bırakılırsa isimden otomatik oluşturulur.</div>
                        @error('slug')
                            <div class="form-error">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-group">
                        <label class="form-label" for="description">Açıklama</label>
                        <textarea 
                            id="description" 
                            name="description" 
                            class="form-control" 
                            placeholder="Kategori hakkında kısa açıklama"
                        >{{ old('description', $forumCategory->description) }}</textarea>
                        @error('description')
                            <div class="form-error">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="form-row">
                        <div class="form-group">
                            <label class="form-label" for="parent_id">Üst Kategori</label>
                            <select id="parent_id" name="parent_id" class="form-select">
                                <option value="">— Ana Kategori —</option>
                                @foreach($parentCategories as $id => $name)
                                    <option value="{{ $id }}" {{ old('parent_id', $forumCategory->parent_id) == $id ? 'selected' : '' }}>{{ $name }}</option>
                                @endforeach
                            </select>
                            @error('parent_id')
                                <div class="form-error">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label class="form-label" for="order">Sıra</label>
                            <input 
                                type="number" 
                                id="order" 
                                name="order" 
                                value="{{ old('order', $forumCategory->order ?? 0) }}" 
                                min="0" 
                                class="form-control"
                            >
                            @error('order')
                                <div class="form-error">{{ $message }}</div>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label class="form-label" for="icon">İkon (Emoji)</label>
                            <input 
                                type="text" 
                                id="icon" 
                                name="icon" 
                                value="{{ old('icon', $forumCategory->icon ?? '💬') }}" 
                                class="form-control" 
                                placeholder="💬"
                                maxlength="2"
                            >
                            @error('icon')
                                <div class="form-error">{{ $message }}</div>
                            @enderror
                        </div>
                    </div>

                    <div class="form-actions">
                        <button type="submit" class="btn btn-primary">
                            <svg style="width:18px;height:18px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                            </svg>
                            Güncelle
                        </button>
                        <a href="{{ route('admin.forum-categories.index') }}" class="btn btn-secondary">İptal</a>
                        <form action="{{ route('admin.forum-categories.destroy', $forumCategory) }}" method="POST" style="display: flex; margin-left: auto;" onsubmit="return confirm('Bu kategoriyi silmek istediğinizden emin misiniz?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger">
                                <svg style="width:18px;height:18px" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/>
                                </svg>
                                Sil
                            </button>
                        </form>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div>
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">📋 Kategori Bilgileri</h3>
            </div>
            <div class="card-body">
                <table class="info-table">
                    <tr>
                        <td>Slug:</td>
                        <td><code>{{ $forumCategory->slug }}</code></td>
                    </tr>
                    <tr>
                        <td>Konu Sayısı:</td>
                        <td><strong>{{ $forumCategory->topics_count ?? 0 }}</strong></td>
                    </tr>
                    <tr>
                        <td>Alt Kategoriler:</td>
                        <td><strong>{{ $forumCategory->children_count ?? 0 }}</strong></td>
                    </tr>
                    <tr>
                        <td>Oluşturulma:</td>
                        <td>{{ $forumCategory->created_at->format('d.m.Y H:i') }}</td>
                    </tr>
                </table>
            </div>
        </div>

        @if($forumCategory->children && $forumCategory->children->count() > 0)
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">📁 Alt Kategoriler</h3>
            </div>
            <div class="card-body">
                <div class="list-items">
                    @foreach($forumCategory->children as $child)
                    <a href="{{ route('admin.forum-categories.edit', $child) }}" class="list-item">
                        <span class="list-item-name">{{ $child->icon }} {{ $child->name }}</span>
                        <span class="list-item-badge">{{ $child->topics_count ?? 0 }} konu</span>
                    </a>
                    @endforeach
                </div>
            </div>
        </div>
        @endif
    </div>
</div>

@endsection
