<?php
	include("header.php");
	
	$uname = isset($_SESSION['name']) ? $_SESSION['name'] : 'user';
	$utype = isset($_SESSION['user_type']) ? $_SESSION['user_type'] : 'user';
	
	
	
	

	$db = new Connection();
	$resU = $db->Query("select username,test_password  from users order by username asc");
	foreach($resU as $row) {
		$user_name = $row['username'];
		$pass = $row['test_password'];
		$userArray[$user_name] = trim($pass);
	}
	
	
	uksort($userArray,'strcasecmp');

	$user_name = $productArray = $scheduleArr = array();
	$res = $db->Query("select id, username from users Where disabled != '1' order by username asc");
	foreach($res as $row) {
		$user_id = $row['id'];
		$user_name = $row['username'];
		$user_pass = '';
		if(isset($userArray[$user_name])) {
			$user_pass = $userArray[$user_name];
		}
		$userNewArr[$user_id][$user_name] = $user_pass;
	}
//	echo  ($_SESSION['user_type'] == 'superadmin') ? '<a href="login2.php" class="btn btn-success mb-2 btn-sm">Login Style Table 2</a>':'';
	print '<table border=2>';
	print '<tr>
				<td style="font-weight:bold;width:80px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;font-size: 120%;" align="center">Sr. No.</td>
				<td style="font-weight:bold;width:55px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;font-size: 120%;" align="center">ID</td>
				<td style="font-weight:bold;width:200px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;font-size: 120%;" align="center">Name</td>
				
			</tr>';
	print '</table>';
	print '<table border=2>';
	$c=0;
	$sr_no = 0;
	foreach ($userNewArr as $user_id=>$credential) {
		$rundate = $start_date = $server = null;
		
		foreach($credential as $key=>$value) {
			$sr_no++;
			//print "$key ## $value<br>";
			$encodedString = base64_encode($value);
		echo '<tr>
				<td style="font-weight:bold;width:81px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;" align="center">'.$sr_no.'</td>
				<td><form id="toggleForm" action="https://www.mschannellogin.com/users/login/" method="Post" target="top">
				<input name="data[User][name]" type="hidden" value="'.$uname.'" />
				<input name="data[User][utype]" type="hidden" value="'.$utype.'" />
				<input name="data[User][username]" type="hidden" value="'.$key.'" />
				<input name="data[User][password]" id="ps_'.$sr_no.'" type="hidden" value="'.$encodedString.'" />
				<input style="font-weight:bold;width:50px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;" value="'.$user_id.'" />
				<input style="font-weight:bold;width:190px;color:#000000;background-color:#e8e8e8;margin:2px;padding:2px;"  onclick="decryptPassword(\''.$encodedString.'\', \'ps_'.$sr_no.'\')" type="submit" value="'.ucfirst($key).'" /> 
			</form></td>';
			
			
		}
	}
	print '</table>';
	
	
	
	
?>
<script>
  $(document).ready(function () {
    let isTest = true; // Track the current mode

    $("#toggleUrl").click(function (event) {
        event.preventDefault(); // Prevent immediate submission

        const form = $("#toggleForm");
        const button = $("#toggleUrl");

        if (isTest) {
            form.attr("action", "https://www.mschannellogin.com/users/login/"); // Change form action to live
            button.text("Change Form Url To Live Server"); // Change button name to Live
        } else {
            form.attr("action", "https://www.mysammsecure.com/users/login/"); // Change form action to test
            button.text("Change Form Url To Test Server"); // Change button name to Test
        }

        isTest = !isTest; // Toggle the state
    });
});

  function decryptPassword(encodedString, inputId) {
    let decodedString = atob(encodedString); 
    document.getElementById(inputId).value = decodedString;
}
</script>