<?php
include("header.php");

// Check if user is VIP
$check_tier = mysqli_query($condb, "SELECT license_tier FROM users WHERE username = '".$_SESSION['username']."'");
$user_tier = mysqli_fetch_assoc($check_tier);

if ($user_tier['license_tier'] !== 'vip') {
    header("Location: index.php");
    exit('Access Denied: VIP features only');
}

// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['action'])) {
        switch ($_POST['action']) {
            case 'start_attack':
                $target = mysqli_real_escape_string($condb, $_POST['target']);
                $port = intval($_POST['port']);
                $duration = intval($_POST['duration']);
                $method = mysqli_real_escape_string($condb, $_POST['method']);
                
                // Add attack to database
                $add_attack = $condb->prepare("INSERT INTO ddos_attacks (username, target, port, duration, method, status, start_time) VALUES (?, ?, ?, ?, ?, 'running', NOW())");
                $add_attack->bind_param("ssiis", $_SESSION['username'], $target, $port, $duration, $method);
                
                if ($add_attack->execute()) {
                    echo '<div class="cyber-notification success"><i class="fas fa-check-circle"></i> Attack started successfully!</div>';
                } else {
                    echo '<div class="cyber-notification error"><i class="fas fa-exclamation-circle"></i> Error starting attack: ' . $condb->error . '</div>';
                }
                break;
                
            case 'stop_attack':
                $attack_id = intval($_POST['attack_id']);
                
                // Update attack status
                $stop_attack = $condb->prepare("UPDATE ddos_attacks SET status = 'stopped', end_time = NOW() WHERE id = ? AND username = ?");
                $stop_attack->bind_param("is", $attack_id, $_SESSION['username']);
                
                if ($stop_attack->execute()) {
                    echo '<div class="cyber-notification success"><i class="fas fa-check-circle"></i> Attack stopped successfully!</div>';
                } else {
                    echo '<div class="cyber-notification error"><i class="fas fa-exclamation-circle"></i> Error stopping attack: ' . $condb->error . '</div>';
                }
                break;
        }
    }
}

// Get active attacks
$active_attacks = mysqli_query($condb, "SELECT * FROM ddos_attacks WHERE username = '".$_SESSION['username']."' AND status = 'running' ORDER BY start_time DESC");
?>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="cyber-panel">
                <div class="cyber-panel-header">
                    <div class="cyber-title">
                        <i class="fas fa-bolt cyber-icon"></i>
                        <h3>DDoS Control Panel</h3>
                        <div class="cyber-title-decoration"></div>
                    </div>
                </div>
                <div class="cyber-panel-body">
                    <div class="cyber-alert">
                        <i class="fas fa-info-circle"></i>
                        <span>Configure and launch DDoS attacks against your targets. Monitor active attacks and their status.</span>
                    </div>

                    <!-- Attack Configuration Form -->
                    <div class="cyber-form">
                        <div class="section-header">
                            <div class="header-icon">
                                <i class="fas fa-crosshairs"></i>
                            </div>
                            <div class="header-content">
                                <h4>Launch Attack</h4>
                                <p class="header-description">Configure your attack parameters</p>
                            </div>
                        </div>

                        <form method="POST" action="">
                            <input type="hidden" name="action" value="start_attack">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-crosshairs"></i> Target</label>
                                        <input type="text" name="target" class="form-control" placeholder="IP or Domain" required>
                                    </div>
                                    <div class="form-group">
                                        <label><i class="fas fa-network-wired"></i> Port</label>
                                        <input type="number" name="port" class="form-control" min="1" max="65535" value="80" required>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-clock"></i> Duration (seconds)</label>
                                        <input type="number" name="duration" class="form-control" min="1" max="3600" value="60" required>
                                    </div>
                                    <div class="form-group">
                                        <label><i class="fas fa-code"></i> Method</label>
                                        <select name="method" class="form-control" required>
                                            <option value="tcp">TCP Flood</option>
                                            <option value="udp">UDP Flood</option>
                                            <option value="http">HTTP Flood</option>
                                            <option value="syn">SYN Flood</option>
                                            <option value="icmp">ICMP Flood</option>
                                            <option value="slowloris">Slowloris</option>
                                            <option value="amplification">DNS Amplification</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-tachometer-alt"></i> Attack Power</label>
                                        <select name="power" class="form-control" required>
                                            <option value="low">Low (Stealth)</option>
                                            <option value="medium" selected>Medium (Balanced)</option>
                                            <option value="high">High (Maximum)</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label><i class="fas fa-random"></i> Packet Size</label>
                                        <input type="number" name="packet_size" class="form-control" min="64" max="65507" value="1024" required>
                                        <small class="form-text text-muted">Size in bytes (64-65507)</small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label><i class="fas fa-globe"></i> Proxy Mode</label>
                                        <select name="proxy_mode" class="form-control" required>
                                            <option value="none">No Proxy</option>
                                            <option value="tor">TOR Network</option>
                                            <option value="vpn">VPN Chain</option>
                                            <option value="rotating">Rotating Proxies</option>
                                        </select>
                                    </div>
                                    <div class="form-group">
                                        <label><i class="fas fa-shield-alt"></i> Anti-Detection</label>
                                        <select name="anti_detection" class="form-control" required>
                                            <option value="basic">Basic</option>
                                            <option value="advanced" selected>Advanced</option>
                                            <option value="stealth">Stealth Mode</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label><i class="fas fa-cogs"></i> Advanced Options</label>
                                        <div class="advanced-options">
                                            <div class="form-check">
                                                <input type="checkbox" name="auto_retry" id="auto_retry" class="form-check-input" checked>
                                                <label class="form-check-label" for="auto_retry">Auto-retry on failure</label>
                                            </div>
                                            <div class="form-check">
                                                <input type="checkbox" name="random_packets" id="random_packets" class="form-check-input" checked>
                                                <label class="form-check-label" for="random_packets">Random packet patterns</label>
                                            </div>
                                            <div class="form-check">
                                                <input type="checkbox" name="spoof_ip" id="spoof_ip" class="form-check-input">
                                                <label class="form-check-label" for="spoof_ip">IP Spoofing</label>
                                            </div>
                                            <div class="form-check">
                                                <input type="checkbox" name="notifications" id="notifications" class="form-check-input" checked>
                                                <label class="form-check-label" for="notifications">Enable notifications</label>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="form-actions">
                                <button type="submit" class="btn btn-primary"><i class="fas fa-play"></i> Start Attack</button>
                            </div>
                        </form>
                    </div>

                    <!-- Attack Statistics -->
                    <div class="cyber-table-container">
                        <div class="section-header">
                            <div class="header-icon">
                                <i class="fas fa-chart-bar"></i>
                            </div>
                            <div class="header-content">
                                <h4>Attack Statistics</h4>
                                <p class="header-description">Real-time attack performance metrics</p>
                            </div>
                        </div>

                        <div class="stats-grid">
                            <div class="stat-card">
                                <div class="stat-icon">
                                    <i class="fas fa-bolt"></i>
                                </div>
                                <div class="stat-content">
                                    <h5>Packets Sent</h5>
                                    <div class="stat-value">1.2M</div>
                                    <div class="stat-trend up">
                                        <i class="fas fa-arrow-up"></i> 15.2%
                                    </div>
                                </div>
                            </div>

                            <div class="stat-card">
                                <div class="stat-icon">
                                    <i class="fas fa-tachometer-alt"></i>
                                </div>
                                <div class="stat-content">
                                    <h5>Attack Speed</h5>
                                    <div class="stat-value">45.5 Gbps</div>
                                    <div class="stat-trend up">
                                        <i class="fas fa-arrow-up"></i> 8.7%
                                    </div>
                                </div>
                            </div>

                            <div class="stat-card">
                                <div class="stat-icon">
                                    <i class="fas fa-network-wired"></i>
                                </div>
                                <div class="stat-content">
                                    <h5>Active Connections</h5>
                                    <div class="stat-value">12.5K</div>
                                    <div class="stat-trend neutral">
                                        <i class="fas fa-minus"></i> Stable
                                    </div>
                                </div>
                            </div>

                            <div class="stat-card">
                                <div class="stat-icon">
                                    <i class="fas fa-clock"></i>
                                </div>
                                <div class="stat-content">
                                    <h5>Uptime</h5>
                                    <div class="stat-value">98.5%</div>
                                    <div class="stat-trend up">
                                        <i class="fas fa-arrow-up"></i> 2.3%
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Active Attacks Table -->
                    <div class="cyber-table-container">
                        <div class="section-header">
                            <div class="header-icon">
                                <i class="fas fa-chart-line"></i>
                            </div>
                            <div class="header-content">
                                <h4>Active Attacks</h4>
                                <p class="header-description">Monitor your running attacks</p>
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>Target</th>
                                        <th>Port</th>
                                        <th>Method</th>
                                        <th>Duration</th>
                                        <th>Start Time</th>
                                        <th>Status</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($attack = mysqli_fetch_assoc($active_attacks)): ?>
                                    <tr class="crypto-row">
                                        <td><?php echo htmlspecialchars($attack['target']); ?></td>
                                        <td><?php echo $attack['port']; ?></td>
                                        <td><?php echo strtoupper($attack['method']); ?></td>
                                        <td><?php echo $attack['duration']; ?>s</td>
                                        <td><?php echo date('Y-m-d H:i:s', strtotime($attack['start_time'])); ?></td>
                                        <td><span class="status-badge status-active">Running</span></td>
                                        <td>
                                            <form method="POST" action="" style="display: inline;">
                                                <input type="hidden" name="action" value="stop_attack">
                                                <input type="hidden" name="attack_id" value="<?php echo $attack['id']; ?>">
                                                <button type="submit" class="btn btn-xs btn-warning"><i class="fas fa-stop"></i> Stop</button>
                                            </form>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
body {
    background: #13131f;
}

.container-fluid {
    background: #13131f;
}

.cyber-panel {
    background: rgba(19, 19, 31, 0.95);
    border: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 15px;
    box-shadow: 0 0 30px rgba(0, 255, 255, 0.1);
}

.cyber-panel-header {
    background: rgba(0, 0, 0, 0.3);
    border-bottom: 1px solid rgba(0, 255, 255, 0.2);
    padding: 20px;
    border-radius: 15px 15px 0 0;
}

.cyber-title {
    display: flex;
    align-items: center;
    gap: 15px;
}

.cyber-title h3 {
    color: #00f3ff;
    font-family: 'Orbitron', sans-serif;
    margin: 0;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.cyber-icon {
    color: #00f3ff;
    font-size: 24px;
}

.cyber-title-decoration {
    flex-grow: 1;
    height: 2px;
    background: linear-gradient(90deg, rgba(0, 243, 255, 0.2), rgba(0, 243, 255, 0));
}

.cyber-panel-body {
    padding: 20px;
}

.cyber-alert {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.cyber-alert i {
    color: #00f3ff;
    font-size: 18px;
}

.cyber-alert span {
    color: #ffffff;
    font-size: 14px;
}

.cyber-form {
    background: rgba(19, 19, 31, 0.85);
    border: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 15px;
    padding: 20px;
    margin-bottom: 30px;
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
}

.cyber-table-container {
    background: rgba(19, 19, 31, 0.85);
    border: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
}

.section-header {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid rgba(0, 255, 255, 0.1);
}

.header-icon {
    width: 40px;
    height: 40px;
    background: rgba(0, 255, 255, 0.1);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.header-icon i {
    font-size: 20px;
    color: #00f3ff;
}

.header-content h4 {
    color: #00f3ff;
    margin: 0;
    font-family: 'Orbitron', sans-serif;
    font-size: 18px;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.header-description {
    color: #ffffff;
    margin: 5px 0 0;
    font-size: 14px;
    opacity: 0.7;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: flex;
    align-items: center;
    color: #00f3ff;
    font-family: 'Orbitron', sans-serif;
    font-size: 14px;
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.form-group label i {
    margin-right: 8px;
}

.form-control {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(0, 255, 255, 0.2);
    color: #ffffff;
    border-radius: 8px;
    padding: 12px 15px;
    transition: all 0.3s ease;
}

.form-control:focus {
    border-color: #00f3ff;
    box-shadow: 0 0 15px rgba(0, 243, 255, 0.3);
    outline: none;
    background: rgba(0, 0, 0, 0.3);
}

.table {
    color: #ffffff;
    background: transparent;
}

.table thead th {
    background: rgba(0, 0, 0, 0.3);
    color: #00f3ff;
    border-bottom: 2px solid rgba(0, 255, 255, 0.2);
    text-transform: uppercase;
    font-size: 12px;
    letter-spacing: 1px;
    padding: 15px;
}

.crypto-row {
    background: rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
    border-bottom: 1px solid rgba(0, 255, 255, 0.05);
}

.crypto-row:hover {
    background: rgba(0, 255, 255, 0.05);
    transform: translateX(5px);
}

.crypto-row td {
    padding: 15px;
    border-color: rgba(0, 255, 255, 0.1);
}

.status-badge {
    display: inline-block;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: center;
    min-width: 80px;
}

.status-active {
    background: rgba(0, 255, 128, 0.15);
    color: #00ff80;
    border: 1px solid rgba(0, 255, 128, 0.3);
}

.btn-primary {
    background: linear-gradient(45deg, #00f3ff, #0066ff);
    border: none;
    padding: 12px 25px;
    border-radius: 8px;
    color: #ffffff;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1px;
    transition: all 0.3s ease;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 0 20px rgba(0, 243, 255, 0.4);
    background: linear-gradient(45deg, #00f3ff, #0066ff);
}

.btn-warning {
    background: linear-gradient(45deg, #ffa500, #ff6600);
    border: none;
}

.btn-warning:hover {
    background: linear-gradient(45deg, #ffa500, #ff6600);
    box-shadow: 0 0 20px rgba(255, 165, 0, 0.4);
}

.form-actions {
    margin-top: 30px;
    display: flex;
    justify-content: flex-end;
}

.cyber-notification {
    padding: 15px;
    margin-bottom: 20px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.cyber-notification.success {
    background: rgba(0, 255, 128, 0.1);
    border: 1px solid rgba(0, 255, 128, 0.2);
    color: #00ff80;
}

.cyber-notification.error {
    background: rgba(255, 68, 68, 0.1);
    border: 1px solid rgba(255, 68, 68, 0.2);
    color: #ff4444;
}

/* Ensure no white backgrounds in any element */
* {
    background-color: transparent;
}

/* Override any Bootstrap default backgrounds */
.table-striped > tbody > tr:nth-of-type(odd) {
    background-color: rgba(0, 0, 0, 0.3);
}

.table-striped > tbody > tr:nth-of-type(even) {
    background-color: rgba(0, 0, 0, 0.2);
}

/* Ensure form elements have dark backgrounds */
select.form-control,
input[type="text"].form-control,
input[type="password"].form-control,
input[type="email"].form-control,
input[type="number"].form-control {
    background-color: rgba(0, 0, 0, 0.3);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-top: 20px;
}

.stat-card {
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid rgba(0, 255, 255, 0.2);
    border-radius: 10px;
    padding: 20px;
    display: flex;
    align-items: center;
    gap: 15px;
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-5px);
    border-color: rgba(0, 255, 255, 0.4);
    box-shadow: 0 0 20px rgba(0, 255, 255, 0.1);
}

.stat-icon {
    width: 50px;
    height: 50px;
    background: rgba(0, 255, 255, 0.1);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.stat-icon i {
    font-size: 24px;
    color: #00f3ff;
}

.stat-content {
    flex-grow: 1;
}

.stat-content h5 {
    color: #00f3ff;
    font-family: 'Orbitron', sans-serif;
    font-size: 14px;
    margin: 0 0 5px 0;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.stat-value {
    color: #ffffff;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 5px;
}

.stat-trend {
    font-size: 12px;
    display: flex;
    align-items: center;
    gap: 5px;
}

.stat-trend.up {
    color: #00ff80;
}

.stat-trend.down {
    color: #ff4444;
}

.stat-trend.neutral {
    color: #00f3ff;
}

.stat-trend i {
    font-size: 10px;
}

.advanced-options {
    background: rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(0, 255, 255, 0.1);
    border-radius: 8px;
    padding: 15px;
    margin-top: 10px;
}

.form-check {
    margin-bottom: 10px;
}

.form-check:last-child {
    margin-bottom: 0;
}

.form-check-input {
    background-color: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(0, 255, 255, 0.2);
}

.form-check-input:checked {
    background-color: #00f3ff;
    border-color: #00f3ff;
}

.form-check-label {
    color: #ffffff;
    margin-left: 8px;
}

.form-check-input:focus {
    box-shadow: 0 0 0 0.2rem rgba(0, 243, 255, 0.25);
}
</style>

<?php include("footer.php"); ?> 