<?php

//////////////////////////////////////////////////////////////
//===========================================================
// plans_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 getSortHeader($header_name, $column ,$size = 80){

global $theme, $globals;
	$str = '<div class="d-inline-flex align-items-center" style="width:'.$size.'px;">
		<div class="mr-2">
			<span class="" >'.$header_name.'</span>
		</div>
		<span class="sorter">
			<a class="text-always-white" href="'.$globals['index'].'act=plans&sortcolumn='.$column.'&sortby=asc"><i class="fas fa-sort-up"></i></a><br>
			<a class="text-always-white" href="'.$globals['index'].'act=plans&sortcolumn='.$column.'&sortby=desc"><i class="fas fa-sort-down"></i></a>
		</span>
	</div>';
	return $str;
}

function plans_theme(){

global $theme, $globals, $kernel, $user, $l, $plans, $error, $done;

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

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

echo page_header(__('Plans'), 'fas fa-clipboard', $globals['docs'].'adding-a-plan', 1, 0, $buttons);

error_handle($error);

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

function action_chosen(action, plid , ele){

	plid = plid || 0;
	action = action || "";
	
	if(action == ""){
		if($(ele).parent().prev().find("select").val() == 0){
				showalert("'.__J('No action selected !').'","1");
				return false;
		}else{
			action = $(ele).parent().prev().find("select").val();
		}
	}

	// List of ids
	var plan_list = new Array();

	if(plid < 1){
		$(".ios:checked").each(function(){
			plan_list.push($(this).val());
		});
	}else{	
		plan_list.push(plid);
	}
	
	if(plan_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}
	// To delete plans
	if(action == "delete"){
		showalert("'.__J('Are you sure want to Delete the Plan ?').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				finalData["delete"] = plan_list.join(",");
				exec(finalData);
			}
		});
	// To enable plans	
	}else if(action == "enable"){
		showalert("'.__J('Are you sure want to Enable the Plan ?').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				finalData["enable"] = plan_list.join(",");
				exec(finalData);
			}
		});
	// To disable plans	
	}else if(action == "disable"){
		showalert("'.__J('Are you sure want to Disable the Plan ?').'","3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				finalData["disable"] = plan_list.join(",");
				exec(finalData);
			}
		});
	}else{
		showalert("'.__J('No action selected !').'","1");
		return false;
	}
	
	//alert(finalData);
	//return false;
	return false;
};

function exec(finalData){
	$("#progress_bar").show();
	$.ajax({
		type: "POST",
		url: "'.$globals['index'].'act=plans&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("error" in data){
				var errstr = "'.__J('The following errors were found').'";
				for(x in data["error"]){
					errstr = errstr+"\n"+data["error"][x];
				}
				setTimeout(function(){showalert(errstr,"1");} ,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;
		}
	});
};

// ]]></script>
<div class="card p-2 mx-3">
	<div id="showsearch" class="mb-3" style="display:'.(optREQ('search') || (!empty($plans) && !empty($globals['showsearch'])) ? "" : "none").';">
		<form accept-charset="'.$globals['charset'].'" name="plans" id="plans" method="GET" action="" class="form-horizontal">
			<input type="hidden" name="act" value="plans">
			<div class="form-group_head">
			<div class="row mx-auto w-100">
				<div class="col-sm-3"></div>
				<div class="col-sm-3"><label>'.__('Plan Name').'</label>
				<input type="text" class="form-control" name="planname" id="planname" size="30" value="'.REQval('planname','').'"/></div>
				<div class="col-sm-3"><label>'.__('Plan Type').'</label>
				<select name="ptype" class="custom-select w-100">
					<option value="" '.REQselect('ptype', '', true).'>All</option>
					<option value="xen" '.REQselect('ptype', 'xen').'>Xen</option>
					<option value="kvm" '.REQselect('ptype', 'kvm').'>KVM</option>
					<option value="xcp" '.REQselect('ptype', 'xcp').'>Xen Server</option>
					<option value="lxc" '.REQselect('ptype', 'lxc').'>LXC</option>
					<option value="vzo" '.REQselect('ptype', 'vzo').'>Virtuozzo OpenVZ</option>
					<option value="vzk" '.REQselect('ptype', 'vzk').'>Virtuozzo KVM</option>
					<option value="docker" '.REQselect('ptype', 'docker').'>Docker</option>
					<option value="baremetal" '.REQselect('ptype', 'baremetal').'>Baremetal</option>
				</select>
				</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(__('Plan was deleted successfully'));
	}

	if(empty($plans)){
		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 Plans. <a href="index.php?act=addplan">Add a Plan 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="plans_task_select" id="plans_task_select">
							<option value="0">---</option>
							<option value="delete">'.__('Delete').'</option>
							<option value="enable">'.__('Enable').'</option>
							<option value="disable">'.__('Disable').'</option>
						</select>
					</div>
					<div class="pl-2">
						<input type="submit" id ="plans_submit" class="go_btn" name="plans_submit" value="'.__('Go').'" onclick="action_chosen(\'\' ,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_plans" id="multi_plans" 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">
						'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
					</th>
					<th align="center" class="min_width">
						'.getSortHeader(__('ID'), 'plid' ,30).'
					</th>
					<th align="center" class="mw-20">'.getSortHeader(__('Name'), 'plan_name' ,51).'</th>
					<th class="min_width">
					<div class="w-100" align="center">'.__('Type').'</th>
					<th align="center" class="min_width">'.getSortHeader(__('Space'), 'space' ,55).'</th>
					<th align="center" class="min_width">'.getSortHeader(__('RAM'), 'ram' ,42).'</th>
					<th align="center" class="min_width">'.__('Burst').'</th>
					<th align="center" class="min_width">'.getSortHeader(__('CPU Units'), 'cpu').'</th>
					<th align="center" class="min_width">'.getSortHeader(__('Cores'), 'cores' ,52).'</th>
					<th align="center" class="min_width">'.getSortHeader(__('Bandwidth'), 'bandwidth').'</th>
					<th class="min_width text-center">'.__('Status').'</th>
					<th colspan="2" class="min_width text-right">'.__('Manage').'</th>
				</tr>';
				$i = 1;
				foreach($plans as $k => $v){
				echo 
				'<tr>
					<td width="24" >
						'.get_checkbox('plans_list[]', '', '', array('chk_it' => 0), $k).'
					</td>
					<td align="left">'.$v['plid'].'</td>
					<td class="table_text_wrap">'.$v['plan_name'].'</td>
					<td align="center"><script>document.write(show_virtname("'.$v['virt'].'"));</script></td>
					<td align="left">'.size_converter($v['space'], "GB").'</td>
					<td align="left">'.size_converter($v['ram']).'</td>
					<td align="left">'.size_converter((!empty($v['swap']) ? $v['swap'] : $v['burst'])).'</td>
					<td align="left">'. $v['cpu'].'</td>
					<td align="left">'.$v['cores'].'</td>
					<td align="left">'.size_converter($v['bandwidth'], "GB").'</td>
					<td align="center">
						<h5 class="p-0 m-0"><a class="badge '.(empty($v['is_enabled']) ? 'badge-danger' : 'badge-success').'" title="'.(empty($v['is_enabled']) ? __('Plan is Disabled') : __('Plan is Enabled')).'">'.(empty($v['is_enabled']) ? __('Disabled') : __('Enabled')).'</a></h5>
					</td>
					<td class="text-right" width="24">
						<div class="badge_container">
							<a class="badge badge-manage" href="?act=editplan&plid='.$v['plid'].'" title="Edit"><i class="fas fa-pencil-alt"></i></a>
							<a class="badge badge-trash" href="javascript:void(0);" onclick="return action_chosen(\'delete\', '.$k.');" title="Delete"><i class="fas fa-trash"></i></a>
						</div>
					</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 class="custom-select" name="plans_task_select" id="plans_task_select">
								<option value="0">---</option>
								<option value="delete">'.__('Delete').'</option>
								<option value="enable">'.__('Enable').'</option>
								<option value="disable">'.__('Disable').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="plans_submit" class="go_btn" name="plans_submit" value="'.__('Go').'" onclick="action_chosen(\'\' ,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>
	<br>
	<div id="progress_bar" style="display:none;">
		<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="'.$globals['ind'].'act=addplan" class="link_btn">'.__('Add Plan').'</a>
	</div>
</div>
<br>';

softfooter();

}
?>