<?php

//////////////////////////////////////////////////////////////
//===========================================================
// servergroups_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 servergroups_theme(){

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

softheader($l['<title>']);
					
$buttons = array(
	0 => array(
		'name' => __('Add Server Group'),
		'act'  => 'addsg'
	)
);

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

echo page_header(__('Server Groups / Regions'), 'icon icon-servers','', 1, 0, $buttons);

error_handle($error);

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

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

function Delservergrp(sgid ,ele){

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

			//alert(finalData);
			//return false;
			
			$("#progress_bar").show();
			
			$.ajax({
				type: "POST",
				url: "'.$globals['index'].'act=servergroups&api=json",
				data : finalData,
				dataType : "json",
				success: function(data){
					$("#progress_bar").hide();
					if("done" in data){
						setTimeout(function(){showalert("'.__J('The action was completed sucessfully').'");
						$("#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);
					}
				},
				error: function(data) {
					$("#progress_bar").hide();
					//alert(data.description);
					return false;
				}
			});
		}
	});
	return false;
};

// ]]></script>
<div class="card mx-3 p-2">
	<div id="showsearch" class="mb-3" style="display:'.(optREQ('search') || (!empty($servergroups) && !empty($globals['showsearch'])) ? "" : "none").';">
		<form accept-charset="'.$globals['charset'].'" name="servergroup" method="GET" action="" class="form-horizontal">
			<input type="hidden" name="act" value="servergroups">
			<div class="form-group_head">
				<div class="row mx-auto w-100">
					<div class="col-sm-4"></div>
					<div class="col-sm-4"><label>'.__('Server Group Name').'</label><input type="text" class="form-control" name="sg_name" id="sg_name" size="30" value="'.REQval('sg_name', '').'" /></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($saved)){
	show_alert(__('The service has been restarted'));
	//echo '<div class="notice"><b>NOTE : </b>&nbsp; '..'</div><br />';
}

if(empty($servergroups)){
	optREQ('search') ? show_alert(__('No results were found for your search query'), "2") : show_alert(__('There are no Server groups <a href="index.php?act=addsg">Add a Server Group now</a>'), "2");
	echo '<br><br><div class="notice"><b>NOTE : </b>&nbsp; '.(optREQ('search') ? __('No results were found for your search query') : __('There are no Server groups <a href="index.php?act=addsg">Add a Server Group 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 name="servergrp_task_select" id="servergrp_task_select" class="custom-select">
							<option value="0">---</option>
							<option value="1">'.__('Delete').'</option>
						</select>
					</div>
					<div class="pl-2">
						<input type="submit" id ="servergrp_submit" class="go_btn" name="servergrp_submit" value="'.__('Go').'" onclick="Delservergrp(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(count($servergroups)).'
		</div>
	</div>
	<form accept-charset="'.$globals['charset'].'" name="multi_servergrp" id="multi_servergrp" 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="z-index-1 min_width" align="center">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th class="min_width" align="center" width="30">'.__('ID').'</th>
					<th align="center" width="90">'.__('Name').'</th>
					<th align="center" width="250">'.__('Description').'</th>
					<th align="center" width="80">'.__('Server Selection').'</th>
					<th align="center" width="100">'.__('Servers').'</th>
					<th width="50">'.__('Location').'</th>
					<th class="min_width text-center">'.__('HA').'</th>
					<th class="min_width text-right" colspan="2">'.__('Manage').'</th>
				</tr>';

			$i = 1;
			$sg_select_map = array(1 => 'space', 2 => 'ram', 3 => 'cores', 4 => 'ips', 5 => 'ipv6', 6 => 'ipv6sub', 7 => 'numvps',8 => 'sys_load');
			$sg_select_lang['sg_select_space'] = 'Most Available Space';
			$sg_select_lang['sg_select_ram'] = 'Most Available Ram';
			$sg_select_lang['sg_select_cores'] = 'Most Available Cores';
			$sg_select_lang['sg_select_ips'] = 'Most Available IPv4';
			$sg_select_lang['sg_select_ipv6'] = 'Most Available IPv6';
			$sg_select_lang['sg_select_ipv6sub'] = 'Most Available IPv6 Subnets';
			$sg_select_lang['sg_select_numvps'] = 'Least Number of '.VM_SHORT;
			$sg_select_lang['sg_select_sys_load'] = 'Least System Load';
			foreach($servergroups as $k => $v){

			echo '<tr>
					<td class="z-index-1 min_width">
					'.(empty($k) ? '' : ''.get_checkbox('servergroup_list[]', '', '', array('chk_it' => 0), $k)).'
					</td>
					<td>'.$v['sgid'].'</td>
					<td>'.$v['sg_name'].'</td>
					<td>'.$v['sg_desc'].'</td>
					<td>'.(empty($v['sg_select']) ? '--' : $sg_select_lang['sg_select_'.$sg_select_map[$v['sg_select']]]).'</td>
					<td style="word-break:break-all;">
    					'.(!empty($v['servers']) ? implode('<br>', $v['servers']) : '<em>'.__('None').'</em>').'</td>
					<td align="center"><img src="'.(!empty($v['sg_data'] && array_key_exists('location', $v['sg_data'])) ? location_flag($v['sg_data']['location']) : location_flag($globals['server_country_code'])).'" width="40"></td>
					<td align="center">'.(!empty($v['sg_ha']) ? '<i class="fas fa-check text-success"></i>' : '<i class="fas text-danger fa-times"></i>').'</td>
					<td class="text-center min_width">
						'.(empty($k) ? '' : '<div class="badge_container"><a class="badge badge-manage" href="'.$globals['ind'].'act=editsg&sgid='.$v['sgid'].'"><i class="fas fa-pencil-alt"></i></a><a class="badge badge-trash" href="javascript:void(0);" onclick="return Delservergrp('.$k.');"><i class="fas fa-trash"></i></a>').'
					</td>
				</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 name="servergrp_task_select" id="servergrp_task_select" class="custom-select">
								<option value="0">---</option>
								<option value="1">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="servergrp_submit" class="go_btn" name="servergrp_submit" value="'.__('Go').'" onclick="Delservergrp(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=addsg" class="link_btn">'.__('Add Server Group').'</a>
	</div>
</div>
';

softfooter();

}

?>