<?php
// admin/create_admin.php - Create Multiple Admins
require_once '../config/config.php';

$message = '';
$type = '';
$show_form = true;

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username']);
    $password = $_POST['password'];
    $email = trim($_POST['email']);
    $full_name = trim($_POST['full_name']);
    
    // Check if username already exists
    $stmt = $pdo->prepare("SELECT COUNT(*) FROM admin_users WHERE username = ?");
    $stmt->execute([$username]);
    
    if ($stmt->fetchColumn() > 0) {
        $type = 'error';
        $message = 'Username already exists! Please choose a different username.';
    } else {
        // Create admin user
        $stmt = $pdo->prepare("
            INSERT INTO admin_users (username, password, email, full_name, created_at) 
            VALUES (?, ?, ?, ?, NOW())
        ");
        
        $hashed_password = password_hash($password, PASSWORD_DEFAULT);
        $stmt->execute([$username, $hashed_password, $email, $full_name]);
        
        $type = 'success';
        $message = 'Admin user created successfully!';
        $show_form = false;
    }
}

// Get all existing admins
$admins = $pdo->query("SELECT id, username, email, full_name, created_at FROM admin_users ORDER BY created_at DESC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Create Admin - <?php echo APP_NAME; ?></title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
</head>
<body class="bg-gradient-to-br from-blue-500 to-purple-600 min-h-screen p-4 sm:p-8">
    
    <div class="max-w-6xl mx-auto">
        <!-- Header -->
        <div class="text-center mb-8">
            <h1 class="text-4xl font-bold text-white mb-2">
                <i class="fas fa-users-cog mr-2"></i> Admin User Management
            </h1>
            <p class="text-blue-100">Create and manage admin users</p>
        </div>
        
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
            
            <!-- Create New Admin Form -->
            <div class="bg-white rounded-2xl shadow-2xl p-8">
                <h2 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                    <i class="fas fa-user-plus text-blue-600 mr-2"></i> Create New Admin
                </h2>
                
                <?php if ($message): ?>
                    <div class="mb-6 bg-<?php echo $type === 'success' ? 'green' : 'red'; ?>-50 border-l-4 border-<?php echo $type === 'success' ? 'green' : 'red'; ?>-500 p-4 rounded-lg">
                        <p class="text-<?php echo $type === 'success' ? 'green' : 'red'; ?>-700 text-sm">
                            <?php echo escape($message); ?>
                        </p>
                    </div>
                <?php endif; ?>
                
                <?php if ($show_form): ?>
                    <form method="POST" class="space-y-4">
                        <div>
                            <label class="block text-gray-700 font-semibold mb-2 text-sm">
                                <i class="fas fa-user mr-1"></i> Username *
                            </label>
                            <input type="text" name="username" required 
                                   pattern="[a-zA-Z0-9_]+" 
                                   class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                                   placeholder="admin_user">
                            <p class="text-xs text-gray-500 mt-1">Only letters, numbers, and underscores</p>
                        </div>
                        
                        <div>
                            <label class="block text-gray-700 font-semibold mb-2 text-sm">
                                <i class="fas fa-id-card mr-1"></i> Full Name *
                            </label>
                            <input type="text" name="full_name" required 
                                   class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                                   placeholder="John Doe">
                        </div>
                        
                        <div>
                            <label class="block text-gray-700 font-semibold mb-2 text-sm">
                                <i class="fas fa-envelope mr-1"></i> Email *
                            </label>
                            <input type="email" name="email" required 
                                   class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                                   placeholder="admin@example.com">
                        </div>
                        
                        <div>
                            <label class="block text-gray-700 font-semibold mb-2 text-sm">
                                <i class="fas fa-lock mr-1"></i> Password *
                            </label>
                            <input type="password" name="password" required minlength="6"
                                   class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                                   placeholder="Minimum 6 characters">
                        </div>
                        
                        <button type="submit" 
                                class="w-full bg-gradient-to-r from-blue-600 to-purple-600 text-white font-semibold py-3 rounded-lg hover:from-blue-700 hover:to-purple-700 transition shadow-lg">
                            <i class="fas fa-plus-circle mr-2"></i> Create Admin
                        </button>
                    </form>
                <?php else: ?>
                    <div class="text-center py-8">
                        <div class="inline-block p-4 bg-green-100 rounded-full mb-4">
                            <i class="fas fa-check-circle text-green-600 text-3xl"></i>
                        </div>
                        <p class="text-gray-600 mb-6">Admin created successfully!</p>
                        <button onclick="location.reload()" 
                                class="px-6 py-3 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                            <i class="fas fa-plus mr-2"></i> Create Another Admin
                        </button>
                    </div>
                <?php endif; ?>
                
                <div class="mt-6 pt-6 border-t">
                    <a href="<?php echo base_url('admin/login.php'); ?>" 
                       class="block w-full text-center px-6 py-3 border-2 border-blue-600 text-blue-600 font-semibold rounded-lg hover:bg-blue-50 transition">
                        <i class="fas fa-sign-in-alt mr-2"></i> Go to Login
                    </a>
                </div>
            </div>
            
            <!-- Existing Admins List -->
            <div class="bg-white rounded-2xl shadow-2xl p-8">
                <h2 class="text-2xl font-bold text-gray-800 mb-6 flex items-center">
                    <i class="fas fa-users text-purple-600 mr-2"></i> 
                    Existing Admins (<?php echo count($admins); ?>)
                </h2>
                
                <?php if (empty($admins)): ?>
                    <div class="text-center py-12 text-gray-500">
                        <i class="fas fa-user-slash text-5xl mb-4 opacity-30"></i>
                        <p>No admin users yet.</p>
                        <p class="text-sm mt-2">Create your first admin using the form.</p>
                    </div>
                <?php else: ?>
                    <div class="space-y-3 max-h-[500px] overflow-y-auto custom-scrollbar pr-2">
                        <?php foreach ($admins as $admin): ?>
                            <div class="border border-gray-200 rounded-lg p-4 hover:shadow-md transition">
                                <div class="flex items-start gap-3">
                                    <div class="flex-shrink-0">
                                        <div class="w-12 h-12 bg-gradient-to-br from-blue-500 to-purple-600 rounded-full flex items-center justify-center">
                                            <span class="text-white font-bold text-lg">
                                                <?php echo strtoupper(substr($admin->full_name, 0, 1)); ?>
                                            </span>
                                        </div>
                                    </div>
                                    <div class="flex-1 min-w-0">
                                        <h3 class="font-semibold text-gray-900 truncate">
                                            <?php echo escape($admin->full_name); ?>
                                        </h3>
                                        <p class="text-sm text-gray-600 flex items-center mt-1">
                                            <i class="fas fa-user text-xs mr-2 text-gray-400"></i>
                                            <span class="font-mono"><?php echo escape($admin->username); ?></span>
                                        </p>
                                        <p class="text-sm text-gray-600 flex items-center mt-1 truncate">
                                            <i class="fas fa-envelope text-xs mr-2 text-gray-400"></i>
                                            <?php echo escape($admin->email); ?>
                                        </p>
                                        <p class="text-xs text-gray-500 mt-2">
                                            <i class="fas fa-clock mr-1"></i>
                                            Created: <?php echo date('M d, Y', strtotime($admin->created_at)); ?>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>
            
        </div>
    </div>
    
    <style>
        .custom-scrollbar::-webkit-scrollbar {
            width: 6px;
        }
        .custom-scrollbar::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 10px;
        }
        .custom-scrollbar::-webkit-scrollbar-thumb {
            background: #888;
            border-radius: 10px;
        }
        .custom-scrollbar::-webkit-scrollbar-thumb:hover {
            background: #555;
        }
    </style>
    
</body>
</html>