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

requireAdminLogin();

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

$stmt = $pdo->prepare("SELECT * FROM service_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'] ?? '');
    $status = $_POST['status'] ?? 'active';
    
    if (empty($name)) {
        $error = 'Kategori adı gereklidir.';
    } else {
        try {
            $stmt = $pdo->prepare("
                UPDATE service_categories 
                SET name = ?, description = ?, status = ?
                WHERE id = ?
            ");
            $stmt->execute([$name, $description, $status, $id]);
            
            setFlashMessage('success', 'Hizmet kategorisi başarıyla güncellendi.');
            redirect('index.php');
        } catch (PDOException $e) {
            $error = 'Kategori güncellenirken bir hata oluştu.';
        }
    }
}

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1 class="h3">Hizmet Kategorisi 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">
            <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>
            
            <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="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'; ?>





