<?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, $sort_column, $sort_column_by, $column ,$size = 90){

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=user_plans&sortcolumn='.$column.'&sortby=asc"><i class="fas fa-sort-up"></i></a><br>
			<a class="text-always-white" href="'.$globals['index'].'act=user_plans&sortcolumn='.$column.'&sortby=desc"><i class="fas fa-sort-down"></i></a>
		</span>
	</div>';
	return $str;
}

function user_plans_theme(){

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

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

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

echo page_header(__('User Plans'), 'fas fa-clipboard','', 1, 0, $buttons);

error_handle($error);

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

function Delplans(uplid ,ele){

	uplid = uplid || 0;
	
	// List of ids to delete
	var plan_list = new Array();

	let default_uplid = "'.$default_uplid.'" || "";
	
	if(uplid < 1){
		
		if($(ele).parent().prev().find("select").val() != 1){
			showalert("'.__J('No action selected !').'","1");
			return false;
		}
		
		$(".ios:checked").each(function() {
			plan_list.push($(this).val());
		});
		
	}else{
		
		plan_list.push(uplid);
		
	}
	
	if(plan_list.length < 1){
		showalert("'.__J('Nothing selected !').'","1");
		return false;
	}
	
	//checking the default uplid for single delete and multiple delete
	if(default_uplid !== uplid && !plan_list.includes(default_uplid)){
		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(",");

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

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

				$.ajax({
					type: "POST",
					url: "'.$globals['index'].'act=user_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);
						}
					},
					error: function(data) {
						$("#progress_bar").hide();
						//alert(data.description);
						return false;
					}
				});
			}
		});
	
	}else{

		// checking the multiple delete plans
		if(plan_list.length > 1){
			showalert("'.__J('Please unselect the $0 - Default User Plan to delete other user plans.', [$default_uplid]).'", "1")
		}else{
			showalert("'.__J('Cannot delete the default plan. Please set another plan as default to avoid affecting registration and billing settings.').'", "1")
		}

		return false;
	}

	return false;

};

// ]]></script>

<div class="card p-2 mx-3">
	<div id="showsearch" class="mb-2" 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="user_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="form-control virt-select">
					<option value="-1" '.REQselect('ptype', '', true).'>---</option>
					<option value="0" '.REQselect('ptype', '0').'>'.__('User').'</option>
					<option value="2" '.REQselect('ptype', '2').'>'.__('Cloud').'</option>
					<option value="1" '.REQselect('ptype', '1').'>'.__('Admin').'</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'));
			//echo '<div class="notice_pop"><b>NOTE : </b>&nbsp; '..'</div>';
		}

		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=adduser_plans">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="1">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="plans_submit" class="go_btn" name="plans_submit" value="'.__('Go').'" onclick="Delplans(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 min_width" align="center">
					'.get_checkbox('select_all', '', 'select_all', array('chk_it' => 0), '', 'select_all').'
				</th>
				<th align="center" class="min_width">
				'.getSortHeader(__('ID'), $sort_column, $sort_column_by, 'uplid' ,30).'
				</th>
				<th  align="center">'.getSortHeader(__('Name'), $sort_column, $sort_column_by, 'plan_name' ,51).'</th>
				<th class="text-center" width="70">'.__('User Type').'</th>
				<th align="center" width="75">'.getSortHeader(__('Max Space'), $sort_column, $sort_column_by, 'space' ,83).'</th>
				<th align="center" width="75">'.getSortHeader(__('Max RAM'), $sort_column, $sort_column_by, 'ram' ,70).'</th>
				<th align="center" width="75">'.__('Max Burst').'</th>
				<th align="center" width="75">'.getSortHeader(__('CPU Units'), $sort_column, $sort_column_by, 'cpu' ,75).'</th>
				<th align="center" width="75">'.getSortHeader(__('Max Cores'), $sort_column, $sort_column_by, 'cores' ,80).'</th>
				<th  align="center" width="60">'.getSortHeader(__('Max Bandwidth'), $sort_column, $sort_column_by, 'bandwidth' ,111).'</th>
				<th class="text-right" colspan="2">'.__('Manage').'</th>
			</tr>';
			$i = 1;
			$user = array('user.svg' ,'admin.svg', 'cloud_users.svg');
			$user_title = array( __('User'), __('Admin'), __('Cloud User'));

			foreach($plans as $k => $v){
		echo '<tr>
			<td width="20" valign="top" align="center">
				'.get_checkbox('plans_list[]', '', '', array('chk_it' => 0), $k).'
			</td>
			<td align="center">'.$v['uplid'].'</td>
			<td>'.$v['plan_name'].'</td>
			<td align="center"><h6><img src="'.$theme['svg'].$user[$v['type']].'" title="'.$user_title[$v['type']].'"></h6></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((empty($v['bandwidth']) ? __('Unlimited') : $v['bandwidth']), "GB").'</td>
			<td align="center" width="24">
				<div class="badge_container">
					<a class="badge badge-manage" href="?act=edituser_plans&uplid='.$v['uplid'].'" title="Edit"><i class="fas fa-pencil-alt"></i></a>
					<a class="badge badge-trash" href="javascript:void(0);" onclick="return Delplans('.$k.');" title="Delete"><i class="fas fa-trash"></i></a>
				</div>
			</td>
			</tr>';	
			$i++;
			}

		echo '</table>
		</div>';
		echo'<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="1">'.__('Delete').'</option>
							</select>
						</div>
						<div class="pl-2">
							<input type="submit" id ="plans_submit" class="go_btn" name="plans_submit" value="'.__('Go').'" onclick="Delplans(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="'.$globals['ind'].'act=adduser_plans" class="go_btn">'.__('Add User Plan').'</a>
		</div>
</div>
<br>';
softfooter();

}
?>