<?php

//////////////////////////////////////////////////////////////
//===========================================================
// resource_pricing_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 resource_pricing_theme(){

global $theme, $servers, $globals, $kernel, $cluster, $user, $l, $error, $done, $pricing, $servergroups, $resource_types, $rate_types, $pricing_lang;

softheader($l['<title>']);
					
echo '
<script language="javascript" type="text/javascript">	
var servergroups = '.json_encode($servergroups).';

var ratetypes = '.json_encode($rate_types).';

var lang = {};
lang.h_rate = \''.__J('Hourly Rate').'\';
lang.m_rate = \''.__J('Monthly Rate').'\';
lang.y_rate = \''.__J('Yearly Rate').'\';
lang.bandwidth_h_rate = \''.__J('Overage Rate per GB').'\';

</script>';
?>

<script language="javascript" type="text/javascript">

var tmpsgs = {};

// Fill and display modal with available servergroups
function selectregmod(prefix){
	
	if(!(prefix in tmpsgs)){
		tmpsgs[prefix] = {};
	}
	
	var dv = "";
	dv += '<div class="row mx-auto w-100 mh-50 overflow-auto p-1" id="sglist">';
	for(var x in servergroups){
		if(x in tmpsgs[prefix]){
			continue;
		}
		dv += '<div class="col-sm-6 selregrowclass">';
			dv += '<div class="row mx-auto w-100" sgid="'+servergroups[x].sgid+'" onclick="moreregionpricing(\''+prefix+'\', this); $(this).removeAttr(\'onclick\'); $(this).css(\'background-color\', \'var(--primary_color)\'); $(this).css(\'color\', \'var(--always_white)\');">';
				dv += '<div class="col-1">'+servergroups[x].sgid+'</div>';
				dv += '<div class="col-10">'+servergroups[x].sg_name+'</div>';
			dv += '</div>';
		dv += '</div>';
	}
	dv += '</div>';
	
	$("#selectregionforpricing .modal-body").html(dv);
	$("#selectregionforpricing").modal({keyboard: true});
	$("#selectregionforpricing").modal({show:true});
}

// Add selected region (servergroup) pricing row
function moreregionpricing(prefix, selreg, reload){
	
	// Return back if we do not have a valid servergroup to add or if we are not reloading old values back.
	if(reload != 1 && (selreg == "" || $(selreg).attr("sgid") == null)){
		return false;
	}
	
	// Extract sgid
	var rsgid = (reload == 1 ? selreg : $(selreg).attr("sgid"));
	
	if($('#'+prefix+'_h_rate_'+rsgid).length){
		return true;
	}
	
	// Construct div for this sgid:
	var prow = "";
	prow += '<div class="row mx-auto w-100 mb-2">';
	prow += '<div class="col-2">';
	prow += '<label>'+servergroups[rsgid].sg_name+'</label>';
	prow += '</div>';
	for(var i in ratetypes){
		if(prefix == 'bandwidth' && ratetypes[i] != 'h_rate'){
			continue;
		}
		var txt = prefix == 'bandwidth' ? lang[prefix+'_'+ratetypes[i]] : lang[ratetypes[i]];
		prow += '<div class="col-3 pb-0">';
		prow += '<input type="number" step="0.001" class="form-control" placeholder="'+txt+'" name="'+prefix+'_'+ratetypes[i]+'_'+rsgid+'" id="'+prefix+'_'+ratetypes[i]+'_'+rsgid+'" value="" />';
		prow += '</div>';
	}
	prow += '<div class="'+(prefix == 'bandwidth' ? 'col-7' : 'col-1')+' text-right p-0">';
	prow += '<button type="button" class="btn sg red_btn py-2 px-2" onclick="removepricing(\''+prefix+'\', this, \''+rsgid+'\')"><i class="fas fa-minus"></i></button>';
	prow += '</div>';
	prow += '</div>';
	// Append this new div to pricerowcontainer div
	$("#"+prefix+"_price").append(prow);
	
	if(!(prefix in tmpsgs)){
		tmpsgs[prefix] = {};
	}
	
	// Add this to the group
	tmpsgs[prefix][rsgid] = rsgid;
	
}

// Remove selected region pricing row
function removepricing(prefix, ele, sgid){
	
	// Remove selected pricing region
	$(ele).parent().parent().remove();
	
	// Remove this
	delete tmpsgs[prefix][sgid];
	
}

</script>

<?php

echo page_header(__('Resource Pricing'), 'icon icon-config', '', 0, 0, '');

$breadcrummbs = array('dashboard', 'lm_billing', 'lms_resource_pricing');

echo get_breadcrumb($breadcrummbs);

error_handle($error);

if(!empty($done)){
	show_alert(__('The settings have been saved'));
}

echo '
<form accept-charset="'.$globals['charset'].'" action="" method="post" name="config" class="form-horizontal">
	<div class="mx-3 card2 p-3">
	<div class="d-flex" id="resources-card-tab">
		<div>
			<div class="nav flex-column nav-pills" id="resources-tab" role="tablist" aria-orientation="vertical">';
				$i = 0;
				foreach($resource_types as $v){
					$active = ($i == 0) ? ('active') : ('');
					echo'
					<button class="nav-link '.$active.'" id="v-pills-'.$v.'-tab" data-toggle="pill" data-target="#v-pills-'.$v.'" type="button" role="tab" aria-controls="v-pills-home" aria-selected="true">'.$pricing_lang[$v.'_pricing'].'</button>';
					$i++;
				}
			echo'
			</div>
		</div>
		<div class="pl-3 w-100">
			<div class="tab-content" id="resources-tabContent">';
				$i = 0;
				foreach($resource_types as $v){
					$active = ($i == 0) ? ('active show') : ('');
					echo'
						<div class="tab-pane fade '.$active.'" id="v-pills-'.$v.'" role="tabpanel" aria-labelledby="v-pills-home-tab">
							<div class="p-3">
								<h4 class="card_title text-center">'.$pricing_lang[$v.'_pricing'].'</h4>
								<div class="pt-3" id="'.$v.'_price">
									<div class="row mx-auto w-100 mb-2" id="pricerow[-1]">
										<div class="col-2">
											<label>'.__('All Regions').'</label>
										</div>';
										
										foreach($rate_types as $rv){
											
											if($v == 'bandwidth' && $rv != 'h_rate'){
												continue;
											}
											
											$prefix = ($v == 'bandwidth' ? 'bandwidth_' : '');
											
											echo '
											<div class="col-3">
												<input type="number" step="0.001" class="form-control" placeholder="'.$pricing_lang[$prefix.$rv].'" name="'.$v.'_'.$rv.'_-1" id="'.$v.'_'.$rv.'_-1" value="'.POSTval($v.'_'.$rv.'_-1', '').'" />
											</div>';
										}

										echo'			
									</div>
								</div>
								<div class="row mx-0 w-100">
									<div class="col-12 text-right p-0">
										<button type="button" class="btn blue_btn py-2 px-2" data-toggle1="modal" data-target1="#selectregionforpricing" onclick="selectregmod(\''.$v.'\');" ><i class="fas fa-plus"></i></button>
									</div>
								</div>
							</div>
						</div>';
					$i++;
				}
				echo'
			</div>
		</div>
	</div>
	';
		if(!empty($pricing) && empty($_POST)){
			foreach($pricing as $res => $val){
				foreach($val as $rtype => $sval){
					foreach($sval as $sgid => $v){
						$rates[$res.'_'.$rtype.'_'.$sgid] = $v;
					}
				}
			}
		}

		// Are the rates posted ? Then we must add them
		if(!empty($_POST)){
			$rates = $_POST;
		}

		if(!empty($rates)){
			
			echo '<script language="javascript" type="text/javascript">';
			
			foreach($rates as $kk => $vv){
				
				if(!preg_match('/rate/is', $kk)){
					continue;
				}
				
				// If its a regular region
				if(substr($kk, -2) != '-1'){
					$tmp = explode('_', $kk);
					echo '
					moreregionpricing("'.$tmp[0].'", "'.$tmp[3].'", 1);';
				}
				
				echo '
				$("#'.$kk.'").val("'.$rates[$kk].'");';
				
			}
			
			echo '</script>';
			
		}

		echo '
		<div class="modal fade" id="selectregionforpricing" role="dialog">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<div class="modal-title float-left"><b>'.__('Select Region').'</b></div>
						<div class="close" data-dismiss="modal" >X</div>
					</div>
					<div class="modal-body"></div>
				</div>
			</div>
		</div>
		<!-- submit button -->	
		<div class="text-center pt-5">
			<input type="submit" name="editsettings" value="'.__('Save Settings').'" class="go_btn"/>
		</div>
</form>';

softfooter();

}



