<?php
error_reporting(E_ERROR | E_PARSE);
ini_set('display_errors', '1');
require_once '../../includes/functions.php';
chkSession();
if($user_id_2 == 1 || $user_level_2 == 'superadmin' || $user_level_2 == 'reseller' || $user_level_2 == 'developer'){
}else{
	echo "<script> swal({ title: 'Error!' , text: 'Sorry, you dont have permission to access this page.', button: false, closeOnClickOutside: false, icon: 'error' })  </script>";
	$db->RedirectToURL($db->base_url());
	exit;
}

if(!isset($_GET['uid']) || empty($_GET['uid'])){
	echo '<script>alert("Error");</script>';
	$db->RedirectToURL($db->base_url());
	exit;	
}else{
	$uid = $_GET['uid'];
	
    $sql = "SELECT * FROM users WHERE user_id!='$user_id_2' AND user_id='$uid'";
    $qry = $db->sql_query("$sql") OR die();
	$row = $db->sql_fetchrow($qry);
	$username = $row['user_name'];
	$password = $row['user_pass'];
	// Use single decryption to match database encryption
	$userpass = $db->encryptor('decrypt', $password);
	$userlevel = $row['user_level'];
	$userduration = $row['duration'];
	$userupline = $row['upline'];
	$device_id = $row['device_id'];
	$device_model = $row['device_model'];
	$is_activated = $row['is_active'];
	$is_freeze = $row['is_freeze'];
	$device_connected = $row['device_connected'];
	
	$sql_bio = "SELECT bio_link FROM users_profile WHERE profile_id='$uid'";
    $qry_bio = $db->sql_query("$sql_bio") OR die();
	$row_bio = $db->sql_fetchrow($qry_bio);
	$blink = isset($row_bio['bio_link']) ? $row_bio['bio_link'] : '';
	
	// Check if bio file exists before trying to open it
	$content = 'I am a freelance based in Saudi Arabia, passionate about career development and helping people find new roles.';
	$bioFilePath = $_SERVER['DOCUMENT_ROOT'] . "/profile/$uid/$blink";
	if(!empty($blink) && file_exists($bioFilePath)){
	    $file = fopen($bioFilePath, "r");
	    if($file !== false){
	        $content_ = fread($file, filesize($bioFilePath));
	        if(!empty($content_)){
	            $content = $content_;
	        }
	        fclose($file);
	    }
	}
	
	$dur = $db->calc_time($userduration);	
	$pdays = $dur['days'] . " days";
	$phours = $dur['hours'] . " hours";
	$pminutes = $dur['minutes'] . " minutes";
	$pseconds = $dur['seconds'] . " seconds";
	
	if($userduration == 0){
	    $duration = 'Expired';
	}else{
		$duration = date('Y-m-d H:i:s', time() + $userduration);
	}
	
	// Initialize user_level variable
	$user_level = 'Unknown';
	if($userlevel == 'normal'){
	    $user_level = 'Normal';
	}elseif($userlevel == 'bulk'){
	    $user_level = 'Bulk';
	}elseif($userlevel == 'trial'){
	    $user_level = 'Trial';
	}
	
	// Get Account Status
	$account_status = ($is_activated == 1) ? 'Active' : 'Inactive';
	
	// Get SocksIP Status
	$socksip_status = ($row['socksip_enabled'] == 1) ? 'Enabled' : 'Disabled';
	
	// Calculate OpenVPN and Xray usage from bandwidth_logs
	$openvpn_usage = 0;
	$xray_usage = 0;
	
	// Query bandwidth logs for OpenVPN usage (assuming OpenVPN connections are logged)
	$openvpn_qry = $db->sql_query("SELECT sum(bytes_sent + bytes_received) as total_bytes FROM bandwidth_logs WHERE username='$username' AND (server LIKE '%openvpn%' OR server_port IN ('1194', '110', '443'))");
	$openvpn_row = $db->sql_fetchrow($openvpn_qry);
	$openvpn_usage = isset($openvpn_row['total_bytes']) ? intval($openvpn_row['total_bytes']) : 0;
	
	// Query bandwidth logs for Xray usage
	$xray_qry = $db->sql_query("SELECT sum(bytes_sent + bytes_received) as total_bytes FROM bandwidth_logs WHERE username='$username' AND (server LIKE '%xray%' OR server_port IN ('443', '8443', '2053', '2083', '2087', '2096'))");
	$xray_row = $db->sql_fetchrow($xray_qry);
	$xray_usage = isset($xray_row['total_bytes']) ? intval($xray_row['total_bytes']) : 0;
	
	// Format usage to readable size
	function formatBytesForInfo($bytes) {
	    if ($bytes == 0) return '0 Bytes';
	    $units = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
	    $i = 0;
	    while ($bytes >= 1024 && $i < count($units) - 1) {
	        $bytes /= 1024;
	        $i++;
	    }
	    return round($bytes, 2) . ' ' . $units[$i];
	}
	
	$openvpn_usage_formatted = formatBytesForInfo($openvpn_usage);
	$xray_usage_formatted = formatBytesForInfo($xray_usage);
	
	if($device_model == ''){
	    $devicemodel = 'none';
	}else{
	    $devicemodel = $device_model;
	}
	
	if($device_connected == 1){
	    $expiration = $duration;
	}else{
	    $expiration = 'none';
	}
	
	if($is_freeze == 1){
	    $blockstat = 'Yes';
	    $user_freeze_status = 'Unblock';
	    $user_freeze_color = 'danger';
	    $proceed_block = 'unblock';
	    $confirm_block = 'dounblock';
	}else{
	    $blockstat = 'No';
	    $user_freeze_status = 'Block';
	    $user_freeze_color = 'primary';
	    $proceed_block = 'block';
	    $confirm_block = 'doblock';
	}
	
	$sql2 = "SELECT profile_image FROM users_profile WHERE profile_id!='$user_id_2' AND profile_id='$uid'";
    $qry2 = $db->sql_query("$sql2") OR die();
	$row2 = $db->sql_fetchrow($qry2);
	
	$prof2_image = isset($row2['profile_image']) ? $row2['profile_image'] : '';
	$prof_profile = 'profile/'.$uid.'/'.$prof2_image;
	$prof_default = 'profile/avatar-1.png';
	
	if($prof2_image == ''){
	    $upic = '<img class="rounded-circle profile-widget-picture" width="250" src="'.$prof_default.'" alt="default">';
	}else{
	    $upic = '<img class="rounded-circle profile-widget-picture" width="250" src="'.$prof_profile.'" alt="'.$username.'">';
	}
	
	$upline_qry = $db->sql_query("SELECT user_name FROM users WHERE user_id='$userupline'") OR die();
	$upline_row = $db->sql_fetchrow($upline_qry);
	$uupline = $upline_row['user_name'];
	
	//Get normal downlines
	$downline_normal = $db->sql_query("SELECT user_name FROM users WHERE user_id!=1 AND user_level='normal' AND upline='$uid'") OR die();
	$d_normal = $db->sql_numrows($downline_normal);
	
	//Get bulk downlines
	$downline_bulk = $db->sql_query("SELECT user_name FROM users WHERE user_id!=1 AND user_level='bulk' AND upline='$uid'") OR die();
	$d_bulk = $db->sql_numrows($downline_bulk);
	
	//Get trial downlines
	$downline_trial = $db->sql_query("SELECT user_name FROM users WHERE user_id!=1 AND user_level='trial' AND upline='$uid'") OR die();
	$d_trial = $db->sql_numrows($downline_trial);
	
	//Get reseller downlines
	$downline_reseller = $db->sql_query("SELECT user_name FROM users WHERE user_id!=1 AND user_level='reseller' AND upline='$uid'") OR die();
	$d_reseller = $db->sql_numrows($downline_reseller);
	
	//Get total downlines
	$d_total = $d_normal + $d_bulk + $d_trial + $d_reseller;
	
	$values = array();
	$valid = true;
    
	if($row){
		$values['username'] = $username;
		$values['userpass'] = $userpass;
		$values['subscription'] = $user_level;
		$values['expired'] = $expiration;
		$values['upline'] = $uupline;
		$values['device'] = $devicemodel;
		$values['blockstatus'] = $blockstat;
		$values['freezestatus'] = $user_freeze_status;
		$values['freezecolor'] = $user_freeze_color;
		$values['proceedblock'] = $proceed_block;
		$values['confirmblock'] = $confirm_block;
        $values['bio'] = $content;
        $values['account_status'] = $account_status;
        $values['socksip_status'] = $socksip_status;
        $values['openvpn_usage'] = $openvpn_usage_formatted;
        $values['xray_usage'] = $xray_usage_formatted;
        
        $values['dnormal'] = $d_normal;
        $values['dbulk'] = $d_bulk;
        $values['dtrial'] = $d_trial;
        $values['dreseller'] = $d_reseller;
        $values['dtotal'] = $d_total;
        
        $values['upic'] = $upic;
        
		$values['response'] = 1;
	}else{
		$values['response'] = 2;
	}
	if($valid == false){
		$values['response'] = 0;
	}
	echo json_encode($values);
}
?>