<?php
require_once '../../config/config.php';
require_once '../../includes/auth.php';

requireAdminLogin();

$id = $_GET['id'] ?? 0;

$stmt = $pdo->prepare("SELECT * FROM company_categories WHERE id = ?");
$stmt->execute([$id]);
$category = $stmt->fetch();

if (!$category) {
    setFlashMessage('error', 'Kategori bulunamadı.');
    redirect('index.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = trim($_POST['name'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $meta_title = trim($_POST['meta_title'] ?? '');
    $meta_description = trim($_POST['meta_description'] ?? '');
    $meta_keywords = trim($_POST['meta_keywords'] ?? '');
    $status = $_POST['status'] ?? 'active';
    
    // Slug oluştur
    $slug = createSlug($name);
    
    // Icon yükleme
    $icon = $category['icon'];
    if (isset($_FILES['icon']) && $_FILES['icon']['error'] === UPLOAD_ERR_OK) {
        $upload = uploadImage($_FILES['icon'], 'categories');
        if ($upload['success']) {
            // Eski resmi sil
            if ($icon) {
                $oldPath = str_replace(UPLOAD_URL, UPLOAD_PATH, $icon);
                deleteImage($oldPath);
            }
            $icon = $upload['url'];
        } else {
            $error = $upload['message'];
        }
    }
    
    if (empty($name)) {
        $error = 'Kategori adı gereklidir.';
    } elseif (empty($slug)) {
        $error = 'Geçerli bir kategori adı giriniz.';
    } else {
        // Slug'un benzersiz olup olmadığını kontrol et (kendi ID'si hariç)
        $stmt = $pdo->prepare("SELECT id FROM company_categories WHERE slug = ? AND id != ?");
        $stmt->execute([$slug, $id]);
        if ($stmt->fetch()) {
            $slug = $slug . '-' . time();
        }
        
        try {
            $stmt = $pdo->prepare("
                UPDATE company_categories 
                SET name = ?, slug = ?, icon = ?, description = ?, meta_title = ?, meta_description = ?, meta_keywords = ?, status = ?
                WHERE id = ?
            ");
            $stmt->execute([$name, $slug, $icon, $description, $meta_title, $meta_description, $meta_keywords, $status, $id]);
            
            setFlashMessage('success', 'Kategori başarıyla güncellendi.');
            redirect('index.php');
        } catch (PDOException $e) {
            $error = 'Kategori güncellenirken bir hata oluştu.';
        }
    }
}

$page_title = 'Kategori Düzenle';
include '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Kategori Düzenle</h1>
    <a href="index.php" class="btn btn-secondary">
        <i class="fas fa-arrow-left me-2"></i>Geri Dön
    </a>
</div>

<?php if ($error): ?>
    <div class="alert alert-danger"><?= e($error) ?></div>
<?php endif; ?>

<div class="card">
    <div class="card-body">
        <form method="POST" enctype="multipart/form-data">
            <div class="row">
                <div class="col-md-8">
                    <div class="mb-3">
                        <label for="name" class="form-label">Kategori Adı <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" id="name" name="name" value="<?= e($category['name']) ?>" required>
                    </div>
                    
                    <div class="mb-3">
                        <label for="description" class="form-label">Açıklama</label>
                        <textarea class="form-control" id="description" name="description" rows="4"><?= e($category['description']) ?></textarea>
                    </div>
                    
                    <h5 class="mt-4 mb-3">SEO Ayarları</h5>
                    
                    <div class="mb-3">
                        <label for="meta_title" class="form-label">Meta Başlık</label>
                        <input type="text" class="form-control" id="meta_title" name="meta_title" value="<?= e($category['meta_title']) ?>">
                    </div>
                    
                    <div class="mb-3">
                        <label for="meta_description" class="form-label">Meta Açıklama</label>
                        <textarea class="form-control" id="meta_description" name="meta_description" rows="3"><?= e($category['meta_description']) ?></textarea>
                    </div>
                    
                    <div class="mb-3">
                        <label for="meta_keywords" class="form-label">Meta Anahtar Kelimeler</label>
                        <input type="text" class="form-control" id="meta_keywords" name="meta_keywords" value="<?= e($category['meta_keywords']) ?>">
                    </div>
                </div>
                
                <div class="col-md-4">
                    <div class="mb-3">
                        <label for="icon" class="form-label">Kategori İkonu/Resmi</label>
                        <?php if ($category['icon']): ?>
                            <div class="mb-2">
                                <img src="<?= e(getImageUrl($category['icon'])) ?>" alt="" class="img-thumbnail" style="max-width: 200px;">
                            </div>
                        <?php endif; ?>
                        <input type="file" class="form-control" id="icon" name="icon" accept="image/*">
                        <small class="text-muted">Yeni resim yüklemek için seçin</small>
                    </div>
                    
                    <div class="mb-3">
                        <label for="status" class="form-label">Durum</label>
                        <select class="form-select" id="status" name="status">
                            <option value="active" <?= $category['status'] === 'active' ? 'selected' : '' ?>>Aktif</option>
                            <option value="inactive" <?= $category['status'] === 'inactive' ? 'selected' : '' ?>>Pasif</option>
                        </select>
                    </div>
                    
                    <div class="mb-3">
                        <label class="form-label">Slug</label>
                        <input type="text" class="form-control" value="<?= e($category['slug']) ?>" disabled>
                        <small class="text-muted">URL: <?= SITE_URL ?>/<?= e($category['slug']) ?></small>
                    </div>
                </div>
            </div>
            
            <div class="mt-4">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save me-2"></i>Güncelle
                </button>
                <a href="index.php" class="btn btn-secondary">İptal</a>
            </div>
        </form>
    </div>
</div>

<?php include '../includes/footer.php'; ?>




