<?php
require_once 'config/config.php';
requireAdmin();

$currentUser = getCurrentUser();
$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = sanitizeInput($_POST['username'] ?? '');
    $password = $_POST['password'] ?? '';
    $email = sanitizeInput($_POST['email'] ?? '');
    $fullName = sanitizeInput($_POST['full_name'] ?? '');
    $role = $_POST['role'] ?? 'user';
    $status = $_POST['status'] ?? 'active';
    
    // Validasyon
    if (empty($username) || empty($password) || empty($email) || empty($fullName)) {
        $error = 'Tüm alanlar zorunludur.';
    } elseif (strlen($password) < 6) {
        $error = 'Şifre en az 6 karakter olmalıdır.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error = 'Geçerli bir e-posta adresi giriniz.';
    } elseif (!in_array($role, ['admin', 'user', 'manager'])) {
        $error = 'Geçersiz rol seçimi.';
    } elseif (!in_array($status, ['active', 'inactive'])) {
        $error = 'Geçersiz durum seçimi.';
    } else {
        try {
            $db = new Database();
            
            // Kullanıcı adı kontrolü
            $existingUser = $db->query("SELECT id FROM users WHERE username = ?", [$username])->fetch();
            if ($existingUser) {
                $error = 'Bu kullanıcı adı zaten kullanılıyor.';
            } else {
                // E-posta kontrolü
                $existingEmail = $db->query("SELECT id FROM users WHERE email = ?", [$email])->fetch();
                if ($existingEmail) {
                    $error = 'Bu e-posta adresi zaten kullanılıyor.';
                } else {
                    // Kullanıcıyı ekle
                    $hashedPassword = hashPassword($password);
                    $db->query(
                        "INSERT INTO users (username, password, email, full_name, role, status) VALUES (?, ?, ?, ?, ?, ?)",
                        [$username, $hashedPassword, $email, $fullName, $role, $status]
                    );
                    
                    $success = 'Kullanıcı başarıyla eklendi.';
                    
                    // Formu temizle
                    $username = $email = $fullName = '';
                    $role = 'user';
                    $status = 'active';
                }
            }
        } catch (Exception $e) {
            $error = 'Kullanıcı eklenirken bir hata oluştu.';
            error_log("Add user error: " . $e->getMessage());
        }
    }
}
?>
<!DOCTYPE html>
<html lang="tr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kullanıcı Ekle - <?php echo SITE_NAME; ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: #f5f5f5;
            line-height: 1.6;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .header-content {
            display: flex;
            justify-content: space-between;
            align-items: center;
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .logo h1 {
            font-size: 24px;
        }
        
        .user-info {
            display: flex;
            align-items: center;
            gap: 20px;
        }
        
        .user-info span {
            font-size: 14px;
        }
        
        .nav {
            background: white;
            padding: 1rem 2rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .nav-content {
            max-width: 1200px;
            margin: 0 auto;
        }
        
        .nav ul {
            list-style: none;
            display: flex;
            gap: 30px;
        }
        
        .nav a {
            text-decoration: none;
            color: #333;
            font-weight: 500;
            padding: 10px 15px;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .nav a:hover, .nav a.active {
            background: #667eea;
            color: white;
        }
        
        .container {
            max-width: 800px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        
        .page-header {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 2rem;
        }
        
        .page-header h2 {
            color: #333;
            margin-bottom: 0.5rem;
        }
        
        .page-header p {
            color: #666;
        }
        
        .form-container {
            background: white;
            padding: 2rem;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        .form-group {
            margin-bottom: 1.5rem;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 0.5rem;
            color: #333;
            font-weight: 500;
        }
        
        .form-group input,
        .form-group select {
            width: 100%;
            padding: 12px;
            border: 2px solid #e1e1e1;
            border-radius: 6px;
            font-size: 16px;
            transition: border-color 0.3s;
        }
        
        .form-group input:focus,
        .form-group select:focus {
            outline: none;
            border-color: #667eea;
        }
        
        .form-row {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 1rem;
        }
        
        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: #667eea;
            color: white;
            text-decoration: none;
            border-radius: 6px;
            font-weight: 500;
            transition: background-color 0.3s;
            border: none;
            cursor: pointer;
            font-size: 16px;
        }
        
        .btn:hover {
            background: #5a6fd8;
        }
        
        .btn-secondary {
            background: #6c757d;
        }
        
        .btn-secondary:hover {
            background: #5a6268;
        }
        
        .btn-group {
            display: flex;
            gap: 1rem;
            margin-top: 2rem;
        }
        
        .error {
            background: #fee;
            color: #c33;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border: 1px solid #fcc;
        }
        
        .success {
            background: #d4edda;
            color: #155724;
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            border: 1px solid #c3e6cb;
        }
        
        .required {
            color: #e74c3c;
        }
        
        @media (max-width: 768px) {
            .form-row {
                grid-template-columns: 1fr;
            }
            
            .btn-group {
                flex-direction: column;
            }
            
            .container {
                padding: 0 1rem;
            }
        }
    </style>
</head>
<body>
    <header class="header">
        <div class="header-content">
            <div class="logo">
                <h1>Trendyol Sipariş Yönetimi</h1>
            </div>
            <div class="user-info">
                <span>Hoş geldiniz, <strong><?php echo htmlspecialchars($currentUser['full_name']); ?></strong></span>
                <span>(<?php echo htmlspecialchars($currentUser['role']); ?>)</span>
                <a href="logout.php" class="btn btn-danger">Çıkış</a>
            </div>
        </div>
    </header>
    
    <nav class="nav">
        <div class="nav-content">
            <ul>
                <li><a href="dashboard.php">Ana Panel</a></li>
                <li><a href="users.php">Kullanıcı Yönetimi</a></li>
                <li><a href="add-user.php" class="active">Kullanıcı Ekle</a></li>
                <li><a href="profile.php">Profil</a></li>
            </ul>
        </div>
    </nav>
    
    <div class="container">
        <div class="page-header">
            <h2>Yeni Kullanıcı Ekle</h2>
            <p>Sisteme yeni kullanıcı eklemek için aşağıdaki formu doldurun.</p>
        </div>
        
        <div class="form-container">
            <?php if ($error): ?>
                <div class="error"><?php echo htmlspecialchars($error); ?></div>
            <?php endif; ?>
            
            <?php if ($success): ?>
                <div class="success"><?php echo htmlspecialchars($success); ?></div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="form-group">
                    <label for="username">Kullanıcı Adı <span class="required">*</span></label>
                    <input type="text" id="username" name="username" required 
                           value="<?php echo htmlspecialchars($username ?? ''); ?>"
                           placeholder="Kullanıcı adını giriniz">
                </div>
                
                <div class="form-group">
                    <label for="full_name">Ad Soyad <span class="required">*</span></label>
                    <input type="text" id="full_name" name="full_name" required 
                           value="<?php echo htmlspecialchars($fullName ?? ''); ?>"
                           placeholder="Ad ve soyadını giriniz">
                </div>
                
                <div class="form-group">
                    <label for="email">E-posta <span class="required">*</span></label>
                    <input type="email" id="email" name="email" required 
                           value="<?php echo htmlspecialchars($email ?? ''); ?>"
                           placeholder="E-posta adresini giriniz">
                </div>
                
                <div class="form-group">
                    <label for="password">Şifre <span class="required">*</span></label>
                    <input type="password" id="password" name="password" required 
                           placeholder="En az 6 karakter olmalıdır">
                </div>
                
                <div class="form-row">
                    <div class="form-group">
                        <label for="role">Rol <span class="required">*</span></label>
                        <select id="role" name="role" required>
                            <option value="user" <?php echo ($role ?? 'user') === 'user' ? 'selected' : ''; ?>>Kullanıcı</option>
                            <option value="manager" <?php echo ($role ?? '') === 'manager' ? 'selected' : ''; ?>>Yönetici</option>
                            <option value="admin" <?php echo ($role ?? '') === 'admin' ? 'selected' : ''; ?>>Admin</option>
                        </select>
                    </div>
                    
                    <div class="form-group">
                        <label for="status">Durum <span class="required">*</span></label>
                        <select id="status" name="status" required>
                            <option value="active" <?php echo ($status ?? 'active') === 'active' ? 'selected' : ''; ?>>Aktif</option>
                            <option value="inactive" <?php echo ($status ?? '') === 'inactive' ? 'selected' : ''; ?>>Pasif</option>
                        </select>
                    </div>
                </div>
                
                <div class="btn-group">
                    <button type="submit" class="btn">Kullanıcı Ekle</button>
                    <a href="users.php" class="btn btn-secondary">İptal</a>
                </div>
            </form>
        </div>
    </div>
</body>
</html>
