<?php

//////////////////////////////////////////////////////////////
//===========================================================
// manage_baremetal_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 manage_baremetal_theme(){

global $theme, $globals, $kernel, $l, $error, $done, $SESS, $baremetal, $servergroups, $info;

softheader($l['<title>']);

$GLOBALS['breadcrumbs'] = array( 'dashboard', 'lm_list_baremetals', 'lms_manage_baremetal');

$cutom_html = '

<div class="d-flex align-items-center gap-2">
	<span class="terminal">
		<a id="jviewer" href="javascript: get_javaconsole();">
			'.__('JViewer').'
		</a>
	</span>
	<span class="terminal">
		<a id="bm_start" href="javascript: void(0);">
			<i class="fas fa-play d-flex align-items-center justify-content-center replace-class text-success"></i>
		</a>
	</span>
	<span class="terminal">
		<a id="bm_stop" href="javascript: void(0);">
			<i class="fas fa-stop d-flex align-items-center justify-content-center replace-class text-danger"></i>
		</a>
	</span>
	<span class="terminal">
		<a id="bm_restart" href="javascript: void(0);">
			<i class="fas fa-sync d-flex align-items-center justify-content-center replace-class text-primary"></i>
		</a>
	</span>
	<h5 class="p-0 m-0 table-hover">
		<a id="bm_status" class="badge badge-success"></a>
	</h5>
	<i class="fas fa-sync ml-2 cursor-pointer" id="refresh_status" onclick="getvpsstatus();"></i>
</div>';

echo page_header(__('Manage Baremetal'), 'fas fa-servers', '', 0, 0, [], $cutom_html);

error_handle($error);

echo '<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
var task_lang = new Array();
task_lang["delete"] = "'.__J('Removing VPS having ID - ').'";
task_lang["poweroff"] = "'.__J('Powering Off VPS having ID - ').'";
task_lang["start"] = "'.__J('Starting VPS having ID - ').'";
task_lang["stop"] = "'.__J('Sending Shutdown signal to VPS having ID - ').'";
task_lang["restart"] = "'.__J('Restaring VPS having ID - ').'";

task_lang["poweroff_task"] = "'.__J('Power Off').'";
task_lang["start_task"] = "'.__J('Start').'";
task_lang["stop_task"] = "'.__J('Stop').'";
task_lang["restart_task"] = "'.__J('Restart').'";

var from_modal = 0;

function vpsmodal(uuid){
	$("#conf_modal").modal("show");
	$("#saveset").attr("onclick", "from_modal=1;$(\"#conf_modal\").modal(\"hide\");action_chosen(\'delete\', "+uuid+");");
};

function action_chosen(action, uuid ,ele){
	
	action = action || "";
	uuid = uuid || 0;
	
	if(action == ""){
		if($(ele).parent().prev().find("select").val() == 0){
				showalert("'.__J('no_action').'","1");
				return false;
		}else{
			action = $(ele).parent().prev().find("select").val();
		}
	}
	
	// List of ids to delete
	var usr_list = new Array();
	
	if(uuid < 1){
		
		$(".ios:checked").each(function(){
			usr_list.push($(this).val());
		});
		
	}else{
		
		usr_list.push(uuid);
		
	}
	
	if(usr_list.length < 1){
		showalert("'.__J('nothing_selected').'","1");
		return false;
	}
	
	// Just check for delete action
	if(action == "delete" && from_modal == 0){
		vpsmodal();
		from_modal = 1;
		return true;
	}
	
	// To delete the user
	if(action == "delete"){
		
		showalert("'.__J('del_conf').'" ,"3" ,function(confirm){
			if(confirm == false){
				return false;
			}else{
				var finalData = new Object();
				var dont_del_vm = $("#dvps").prop("checked");
		
				finalData["dont_delete_vms"] = (dont_del_vm == true ? 1 : 0);
				finalData["delete"] = usr_list.join(",");
				if(dont_del_vm == false){
					animate_task("'.__J('delvps_task').'");
				}
				perform_action(finalData);
			}
		});
		
	// Not a valid action 
	}else{
		showalert("'.__J('no_action').'","1");
		return false;
	}
	
	return false;
}

function perform_action(finalData, uuid = 0){
	
	//alert(finalData);
	//return false;
	
	$("#progress_bar").show();
	$.ajax({
		type: "POST",
		url: "'.$globals['index'].'act=list_baremetal&api=json",
		data : finalData,
		dataType : "json",
		success: function(data){
			$("#progress_bar").hide();
			if(!empty(uuid)){
				$("#showload_"+uuid).hide();
			}
			if("done" in data){
				setTimeout(function(){showalert("'.__J('action_completed').'");
				$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
				});} ,800);
			}
			
			if("error" in data){
				setTimeout(function(){showalert(data["error"],"1");
				$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
				});} ,800);
			}
		},
		error: function(data) {
			if(!empty(uuid)){
				$("#showload_"+uuid).hide();
			}
			$("#progress_bar").hide();
			return false;
		}
	});
	
	return false;
}

// ]]></script>';

if($done){
	show_alert(__('saved'));
}

if(empty($baremetal)){
	echo '<div class="notice"><b>'.__('note').' : </b> &nbsp;'.__('No Baremetal Sever found').'</div>';
}else{

	echo'
<script language="javascript" type="text/javascript"><!-- // --><![CDATA[
	
	// Get the VPS Status
	function getvpsstatus(){
		
		$("#refresh_status").addClass("fa-spin");
		
		$.ajax({type: "GET",
			url: "'.$globals['index'].'act=list_baremetals&vs_status='.$baremetal['vpsid'].'&api=json",
			data:null,
			success:function(response){
				// alert(response);
				res = $.parseJSON(response);
				if(!empty(res["status"]['.$baremetal['vpsid'].']["status"])){
					$("#bm_status").removeClass("badge-danger").addClass("badge-success");
					$("#bm_status").html("'.__J('Running').'");
				}else{
					$("#bm_status").removeClass("badge-success").addClass("badge-danger");
					$("#bm_status").html("'.__J('Stopped').'");
				}

				// Stop the spinning effect after handling the response
				$("#refresh_status").removeClass("fa-spin");
			},
			error: function() {

				// Stop the spinning effect in case of an error as well
				$("#refresh_status").removeClass("fa-spin");
			}
		});
}

$(document).ready(function(){
	getvpsstatus();
});

function get_javaconsole(){
	$("#jviewer").html("<i class=\"fas fa-sync fa-spin d-flex align-items-center justify-content-center replace-class\"></i>");
	$.ajax({type: "GET",
		url: "'.$globals['index'].'act=manage_baremetal&get_java=1&uuid='.$baremetal['uuid'].'&api=json",
		data:null,
		success:function(response){
			res = $.parseJSON(response);
			var blob=new Blob([res.java_console]);
			var link=document.createElement("a");
			link.href=window.URL.createObjectURL(blob);
			link.download="java_console_'.$baremetal['vpsid'].'.jnlp";
			link.click();
			$("#jviewer").html("'.__J('JViewer').'");
		}
	});
};

function attach_iso(){

	show_loader("media_loader");
	var iso_url = $("#iso_url").val();

	if(empty(iso_url)){
		showalert("'.__J('No ISO URL provided').'","1");
		show_loader("media_loader");
		return false;
	}
		
	post_data = {"iso_url":iso_url};
	
	$("#attach_iso_div").modal("hide");

	$.ajax({type: "POST",
		url: "'.$globals['index'].'act=manage_baremetal&attach_iso=1&uuid='.$baremetal['uuid'].'&api=json",
		data: post_data,
		success:function(response){
			res = $.parseJSON(response);
			if("error" in res){
				showalert(res["error"]["msg"], 1)
			}

			if("success" in res){
				showalert(res["success"]["msg"], 0)
			}

			show_loader("media_loader");

			$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
			});
		}
	});

};

function detach_iso(){

	show_loader("media_loader");

	$.ajax({type: "POST",
		url: "'.$globals['index'].'act=manage_baremetal&dettach_iso=1&uuid='.$baremetal['uuid'].'&api=json",
		data: {},
		success:function(response){
			res = $.parseJSON(response);
			if("error" in res){
				showalert(res["error"]["msg"], 1)
			}

			if("success" in res){
				showalert(res["success"]["msg"], 0)
			}
			
			show_loader("media_loader");

			$("#alert-modal").on("hidden.bs.modal", function (e) {
				location.reload(true);
			});
		}
	});

};

</script>
<div class="row dashboard_row mt-3 mx-auto w-100">
	<!-- card for hypervisor count -->
	<div class="col-lg-3 col-sm-6 col-md-6 mb-3 res_card h-50">
		<div class="card3 cluster-card1 res_card h-25 d-flex flex-column">
			<div class="card_icon_title">
				<h5 class="text-nowrap m-0 d-flex flex-fill align-items-center justify-content-between mx-1">
					<div>
						<img  src="'.$theme['svg'].'server.svg" title="'.__('Baremetal Provider').'"/>
						'.__('Baremetal Provider').'
					</div>
				</h5>
			</div>
			<div class="d-flex justify-content-center row flex-grow-1">
				<div class="d-flex justify-content-between align-items-center">
					'.get_ipmiprovider($baremetal['ipmi_provider'], $baremetal['uuid'], 1).'
				</div>
			</div>
			<div class="mx-auto">
				<label class="">'.get_ipmiprovider($baremetal['ipmi_provider']).'</label>
			</div>
		</div>
	</div>
	<!-- card for Location -->
	<div class="col-lg-3 col-sm-6 col-md-6 mb-3 res_card h-50">
		<div class="card3 cluster-card1 res_card h-25 d-flex flex-column">
			<div class="card_icon_title">
				<h5 class="text-nowrap m-0 d-flex flex-fill align-items-center justify-content-between mx-1">
					<div>
						<i class="fas fa-map-marker-alt"></i>
							'.__('Location').'
					</div>
				</h5>
			</div>
			<div class="d-flex justify-content-center row flex-grow-1">
				<div class="d-flex justify-content-between align-items-center">
					<img src="'.location_flag($servergroups[$baremetal['sgid']]['sg_data']['location']).'" width="100"/>
				</div>
			</div>
			<div class="mx-auto">
				<label class="">'.$baremetal['server_location'].'</label>
			</div>
		</div>
	</div>
	<!-- card for IPMI details -->
	<div class="col-lg-3 col-sm-6 col-md-6 mb-3 res_card h-50">
		<div class="card3 cluster-card1 res_card h-25">
			<div class="card_icon_title">
				<h5 class="text-nowrap m-0 d-flex flex-fill align-items-center justify-content-between mx-1">
					<div>
						<i class="fas fa-info"></i>
							'.__('IPMI/ILO Details').'
						</a>
					</div>
				</h5>
			</div>
			<div class="d-flex justify-content-center mt-1">
				<div class="w-100 my-auto ml-2">
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('IP').'</label>
						</div>
						<div class="">'.$baremetal['ipmi_ip'].'</div>
					</div>
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('Username').'</label>
						</div>
						<div class="">'.$baremetal['ipmi_username'].'</div>
					</div>
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('Password').'</label>
						</div>
						<div class="">'.$baremetal['ipmi_password'].'</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- card for User details -->
	<div class="col-lg-3 col-sm-6 col-md-6 mb-3 res_card h-50">
		<div class="card3 cluster-card1 res_card h-25">
			<div class="card_icon_title">
				<h5 class="text-nowrap m-0 d-flex flex-fill align-items-center justify-content-between mx-1">
					<div>
						<i class="fas fa-user"></i>
							'.__('User Details').'
						</a>
					</div>
				</h5>
			</div>
			<div class="d-flex justify-content-center mt-1">
				<div class="w-100 my-auto ml-2">
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('User').'</label>
						</div>
						<div class="">'.$baremetal['email'].'</div>
					</div>
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('Status').'</label>
						</div>
						<div class="">'.strtoupper($baremetal['machine_status']).'</div>
					</div>
					<div class="d-flex align-items-center justify-content-between card_content">
						<div class="">
							<label class="greay_label">'.__('Billing Usage').'</label>
						</div>
						<div class="">'.$baremetal['current_usage'].'</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- card for IPMI details -->
<div class="col-lg-3 col-sm-6 col-md-6 mb-3 res_card h-50">
	<div class="card3 cluster-card1 res_card h-25">
		<div class="loading_overlay" id="media_loader" style="display:none">
			<div class="loader-lg"></div>
		</div>
		<div class="card_icon_title">
			<h5 class="text-nowrap m-0 d-flex flex-fill align-items-center justify-content-between mx-1">
				<div>
					<i class="fas fa-compact-disc"></i>
						'.__('Virtual Media Details').'
					</a>
				</div>
			</h5>
		</div>
		<div class="d-flex justify-content-center mt-1 h-100">
			<div class="w-100 my-auto ml-2">';
				if(!empty($info['attached_iso'])){
					echo '
						<div class="d-flex align-items-center justify-content-between card_content">
							<div class="">
								<label class="greay_label">'.__('Media Status').'</label>
							</div>
							<div class="">'.__('Connected').'</div>
						</div>
						<div class="d-flex align-items-center justify-content-between card_content">
							<div class="">
								<label class="greay_label">'.__('ISO Name').'</label>
							</div>
							<div class="">'.$info['attached_iso']['image_name'].'</div>
						</div>
						<div class="d-flex align-items-center justify-content-between card_content">
							<div class="">
								<label class="greay_label">'.__('URL').'</label>
							</div>
							<div class="">'.$info['attached_iso']['image'].'</div>
						</div>
						<div class="d-flex align-items-end justify-content-center card_content">
							<button class="btn d-lg-flex align-items-center justify-content-center" id="detach_iso" onclick="detach_iso();">'.__('Dettach ISO').'</button>
						</div>
					';
				}else{
					echo '
					<div class="d-flex flex-column justify-content-between card_content">
						<div class="d-flex align-items-center justify-content-between card_content">
							<div class="">
								<label class="greay_label">'.__('Media Status').'</label>
							</div>
							<div class="">'.__('Not Connected').'</div>
						</div>
						<div class="d-flex align-items-end justify-content-center card_content">
							<button class="btn d-lg-flex align-items-center justify-content-center" id="attach_iso" data-toggle="modal" data-target="#attach_iso_div">'.__('Attach ISO').'</button>
						</div>
					</div>
					';
				}
				echo '
			</div>
		</div>
	</div>
</div>
<div class="row mx-auto w-100 mb-3">
	
</div>
<!-- Attach ISO Modal starts here -->
<div class="modal attach_iso-modal-lg" id="attach_iso_div">
	<div class="modal-dialog modal-dialog-centered modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<div class="attach_iso-header">
					<i class="fas fa-attach_iso mr-2"></i>
					'.__('Attach ISO').'
				</div>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
			</div>
			<div class="modal-body p-0 m-0" id="attach_iso_content">
				<div class="card p-2">
					<div name="attach_iso_form">
						<input type="hidden" name="act" value="manage_baremetal" />
						<input type="hidden" name="from_theme" value="1" />
						<div class="row mx-auto w-100">
							<div class="col-sm-9 my-2">
								<label>'.__('Add ISO URL').'</label>
								<input type="text" class="form-control w-100" name="iso_url" id="iso_url" size="30" value="'.POSTval('iso_url', '').'" autocomplete="off" />
							</div>
							<div class="col-sm-3 d-flex align-items-end justify-content-between my-2">
								<a href="javascript:void(0)" title="'.__('Attach ISO').'" class=""><button onclick="attach_iso();" class="btn" '.(!empty($info['attached_iso']) ? 'disabled' : '').'>'.__('Attach ISO').'</button></a>
							</div>
						</div>
					</div>
					<div class="notice mx-3">
						'.__('ISO URL must be accessible to this server').'
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- Attach ISO Modal ends here -->
<div id="progress_bar" class="my-3" style="display:none;">
	<br />
	<center>
		<font id="progress_txt" size="4">'.__('action_msg').'</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>
		'.__('notify_msg').'
	</center>
</div>

<div class="modal fade" role="dialog" id="conf_modal">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header" id="conf_modal_head">				
				<span class="fhead text-always-white">'.__('dont_destroy_vps_header').'</span>
				<button type="button" class="close text-always-white" data-dismiss="modal" aria-hidden="true">&times;</button>
			</div>
			<div class="modal-body" id="conf_modal_body">
				<div class="row">
					<div class="col-xs-10 col-sm-6">
						<label class="control-label">'.__('dont_destroy_vps').'&nbsp;<a class="info" data-placement="right" tooltip="'.__('dont_destroy_vps_help').'"><i class="fas text-primary fa-info-circle"></i></a></label> 
					</div>
					<div class="col-xs-2 col-sm-6">
						'.get_checkbox('dvps', '', 'dvps').'
					</div>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" id="saveset" class="go_btn">'.__('dont_destroy_vps_continue').'</button>
			</div>
		</div>
	</div>
</div>';  

}// End of else

softfooter();

}
?>