<?php

//////////////////////////////////////////////////////////////
//===========================================================
// support_access_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Alons
// Date:       8th Mar 2010
// Time:       23:00 hrs
// Site:       https://www.virtualizor.com/ (SOFTACULOUS VIRTUALIZOR)
// ----------------------------------------------------------
// Please Read the Terms of use at https://www.virtualizor.com
// ----------------------------------------------------------
//===========================================================
// (c)Softaculous Ltd.
//===========================================================
//////////////////////////////////////////////////////////////

if(!defined('VIRTUALIZOR')){

	die('Hacking Attempt');

}

function support_access_theme(){

global $theme, $globals, $cluster, $user, $l, $support, $servers, $error, $done; 

softheader($l['<title>']);

$GLOBALS['breadcrumbs'] = array('dashboard', 'lms_support_access');

echo page_header($l['<title>'], 'fas fa-life-ring', '', 0, 0, '', '');

error_handle($error);

if(!empty($done)){
	show_alert($done['msg'], '', 'support_access');
}

echo '
<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
	
	function action_chosen(action, serid ,ele){
		
		action = action || "";
		serid = serid || 0;
		
		if(action == ""){
			if($(ele).parent().prev().find("select").val() == 0){
					showalert("'.__J('No action provided').'","1");
					return false;
			}else{
				action = $(ele).parent().prev().find("select").val();
			}
		}
		
		// List of ids to delete
		var key_list = new Array();
		
		if($(".ios:checked").length > 0){
			
			$(".ios:checked").each(function(){
				key_list.push($(this).val());
			});
			
		}else{
			
			key_list.push(serid);
			
		}
		
		if(key_list.length < 1){
			showalert("'.__J('Nothing Selected').'","1");
			return false;
		}
		
		// To delete the user
		if(action == "delete"){
			
			showalert("'.__J('Are you sure you want to revoke the support access ?').'" ,"3" ,function(confirm){
				if(confirm == false){
					return false;
				}else{
					var finalData = new Object();
					finalData["revoke_access"] = key_list.join(",");
					perform_action(finalData);
				}
			});
			
		}else{
			showalert("'.__J('No action provided').'","1");
			return false;
		}
		
		return false;
	}
	
	function perform_action(finalData, serid = 0){
		
		//alert(finalData);
		//return false;
		
		$("#progress_bar").show();
		$.ajax({
			type: "POST",
			url: "'.$globals['index'].'act=support_access&api=json",
			data : finalData,
			dataType : "json",
			success: function(data){
				$("#progress_bar").hide();
				if(!empty(serid)){
					$("#showload_"+serid).hide();
				}
				if("done" in data){
					setTimeout(function(){showalert(data["done"]["msg"]);
						$("#alert-modal").on("hidden.bs.modal", function (e) {
						location.reload(true);
					});} ,800);
				}
				
				if("error" in data){
					setTimeout(function(){showalert(data["error"],"1");
						$("#alert-modal").on("hidden.bs.modal", function (e) {
						location.reload(true);
					});} ,800);
				}
			},
			error: function(data) {
				if(!empty(serid)){
					$("#showload_"+serid).hide();
				}
				$("#progress_bar").hide();
				return false;
			}
		});
		
		return false;
	}
	
	// Make server list
	function make_server_list() {
		var serverValues = [];
		serverValues.push($("#server_access").val());
		$("#server_access_val").val(serverValues.toString());
		if($("#server_access_val").val() == ""){
			$("#server_access_val").val(-1);
		}
	}

	$(document).ready(function(){
		make_server_list();
		$("#server_access").on("change", make_server_list);
	});
	
// ]]></script>
<div id="form-container" class="card p-2 mx-3">
	<form accept-charset="'.$globals['charset'].'" action="" method="post" name="changehost" class="form-horizontal">
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('Select Server').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('Select server to grant access').'"><i class="fas text-primary fa-info-circle"></i></a></label>
			</div>
			<div class="col-sm-4">
				<input type="hidden" id="server_access_val" name="server_access">
				<select class="virt-select custom-select" id="server_access" multiple data-placeholder="'.__('Select Server').'">';
					foreach($servers as $key => $id) {
						if(!empty($support[$key])) continue;
						echo '<option value="'.$key.'" '.(empty($key) ? "selected" : '').'>'.$id['server_name'].'</option>';
					}
				echo '
				</select>
			</div>
		</div>
		<div class="row mx-auto w-100 my-3">
			<div class="col-sm-4">
				<label class="control-label">'.__('Access Hours').'</label><br>
				<span style="font-size:12px" class="text-secondary">'.__('Select hours for access after which, the support access will be automatically revoked ! By default <b>7 days</b> of access will be granted. Maximum <b>30 Days</b> allowed').'</span>
			</div>
			<div class="col-sm-4">
				<input type="text" class="form-control" name="access_hours" id="access_hours" size="30" value="'.POSTval('access_hours').'" />
				<span class="help-block"></span>
			</div>
		</div>
			<center>
				<input type="submit" value="'.__('Grant Access').'" class="go_btn" name="grant_access" />
			</center>
			</br></br>
	</form></br></br>';

if(!empty($support)){
// r_print($support);
echo '<div class="w-100 mx-auto sticky">
	<table class="table table-hover tablesorter common-table">
		<tr class="table_heading">
			<th class="z-index-1 min_width" align="center">
				'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
			</th>
			<th align="center">'.__('Server ID').'</th>
			<th align="center">'.__('Username').'</th>
			<th align="center">'.__('Server Name').'</th>
			<th align="center">'.__('Access granted time').'</th>
			<th align="center">'.__('Time to Revoke access').'</th>
			<th class="text-right pr-3" colspan="3">'.__('Action').'</th>
		</tr>
		<tbody>';

		foreach($support as $k => $v){	
			echo '
			<tr>
				<td valign="middle" align="center">
					'.get_checkbox('user_list[]', '', '', array('chk_it' => 0), $k).'
				</td>
				<td>'.$k.'</td>
				<td width="120" align="center">'.$v['username'].'</td>
				<td>'.$servers[$k]['server_name'].'<div class="ml-2 loader" id="showload_'.$k.'" style="display:none;"></div></td>
				<td>'.$v['installed_time'].'</td>
				<td>'.$v['revoke_time'].'</td>
				<td width="20" valign="middle" align="center">
					<div class="badge badge_container">
						<a class="badge badge-trash" href="javascript:void(0);" onclick="action_chosen(\'delete\', '.$k.', this);" title="'.__('Revoke Access').'"><i class="fas fa-trash"></i></a>
					</div>
				</td>
			</tr>';
		}
		
		echo '</tbody></table>
		</div>
		<div class="row mx-auto w-100 mt-5">
		<div class="col-sm-4 col-lg-4 col-md-6 text-right mb-3">
			<div class="bottom-menu float-right w-100">
				<div class="row my-auto">
					<div class="p-0">
						<label>'.__('With Selected').'</label>
					</div>
					<div class="col p-0">
						<select name="user_task_select" class="custom-select" id="user_task_select">
							<option value="0">---</option>
							<option value="delete">'.__('Revoke Access').'</option>
						</select>
					</div>
					<div class="pl-2">
						<button type="submit" class="go_btn" id="user_submit" name="user_submit" onclick="action_chosen(\'\', 0, this); return false;">'.__('Go').'</button>
					</div>
				</div>
			</div>
		</div>
		<div class="col-sm-8 col-lg-8 col-md-6 mb-3 p-0">';
			page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
		echo'
		</div>
	</div>';
}// End of $support

echo '</div>';

softfooter();

}


