<?php
session_start();
include('config.php');

// Check if user is logged in and is admin
if (!isset($_SESSION['username']) || $_SESSION['username'] !== 'admin') {
    header("Location: login.php");
    exit();
}

// Function to generate license key
function generateLicenseKey() {
    return strtoupper(md5(uniqid(rand(), true)));
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['create_account'])) {
        $username = trim($_POST['username']);
        $password = trim($_POST['password']);
        $license_duration = (int)$_POST['license_duration']; // Duration in days
        
        if (!empty($username) && !empty($password) && $license_duration > 0) {
            // Check if username already exists
            $check_user = $conn->prepare("SELECT id_user FROM users WHERE username = ?");
            $check_user->bind_param("s", $username);
            $check_user->execute();
            $result = $check_user->get_result();
            
            if ($result->num_rows > 0) {
                $_SESSION['error_message'] = "Username already exists.";
            } else {
                // Generate license information
                $license_key = generateLicenseKey();
                $license_start = date('Y-m-d');
                $license_end = date('Y-m-d', strtotime("+$license_duration days"));
                
                // Create new account
                $hashed_password = md5($password);
                $create_user = $conn->prepare("INSERT INTO users (username, password, license_key, license_start, license_end, license_status) VALUES (?, ?, ?, ?, ?, 'active')");
                $create_user->bind_param("sssss", $username, $hashed_password, $license_key, $license_start, $license_end);
                
                if ($create_user->execute()) {
                    $_SESSION['success_message'] = "Account created successfully! License Key: " . $license_key;
                } else {
                    $_SESSION['error_message'] = "Failed to create account: " . $conn->error;
                }
                
                $create_user->close();
            }
            
            $check_user->close();
        } else {
            $_SESSION['error_message'] = "Please fill in all fields and provide a valid license duration.";
        }
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Create Account - Admin Panel</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        body {
            background: #0a0a0f;
            color: #ffffff;
            font-family: 'Orbitron', sans-serif;
        }
        
        .container {
            max-width: 500px;
            margin: 50px auto;
            padding: 20px;
        }
        
        .cyber-panel {
            background: linear-gradient(165deg, #12151C 0%, #0a0a0f 100%);
            border: 1px solid rgba(33, 150, 243, 0.3);
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 0 30px rgba(33, 150, 243, 0.1);
        }
        
        .cyber-title {
            color: #2196F3;
            text-align: center;
            margin-bottom: 30px;
        }
        
        .cyber-form-group {
            margin-bottom: 20px;
        }
        
        .cyber-form-group label {
            display: block;
            margin-bottom: 8px;
            color: #2196F3;
        }
        
        .cyber-form-group input, .cyber-form-group select {
            width: 100%;
            padding: 10px;
            background: rgba(13, 17, 23, 0.8);
            border: 1px solid rgba(33, 150, 243, 0.3);
            border-radius: 5px;
            color: #fff;
            font-size: 14px;
        }
        
        .cyber-button {
            background: #2196F3;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            width: 100%;
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .cyber-button:hover {
            background: #1976D2;
        }
        
        .cyber-notification {
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
            text-align: center;
        }
        
        .cyber-notification.success {
            background: rgba(76, 175, 80, 0.1);
            border: 1px solid rgba(76, 175, 80, 0.3);
            color: #4CAF50;
        }
        
        .cyber-notification.error {
            background: rgba(244, 67, 54, 0.1);
            border: 1px solid rgba(244, 67, 54, 0.3);
            color: #f44336;
        }
        
        .back-link {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #2196F3;
            text-decoration: none;
        }
        
        .back-link:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="cyber-panel">
            <h2 class="cyber-title"><i class="fas fa-user-plus"></i> Create New Account</h2>
            
            <?php if (isset($_SESSION['success_message'])): ?>
                <div class="cyber-notification success">
                    <i class="fas fa-check-circle"></i>
                    <?php echo $_SESSION['success_message']; unset($_SESSION['success_message']); ?>
                </div>
            <?php endif; ?>
            
            <?php if (isset($_SESSION['error_message'])): ?>
                <div class="cyber-notification error">
                    <i class="fas fa-exclamation-circle"></i>
                    <?php echo $_SESSION['error_message']; unset($_SESSION['error_message']); ?>
                </div>
            <?php endif; ?>
            
            <form method="POST" action="">
                <div class="cyber-form-group">
                    <label for="username"><i class="fas fa-user"></i> Username</label>
                    <input type="text" id="username" name="username" required>
                </div>
                
                <div class="cyber-form-group">
                    <label for="password"><i class="fas fa-lock"></i> Password</label>
                    <input type="password" id="password" name="password" required>
                </div>
                
                <div class="cyber-form-group">
                    <label for="license_duration"><i class="fas fa-clock"></i> License Duration (Days)</label>
                    <select id="license_duration" name="license_duration" required>
                        <option value="7">7 Days</option>
                        <option value="30">30 Days</option>
                        <option value="90">90 Days</option>
                        <option value="180">180 Days</option>
                        <option value="365">365 Days</option>
                    </select>
                </div>
                
                <button type="submit" name="create_account" class="cyber-button">
                    <i class="fas fa-user-plus"></i> Create Account
                </button>
            </form>
            
            <a href="profile.php" class="back-link">
                <i class="fas fa-arrow-left"></i> Back to Profile
            </a>
        </div>
    </div>
</body>
</html> 