<?php

//////////////////////////////////////////////////////////////
//===========================================================
// ftpservers_theme.php
//===========================================================
// SOFTACULOUS VIRTUALIZOR
// Version : 1.0
// Inspired by the DESIRE to be the BEST OF ALL
// ----------------------------------------------------------
// Started by: Julien
// Date:       19th July 2012
// 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 backupservers_theme(){

global $theme, $globals, $kernel, $user, $l, $backupservers, $error, $done, $done_test, $request_test;

softheader($l['<title>']);
										
if(!empty($request_test)){
	echo $done_test;
	return true;
}

$buttons = array(
				0 => array(
					'name' => __('Add Backup Server'),
					'act'  => 'addbackupserver'
				)
			);

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

echo page_header(__('Backup Servers'), 'icon icon-servers','', 1, 0, $buttons);

error_handle($error);

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[

function Delbservers(bid ,ele){

	bid = bid || 0;
	
	// List of ids to delete
	var bserver_list = new Array();
	
	if(bid < 1){
		
		if($(ele).parent().prev().find("select").val() != 1){
			showalert("'.__J('No Action Selected !').'","1");
			return false;
		}
		
		$(".ios:checked").each(function() {
			bserver_list.push($(this).val());
		});
		
	}else{
		
		bserver_list.push(bid);
		
	}
	
	if(bserver_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}
	
	showalert("'.__J('Are you sure, you want to delete the Backup Server ?').'" ,"3" ,function(confirm){
		if(confirm == false){
			return false;
		}else{
			var finalData = new Object();
			finalData["delete"] = bserver_list.join(",");

			//alert(finalData);
			//return false;

			$("#progress_bar").show();

			$.ajax({
			type: "POST",
			url: "'.$globals['index'].'act=backupservers&api=json",
			data : finalData,
			dataType : "json",
			success: function(data){
				$("#progress_bar").hide();
				if("done" in data){
					setTimeout(function(){showalert("'.__J('The action was completed successfully. Reloading the Page !').'");
					$("#alert-modal").on("hidden.bs.modal", function (e) {
						location.reload(true);
					});} ,200);		
				}
				if("fatal_error_text" in data){
					setTimeout(function(){showalert(data["fatal_error_text"] ,"1");} ,200);
				}
				if("error" in data){
					setTimeout(function(){showalert(data["error"],"1");} ,200);
				}
				
			},
			error: function(data) {
				$("#progress_bar").hide();
				//alert(data.description);
				return false;
			}
			});
		}
	});
	
	
	
	
	return false;
};

$(document).ready(function(){
	$(".test-button").each(function(index){
		$(this).click(function(){	
			var parent = $(this).parent();
			var id =  parent.attr("id");
			$(this).find("i.fa-network-wired").addClass("fa-circle-notch fa-spin").removeClass("fa-network-wired");
			$.post("'.$globals['index'].'act=backupservers&jsnohf=1&test="+id, function(data){
				if(data == "1"){
					$(".backupserver-test").find("i.fa-circle-notch.fa-spin").addClass("fa-network-wired").removeClass("fa-circle-notch fa-spin").addClass("text-success");
					show_toast("'.__J('Connection successful').'", "'.__J('Successfully connected to the').' '.__J('View Backup Servers').'", 0);
				}
				else{
					$(".backupserver-test").find("i.fa-circle-notch.fa-spin").addClass("fa-network-wired").removeClass("fa-circle-notch fa-spin").addClass("text-danger");
					show_toast("'.__J('Connection Failed').'","'.__J('Unable to connect to the').' '.__J('View Backup Servers').'",3);
				}
			});
			
			return false;
		});
	});
});

// ]]></script>
<div class="card p-2 mx-3">
	<div id="showsearch" class="mb-3" style="display:'.(optREQ('search') || (!empty($backupservers) && !empty($globals['showsearch'])) ? "" : "none").';">
		<form accept-charset="'.$globals['charset'].'" name="bservers" method="GET" action="" class="form-horizontal">
			<input type="hidden" name="act" value="backupservers">
			<div class="form-group_head">
			<div class="row mx-auto w-100">
					<div class="col-sm-4"><label>'.__('Name').'</label>
					<input type="text" class="form-control" name="name" id="name" size="30" value="'.REQval('name','').'"/></div>
					<div class="col-sm-4"><label>'.__('Hostname').'</label>
					<input type="text" class="form-control" name="hostname" id="hostname" size="30" value="'.REQval('hostname','').'"/></div>
					<div class="col-sm-4"><label>'.__('Type').'</label>
					<input type="text" class="form-control" name="type" id="type" size="30" value="'.REQval('type','').'"/></div>
				</div>
				<div class="row mx-auto w-100 my-2">
				<div class="col-sm-12 text-center"><button type="submit" name="search" class="go_btn mt-2" value="Search"/>'.__('Search').'</button></div>
				</div>
			</div>
		</form>
	</div>';

	if(!empty($done)){
		show_alert(__('The Backup Server was deleted successfully'));
		//echo '<div class="notice"><b>NOTE : </b>&nbsp; '..'</div>';
	}

	if(empty($backupservers)){
		optREQ('search') ? show_alert(__('No results were found for your search query'), "2") : "";
		echo '<div class="notice"><b>NOTE : </b>&nbsp; '.(optREQ('search') ? __('No results were found for your search query') : __('There are no Backup Servers. <a href="index.php?act=addbackupserver">Add a Backup Server now</a>')).'</div>';
		
	}else{
	echo
	'<div class="row mx-auto w-100 mb-3">
		<div class="col-sm-4 col-lg-4 col-md-6 text-right d-flex align-items-center">
			<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 class="custom-select" name="bserver_task_select" id="bserver_task_select">
							<option value="0">---</option>
							<option value="1">'.__('Delete').'</option>
						</select>
					</div>
					<div class="pl-2">
						<input type="submit" id ="bserver_submit" class="go_btn" name="bserver_submit" value="'.__('Go').'" onclick="Delbservers(0 ,this); return false;">
					</div>
				</div>
			</div>
		</div>
		<div class="col-sm-8 col-lg-8 col-md-6 d-flex align-items-center justify-content-end p-0 mt-2 mt-md-0 mt-lg-0">';
			page_links($globals['num_res'], $globals['cur_page'], $globals['reslen']);
			echo get_num_records($globals['num_res']).'
		</div>
	</div>
	<form accept-charset="'.$globals['charset'].'" name="multi_bserver" id="multi_bserver" method="post" action="" class="form-horizontal">
		<div class="w-100 mx-auto sticky">
			<table class="table table-hover tablesorter common-table">
				<tr class="table_heading">
					<th class="min_width z-index-1">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th class="min_width text-left" >'.__('ID').'</th>
					<th align="center">'.__('Name').'</th>
					<th class="min_width" align="center">'.__('Type').'</th>
					<th align="center">'.__('Hostname').'</th>
					<th align="center">'.__('Username').'</th>
					<th class="text-center" width="8%">'.__('SSH Key').'</th>
					<th class="min_width" align="center">'.__('Port').'</th>
					<th class="min_width text-center" align="center">'.__('Test').'</th>
					<th class="min_width text-right" colspan="1">'.__('Manage').'</th>
				</tr>';
				$i = 1;
				foreach($backupservers as $k => $v){
					echo '<tr>
						<td class="min_width text-left">
							'.get_checkbox('backupserver_list[]', '', '', array('chk_it' => 0), $k).'
						</td>
						<td class="text-left">'.$v['bid'].'</td>
						<td>'.$v['name'].'</td>
						<td >'.$v['type'].'</td>
						<td>'.$v['hostname'].'</td>
						<td>'.$v['username'].'</td>
						<td align="center"><i class="fas fa-key '.(($v['ssh_key'] == 1) ? "text-success" : "text-danger").'" title="'.(($v['ssh_key'] == 1 ) ? __('SSH key is assigned') : __('SSH key is not assigned')).'"></i></td>
						<td>'.$v['port'].'</td>
						<td class="backupserver-test manage-ico cursor-pointer text-center" id="'.$v['bid'].'"><a class="test-button" title="'.__('Test Connection to Backup Server').'"><i class="fas text-admin fa-network-wired"></i></a></td>
						<td class="float-right">
							<div class="badge_container">
								<a class="badge badge-manage" href="?act=editbackupserver&id='.$v['bid'].'" title="Edit"><h6><i class="fas fa-pencil-alt"></i></h6></a>
								<a class="badge badge-trash" href="javascript:void(0);" onclick="return Delbservers('.$k.');" title="'.__('Delete').'"><h6><i class="fas fa-trash"></i></h6></a>
							</div>
						</tr>';
					$i++;
				}
		echo '</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 class="custom-select" name="bserver_task_select" id="bserver_task_select">
								<option value="0">---</option>
								<option value="1">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="bserver_submit" class="go_btn" name="bserver_submit" value="'.__('Go').'" onclick="Delbservers(0 ,this); return false;">
						</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>
	</form>
	<div id="progress_bar" style="display:none;">
		<br />
		<center>
			<font id="progress_txt" size="4">'.__('Please wait, Action in Progress.').'</font>
		</center>
		<div class="my-2" align="center">
			<div class="progress w-50">
				<div class="progress-bar bg-primary progress-bar-striped progress-bar-animated w-100"></div>
			</div>
		</div>
		<center>
			'.__('You will be notified once the Action is Completed.').'
		</center>
	</div>';
}

echo 
'<div class="text-center my-4">
	<a href="index.php?act=addbackupserver" class="go_btn">'.__('Add Backup Server').'</a>
</div>
</div><br>';

softfooter();

}